:root {
  --brand: #1f3a5f;
  --brand-2: #2c5282;
  --accent: #f59e0b;
  --bg: #f3f5f8;
  --card: #fff;
  --line: #d8dee6;
  --text: #1c2430;
  --muted: #64707f;
  --ok: #1a7f37;
  --ok-bg: #e6f6ea;
  --err: #b42318;
  --err-bg: #fdecea;
  --warn-bg: #fff4dd;
  --radius: 10px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: var(--brand-2); }
h1 { font-size: 1.45rem; margin: .25rem 0 1rem; }
h2 { font-size: 1.1rem; margin: 1.75rem 0 .6rem; }
h3 { font-size: 1rem; margin: 0 0 .6rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.late { color: var(--err); font-weight: 600; }

/* Toppmeny */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  background: var(--brand); color: #fff;
  padding: .6rem 1rem; padding-top: calc(.6rem + env(safe-area-inset-top));
  box-shadow: 0 2px 6px rgba(0,0,0,.15);
}
.brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 1.1rem; }
.brand small { color: var(--accent); font-weight: 600; margin-left: .2rem; }
.menu summary {
  list-style: none; cursor: pointer; width: 44px; height: 44px;
  display: flex; flex-direction: column; justify-content: center; gap: 5px; padding: 0 10px;
}
.menu summary::-webkit-details-marker { display: none; }
.menu summary span { display: block; height: 2px; background: #fff; border-radius: 2px; }
.menu nav {
  position: absolute; right: .5rem; top: 100%;
  background: var(--card); border-radius: var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.2);
  min-width: 230px; padding: .4rem; display: flex; flex-direction: column;
}
.menu nav a, .menu nav .link {
  color: var(--text); text-decoration: none; padding: .75rem .9rem; border-radius: 8px; text-align: left;
}
.menu nav a.active { background: #e8eef7; color: var(--brand); font-weight: 600; }
.menu nav a:hover { background: var(--bg); }
.logout { border-top: 1px solid var(--line); margin-top: .3rem; padding-top: .3rem; }

main { max-width: 1150px; margin: 0 auto; padding: 1rem; }
main.bare { max-width: 560px; padding-top: 2.5rem; }

/* Kort, knappar, formulär */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
button, .button {
  display: inline-flex; align-items: center; justify-content: center; gap: .3rem;
  min-height: 44px; padding: .55rem 1rem; border-radius: 8px; border: 1px solid var(--line);
  background: #fff; color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
}
button:hover, .button:hover { border-color: var(--brand-2); }
.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.primary:hover { background: var(--brand-2); }
.danger { color: var(--err); border-color: #f1b5ae; background: #fff; }
button.small { min-height: 36px; padding: .3rem .7rem; font-size: .9rem; }
button.link { border: 0; background: none; min-height: 0; font-weight: 400; width: 100%; justify-content: flex-start; }
input, select, textarea {
  width: 100%; min-height: 44px; padding: .55rem .7rem; font: inherit; font-size: 16px;
  border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--text);
}
textarea { min-height: 80px; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc0ef; border-color: var(--brand-2); }
.form label { display: block; font-weight: 600; font-size: .92rem; margin-bottom: .8rem; }
.form label input, .form label select, .form label textarea { margin-top: .3rem; font-weight: 400; }
.form label.check { display: flex; gap: .5rem; align-items: center; font-weight: 400; }
.form label.check input { width: auto; min-height: 0; margin: 0; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem .2rem; margin: 0 0 1rem; }
legend { font-weight: 700; padding: 0 .3rem; }
.grid { display: grid; grid-template-columns: 1fr; gap: 0 1rem; }
.actions { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; margin-top: .5rem; }
.actions form { margin: 0; }
.inline { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.inline input { flex: 1 1 140px; width: auto; }
.inline input.short { flex: 0 1 90px; }
.danger-zone { margin-top: 2rem; }

.head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.head h1 { margin-bottom: .6rem; }
.crumb { margin: 0 0 .3rem; font-size: .92rem; }
.crumb a { text-decoration: none; }
.quick { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }

.search { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; align-items: center; }
.search input[type=search] { flex: 1 1 200px; width: auto; }
.chips { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.chip {
  display: inline-flex; align-items: center; min-height: 36px; padding: .3rem .85rem; border-radius: 999px;
  border: 1px solid var(--line); background: #fff; text-decoration: none; color: var(--text); font-size: .92rem;
}
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }

.flash { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; background: var(--ok-bg); color: var(--ok); }
.flash.error { background: var(--err-bg); color: var(--err); }

.badge { display: inline-block; padding: .1rem .55rem; border-radius: 999px; font-size: .8rem; font-weight: 600; background: #e8eef7; color: var(--brand); white-space: nowrap; }
.badge.done { background: var(--ok-bg); color: var(--ok); }
.badge.partial { background: var(--warn-bg); color: #8a5300; }

/* Översikt */
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: .7rem; margin-bottom: .5rem; }
.stat { display: flex; flex-direction: column; text-decoration: none; color: var(--text); margin: 0; padding: .9rem; }
.stat .value { font-size: 1.7rem; font-weight: 700; color: var(--brand); line-height: 1.1; }
.cols { display: grid; grid-template-columns: 1fr; gap: 0 1.5rem; }
ul.plain { list-style: none; margin: 0; padding: 0; }
ul.plain li { padding: .65rem 1rem; border-bottom: 1px solid var(--line); display: flex; flex-wrap: wrap; justify-content: space-between; gap: .3rem 1rem; }
ul.plain li:last-child { border-bottom: 0; }
ul.plain.card { padding: 0; }

.info { display: grid; grid-template-columns: repeat(2, 1fr); gap: .8rem 1rem; }
.info > div { display: flex; flex-direction: column; }
.info .full { grid-column: 1 / -1; }
.info .muted { font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.stockbox { display: flex; flex-wrap: wrap; gap: 1rem; align-items: center; justify-content: space-between; }
.stockbox > div { display: flex; flex-direction: column; }
.stockbox strong { font-size: 1.5rem; }
.stockbox form { flex: 1 1 320px; }
.addline { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }
.addline h3 { flex-basis: 100%; }
.addline select { flex: 1 1 100%; }
.addline input { flex: 1 1 120px; width: auto; }
details.upload summary { cursor: pointer; min-height: 32px; }
details.upload[open] summary { margin-bottom: 1rem; }
details.mini summary { cursor: pointer; color: var(--brand-2); }
details.mini form { margin-top: .5rem; }

/* Tabeller: kort på mobil, tabell på större skärm */
.rtable { width: 100%; border-collapse: collapse; margin-bottom: 1rem; }
.rtable thead { display: none; }
.rtable tbody tr {
  display: block; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .6rem .9rem; margin-bottom: .6rem;
}
.rtable tbody tr.old { opacity: .7; }
.rtable td { display: flex; justify-content: space-between; gap: 1rem; padding: .25rem 0; text-align: right; }
.rtable td::before { content: attr(data-label); color: var(--muted); font-size: .88rem; text-align: left; flex-shrink: 0; }
.rtable td.title { font-weight: 700; font-size: 1.05rem; justify-content: flex-start; text-align: left; }
.rtable td.title::before, .rtable td.actions-cell::before, .rtable tr.empty td::before { content: none; }
.rtable td.actions-cell { justify-content: flex-end; flex-wrap: wrap; gap: .5rem; padding-top: .5rem; }
.rtable td.actions-cell form { margin: 0; }
.rtable tr.empty td { justify-content: center; color: var(--muted); }
.rtable tfoot tr { display: flex; justify-content: space-between; padding: .5rem .9rem; font-weight: 700; }
.rtable tfoot th { padding: 0; }
.rtable tfoot th:empty { display: none; }

.auth-box { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.5rem; }
.auth-box h1 { margin-top: 0; }
.auth-box button[type=submit] { width: 100%; margin-top: .5rem; }
.mysql-fields { border-left: 3px solid var(--line); padding-left: .8rem; margin: .3rem 0 .8rem; }

@media (min-width: 760px) {
  main { padding: 1.5rem; }
  h1 { font-size: 1.7rem; }
  .grid { grid-template-columns: 1fr 1fr; }
  .grid .full { grid-column: 1 / -1; }
  .stats { grid-template-columns: repeat(3, 1fr); }
  .cols { grid-template-columns: 1fr 1fr; }
  .info { grid-template-columns: repeat(4, 1fr); }
  .addline select { flex: 2 1 260px; }
  .auth-box.wide { max-width: 560px; }

  .rtable { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
  .rtable thead { display: table-header-group; }
  .rtable tbody tr { display: table-row; border: 0; border-radius: 0; padding: 0; margin: 0; background: none; }
  .rtable tbody tr:hover { background: #f8fafc; }
  .rtable th, .rtable td { display: table-cell; padding: .6rem .75rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
  .rtable td::before { content: none; }
  .rtable th { background: #eef2f7; font-size: .85rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); }
  .rtable td.title { font-size: 1rem; }
  .rtable .num { text-align: right; }
  .rtable td.actions-cell { text-align: right; white-space: nowrap; }
  .rtable td.actions-cell form { display: inline-flex; margin-left: .3rem; }
  .rtable td.actions-cell .inline input.short { width: 90px; }
  .rtable tfoot tr { display: table-row; }
  .rtable tfoot th { display: table-cell; padding: .6rem .75rem; text-align: left; }
  .rtable tfoot th.num { text-align: right; }
  .rtable tfoot th:empty { display: table-cell; }
  .menu nav { right: 1rem; }
}

@media print {
  .top, .noprint, .flash, .danger-zone, .crumb { display: none !important; }
  body { background: #fff; }
  main { max-width: none; padding: 0; }
  .card { border: 0; padding: 0; }
  .rtable { border: 0; }
  .rtable thead { display: table-header-group; }
  .rtable tbody tr { display: table-row; border: 0; padding: 0; margin: 0; }
  .rtable th, .rtable td { display: table-cell; border-bottom: 1px solid #ccc; padding: .3rem .4rem; text-align: left; }
  .rtable td::before { content: none; }
  .rtable .num { text-align: right; }
  .rtable tfoot tr { display: table-row; }
  .rtable tfoot th { display: table-cell; }
  .info { grid-template-columns: repeat(4, 1fr); }
}

/* --- Version 2: filtyper, arbetsorder, planering --- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ftype {
  display: inline-block; min-width: 2.6rem; padding: .05rem .35rem; border-radius: 5px; text-align: center;
  font-size: .72rem; font-weight: 700; letter-spacing: .02em; color: #fff; background: #6b7280; vertical-align: middle;
}
.ftype.inventor { background: #e8700e; }
.ftype.autocad { background: #c8102e; }
.ftype.pdf { background: #7c3aed; }
.ftype.cad { background: #0f766e; }
.fchip { text-decoration: none; margin: 0 .15rem .15rem 0; display: inline-block; }
.chips-wrap { display: flex; flex-wrap: wrap; gap: .25rem; justify-content: flex-end; }
.chips-wrap .fchip { margin: 0; }
a.button.small, .button.small { min-height: 36px; padding: .3rem .7rem; font-size: .9rem; }
.prio { font-size: .85rem; font-weight: 600; }
.prio.p-hög { color: #b45309; }
.prio.p-brådskande { color: var(--err); }
.prio.p-låg { color: var(--muted); }
.weeknav { display: flex; gap: .4rem; }
.weeknav .button { min-width: 44px; }
.head h2 a, h2 a.small { font-weight: 400; margin-left: .5rem; }

/* Planering: lista på mobil, rutnät på större skärm */
.plan-grid { display: none; }
.plan-list h3 { margin: 1.1rem 0 .4rem; font-size: .95rem; }
.plan-list h3.today { color: var(--brand-2); }
.slot {
  display: block; text-decoration: none; color: var(--text); background: #e8eef7; border-left: 4px solid var(--brand-2);
  border-radius: 6px; padding: .3rem .45rem; margin-bottom: .35rem; font-size: .82rem; line-height: 1.25;
}
.slot:hover { background: #dbe6f5; }
.slot .t { font-weight: 700; display: block; }
.slot .n { font-weight: 600; }
.slot .d { display: block; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.slot.prio-hög { border-left-color: #d97706; }
.slot.prio-brådskande { border-left-color: var(--err); background: #fdecea; }
.slot.is-done { opacity: .55; }

@media (min-width: 760px) {
  .plan-list { display: none; }
  .plan-grid {
    display: grid; grid-template-columns: 170px repeat(var(--days), minmax(110px, 1fr));
    background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 1rem;
  }
  .pg-head { background: #eef2f7; padding: .5rem .6rem; font-size: .85rem; font-weight: 700; color: var(--muted); border-bottom: 1px solid var(--line); border-left: 1px solid var(--line); }
  .pg-corner { border-left: 0; }
  .pg-head.today { color: var(--brand); background: #dde7f5; }
  .pg-res { padding: .5rem .6rem; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: .1rem; }
  .pg-cell { padding: .35rem; border-bottom: 1px solid var(--line); border-left: 1px solid var(--line); min-height: 70px; }
  .pg-cell.today { background: #f6f9fd; }
  .chips-wrap { justify-content: flex-start; }
}
@media print {
  .plan-list { display: none; }
  .plan-grid { display: grid !important; grid-template-columns: 140px repeat(var(--days), 1fr); }
  .pg-head, .pg-res, .pg-cell { border: 1px solid #ccc; padding: .25rem; }
}

/* --- Version 3: projekt och materialnummer --- */
.material-list { border: 1px dashed var(--line); border-radius: 8px; padding: .5rem .75rem; margin-bottom: .8rem; }
.material-list p { margin: 0 0 .4rem; }
.material-row { display: flex !important; align-items: center; gap: .6rem; font-weight: 400 !important; margin-bottom: .4rem !important; }
.material-row input { flex: 0 0 11rem; width: 11rem; margin: 0 !important; }
.material-row span { font-size: .9rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.material-cell input.material-input { min-height: 38px; padding: .3rem .5rem; max-width: 12rem; }
.addline .material-list { flex-basis: 100%; }
@media (min-width: 760px) {
  .material-cell input.material-input { width: 10rem; }
}
