/* Handling devices web app - single stylesheet.
   Tokens first, then base, then components. Mobile-first; no JS needed.
   Class reference and contrast table: docs/web-css.md */

:root {
  color-scheme: light dark;

  /* colour tokens (light) */
  --bg: #f3f5f8;
  --surface: #ffffff;
  --sunken: #e9edf2;
  --text: #18202b;
  --muted: #4a5565;
  --line: #7f8a9a;
  --line-soft: #d3d9e1;
  --accent: #0a58a8;
  --danger: #b3261e;
  --ok: #17693a;
  --warn: #8a5300;
  --on-solid: #ffffff;
  --accent-bg: #e3eefb;
  --ok-bg: #e0f3e8;
  --danger-bg: #fbe4e2;
  --warn-bg: #fdf0d5;
  --focus: #0a58a8;
  --shadow: 0 1px 2px rgba(16, 24, 40, .08);

  /* spacing scale */
  --s-1: .25rem;
  --s-2: .5rem;
  --s-3: .75rem;
  --s-4: 1rem;
  --s-5: 1.5rem;
  --s-6: 2rem;
  --s-7: 3rem;

  /* type scale */
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --t-sm: .875rem;
  --t-base: 1rem;
  --t-lg: 1.25rem;
  --t-xl: 1.625rem;

  --radius: 6px;
  --control-h: 2.75rem; /* 44px */
  --gutter: 1rem;
  --maxw: 72rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141b;
    --surface: #181f29;
    --sunken: #202938;
    --text: #e7ebf1;
    --muted: #a8b3c2;
    --line: #6b788a;
    --line-soft: #2c3645;
    --accent: #7ab8f5;
    --danger: #ff9087;
    --ok: #74d69f;
    --warn: #f2bb54;
    --on-solid: #08121f;
    --accent-bg: #1c3350;
    --ok-bg: #173a29;
    --danger-bg: #472322;
    --warn-bg: #46350f;
    --focus: #7ab8f5;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background-color: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--t-base);
  line-height: 1.5;
  overflow-wrap: anywhere;
}

main { display: block; max-width: var(--maxw); margin: 0 auto; padding: var(--s-4) var(--gutter) var(--s-7); }

h1, h2, h3 { line-height: 1.25; margin: 0 0 var(--s-3); font-weight: 650; }
h1 { font-size: var(--t-xl); }
h2 { font-size: var(--t-lg); margin-top: var(--s-5); }
h3 { font-size: var(--t-base); margin-top: var(--s-4); }
p { margin: 0 0 var(--s-3); }
img, svg, video { max-width: 100%; height: auto; }
hr { border: 0; border-top: 1px solid var(--line-soft); margin: var(--s-5) 0; }
code, kbd { font-family: var(--mono); font-size: .9em; background: var(--sunken); padding: .1em .35em; border-radius: 4px; }

a { color: var(--accent); text-decoration: underline; text-underline-offset: .15em; }
a:hover { text-decoration-thickness: 2px; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 3px; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: var(--s-2); top: -4rem; z-index: 100;
  background: var(--accent); color: var(--on-solid); padding: var(--s-3) var(--s-4);
  border-radius: var(--radius); font-weight: 600; text-decoration: none;
}
.skip-link:focus { top: var(--s-2); }

.muted { color: var(--muted); }
.small { font-size: var(--t-sm); }
.mono { font-family: var(--mono); }

.stack > * + * { margin-top: var(--s-4); }
.stack--tight > * + * { margin-top: var(--s-2); }

.grid { display: grid; gap: var(--s-4); grid-template-columns: 1fr; }
@media (min-width: 640px) { .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 960px) { .grid--3 { grid-template-columns: repeat(3, minmax(0, 1fr)); } }

/* ---------- header and nav ---------- */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--line-soft);
  box-shadow: var(--shadow);
}
.site-header__inner {
  max-width: var(--maxw); margin: 0 auto; padding: var(--s-2) var(--gutter);
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-4);
}
.site-header .brand {
  font-weight: 700; color: var(--text); text-decoration: none; letter-spacing: .01em;
  min-height: var(--control-h); display: inline-flex; align-items: center;
}
.site-header .who { margin-left: auto; display: flex; align-items: center; gap: var(--s-3); flex-wrap: wrap; font-size: var(--t-sm); color: var(--muted); }

.site-nav { display: flex; flex-wrap: wrap; gap: var(--s-1); margin: 0; padding: 0; list-style: none; width: 100%; order: 3; }
.site-nav a {
  display: inline-flex; align-items: center; min-height: var(--control-h); padding: 0 var(--s-3);
  color: var(--text); text-decoration: none; border-radius: var(--radius); border-bottom: 3px solid transparent;
}
.site-nav a:hover { background: var(--sunken); }
.site-nav a[aria-current="page"], .site-nav a.is-current { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
@media (min-width: 768px) {
  .site-nav { width: auto; order: 0; }
}

/* ---------- page head ---------- */
.page-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: var(--s-3) var(--s-4); margin-bottom: var(--s-4);
}
.page-head h1 { margin: 0; }
.page-head .sub { flex-basis: 100%; margin: 0; color: var(--muted); }
.actions { display: flex; flex-wrap: wrap; gap: var(--s-2); align-items: center; }
.actions form { margin: 0; display: inline; }

/* ---------- card ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius);
  padding: var(--s-4); box-shadow: var(--shadow);
}
.card > :first-child { margin-top: 0; }
.card > :last-child { margin-bottom: 0; }
.card h2, .card h3 { margin-top: 0; }
a.card { display: block; color: inherit; text-decoration: none; }
a.card:hover { border-color: var(--accent); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  min-height: var(--control-h); min-width: var(--control-h); padding: 0 var(--s-4);
  font: inherit; font-weight: 600; line-height: 1.2; text-decoration: none; cursor: pointer;
  color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.btn:hover { background: var(--sunken); }
.btn:active { transform: translateY(1px); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--on-solid); }
.btn--primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn--danger { background: var(--danger); border-color: var(--danger); color: var(--on-solid); }
.btn--danger:hover { background: var(--danger); filter: brightness(1.08); }
.btn--link { background: transparent; border-color: transparent; color: var(--accent); text-decoration: underline; padding: 0 var(--s-2); }
.btn--block { width: 100%; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .6; cursor: not-allowed; }
@media (max-width: 479px) { .actions .btn { flex: 1 1 auto; } }

/* ---------- forms ---------- */
.form { max-width: 40rem; }
.form-row { margin: 0 0 var(--s-4); display: block; }
.form-row > label, .form-row > .label, .form-row > legend {
  display: block; font-weight: 600; margin-bottom: var(--s-1); padding: 0;
}
.form-row .hint { display: block; color: var(--muted); font-size: var(--t-sm); margin-top: var(--s-1); }
.form-row .error { display: block; color: var(--danger); font-size: var(--t-sm); margin-top: var(--s-1); font-weight: 600; }
fieldset.form-row { border: 0; padding: 0; min-width: 0; }

input[type="text"], input[type="email"], input[type="password"], input[type="search"], input[type="number"],
input[type="url"], input[type="date"], input[type="tel"], select, textarea {
  display: block; width: 100%; min-height: var(--control-h); padding: var(--s-2) var(--s-3);
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius);
}
textarea { min-height: 7rem; resize: vertical; }
select { appearance: auto; }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 1; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }
[aria-invalid="true"] { border-color: var(--danger); border-width: 2px; }
input:disabled, select:disabled, textarea:disabled { background: var(--sunken); color: var(--muted); cursor: not-allowed; }

input[type="file"] {
  display: block; width: 100%; min-height: var(--control-h); padding: var(--s-2) var(--s-3);
  font: inherit; color: var(--text); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius);
}
input[type="file"]::file-selector-button {
  min-height: 2rem; margin-right: var(--s-3); padding: 0 var(--s-3); font: inherit; font-weight: 600; cursor: pointer;
  color: var(--text); background: var(--sunken); border: 1px solid var(--line); border-radius: var(--radius);
}

.check { display: flex; align-items: center; gap: var(--s-3); min-height: var(--control-h); font-weight: 400; cursor: pointer; }
.check input[type="checkbox"], .check input[type="radio"] { width: 1.5rem; height: 1.5rem; margin: 0; flex: none; accent-color: var(--accent); }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); }

@media (min-width: 640px) {
  .form-row--inline { display: flex; gap: var(--s-3); align-items: end; }
  .form-row--inline > * { flex: 1 1 0; }
  .form-row--inline > .btn { flex: none; }
}

/* ---------- badges ---------- */
.badge {
  display: inline-flex; align-items: center; gap: .35em; padding: .1rem .6rem; min-height: 1.5rem;
  font-size: var(--t-sm); font-weight: 600; line-height: 1.3; white-space: nowrap; vertical-align: middle;
  color: var(--text); background: var(--sunken); border: 1px solid var(--line); border-radius: 999px;
}
.badge--open { color: var(--accent); background: var(--accent-bg); border-color: var(--accent); }
.badge--proposed { color: var(--warn); background: var(--warn-bg); border-color: var(--warn); }
.badge--solved { color: var(--ok); background: var(--ok-bg); border-color: var(--ok); }
.badge--navisworks { color: var(--text); background: var(--sunken); border-style: solid; border-radius: var(--radius); }
.badge--web { color: var(--text); background: var(--surface); border-style: dashed; border-radius: var(--radius); }
.badge--role { color: var(--text); background: var(--surface); border-color: var(--line); font-weight: 500; }
.badge--danger { color: var(--danger); background: var(--danger-bg); border-color: var(--danger); }

/* ---------- messages ---------- */
.msg {
  margin: 0 0 var(--s-4); padding: var(--s-3) var(--s-4); border-radius: var(--radius);
  border: 1px solid var(--line); border-left-width: 6px; background: var(--surface); color: var(--text);
}
.msg--info { background: var(--accent-bg); border-color: var(--accent); }
.msg--ok { background: var(--ok-bg); border-color: var(--ok); }
.msg--error { background: var(--danger-bg); border-color: var(--danger); }
.msg--warn { background: var(--warn-bg); border-color: var(--warn); }
.msg > :last-child { margin-bottom: 0; }
.msg strong:first-child { margin-right: .25em; }

/* ---------- impersonation banner ---------- */
.banner {
  background: var(--warn-bg); color: var(--text); border-bottom: 3px solid var(--warn);
  padding: var(--s-3) var(--gutter);
}
.banner__inner {
  max-width: var(--maxw); margin: 0 auto; display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-2) var(--s-4);
}
.banner form { margin: 0; margin-left: auto; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; max-width: 100%; }
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line-soft); }
.table caption { text-align: left; padding: var(--s-2) 0; color: var(--muted); font-size: var(--t-sm); }
.table th, .table td { padding: var(--s-3); text-align: left; vertical-align: top; border-bottom: 1px solid var(--line-soft); }
.table thead th { background: var(--sunken); font-weight: 650; font-size: var(--t-sm); white-space: nowrap; }
.table tbody tr:hover { background: var(--sunken); }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table .row-actions { white-space: nowrap; }

/* Under 640 px every row becomes a card. Cells need data-label="..." for the caption. */
@media (max-width: 639px) {
  .table--stack, .table--stack caption, .table--stack tbody, .table--stack tr, .table--stack td { display: block; width: 100%; }
  .table--stack { background: transparent; border: 0; }
  .table--stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .table--stack tr { background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius); margin-bottom: var(--s-3); padding: var(--s-2) var(--s-3); }
  .table--stack td { border: 0; padding: var(--s-2) 0; display: grid; grid-template-columns: 7.5rem minmax(0, 1fr); gap: var(--s-3); }
  .table--stack td::before { content: attr(data-label); color: var(--muted); font-size: var(--t-sm); font-weight: 600; }
  .table--stack td:not([data-label])::before { content: none; }
  .table--stack td:not([data-label]) { grid-template-columns: 1fr; }
  .table--stack td.num { text-align: left; }
  .table--stack td > .badge { justify-self: start; }
}

/* ---------- definition list ---------- */
.details { margin: 0; display: grid; grid-template-columns: 1fr; gap: 0; }
.details dt { color: var(--muted); font-size: var(--t-sm); font-weight: 600; margin-top: var(--s-3); }
.details dd { margin: 0; }
.details dt:first-child { margin-top: 0; }
@media (min-width: 640px) {
  .details { grid-template-columns: 11rem minmax(0, 1fr); column-gap: var(--s-4); }
  .details dt, .details dd { padding: var(--s-2) 0; margin: 0; border-bottom: 1px solid var(--line-soft); }
  .details dt { font-size: var(--t-base); }
}

/* ---------- thread ---------- */
.thread { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--s-3); }
.thread-item {
  background: var(--surface); border: 1px solid var(--line-soft); border-left: 4px solid var(--line);
  border-radius: var(--radius); padding: var(--s-3) var(--s-4);
}
.thread-item__meta { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-1) var(--s-3); font-size: var(--t-sm); color: var(--muted); margin-bottom: var(--s-2); }
.thread-item__meta strong { color: var(--text); }
.thread-item__body { white-space: pre-wrap; margin: 0; }
.thread-item--comment { border-left-color: var(--line); }
.thread-item--proposal { border-left-color: var(--warn); background: var(--warn-bg); }
.thread-item--solved { border-left-color: var(--ok); background: var(--ok-bg); }
.thread-item--reopen { border-left-color: var(--accent); background: var(--accent-bg); }
.thread-item--system { border-left-color: var(--line); background: var(--sunken); font-size: var(--t-sm); }
.thread-item--rejected { border-left-color: var(--danger); background: var(--danger-bg); }

/* ---------- attachments and thumbs ---------- */
.attachments { list-style: none; margin: var(--s-2) 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: var(--s-2); }
.attachments li { display: inline-flex; }
.attachments a {
  display: inline-flex; align-items: center; gap: var(--s-2); min-height: var(--control-h); padding: 0 var(--s-3);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); color: var(--accent); text-decoration: none; font-size: var(--t-sm);
}
.attachments a:hover { background: var(--sunken); text-decoration: underline; }
.attachments .size { color: var(--muted); }
.thumb { display: block; max-width: 12rem; max-height: 9rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--sunken); }
.thumb-link { display: inline-block; margin-top: var(--s-2); }

/* ---------- viewpoint summary ---------- */
.viewpoint {
  background: var(--sunken); border: 1px solid var(--line-soft); border-radius: var(--radius); padding: var(--s-3) var(--s-4);
}
.viewpoint h3 { margin: 0 0 var(--s-2); font-size: var(--t-sm); text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.viewpoint dl { margin: 0; display: grid; grid-template-columns: auto minmax(0, 1fr); gap: var(--s-1) var(--s-4); }
.viewpoint dt { color: var(--muted); }
.viewpoint dd { margin: 0; font-family: var(--mono); font-size: .9em; }

/* ---------- empty state ---------- */
.empty {
  text-align: center; padding: var(--s-6) var(--s-4); color: var(--muted);
  background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius);
}
.empty h2, .empty h3 { margin-top: 0; color: var(--text); }
.empty .actions { justify-content: center; margin-top: var(--s-3); }

/* ---------- filter bar ---------- */
.filter-bar {
  display: flex; flex-wrap: wrap; gap: var(--s-3); align-items: end; margin-bottom: var(--s-4);
  background: var(--surface); border: 1px solid var(--line-soft); border-radius: var(--radius); padding: var(--s-3) var(--s-4);
}
.filter-bar .form-row { margin: 0; flex: 1 1 100%; }
.filter-bar .btn { flex: 1 1 auto; }
@media (min-width: 640px) {
  .filter-bar .form-row { flex: 1 1 10rem; }
  .filter-bar .btn { flex: none; }
}

/* ---------- pagination ---------- */
.pagination { display: flex; flex-wrap: wrap; gap: var(--s-2); align-items: center; justify-content: center; margin: var(--s-5) 0 0; padding: 0; list-style: none; }
.pagination a, .pagination span {
  display: inline-flex; align-items: center; justify-content: center; min-width: var(--control-h); min-height: var(--control-h); padding: 0 var(--s-3);
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); text-decoration: none;
}
.pagination a:hover { background: var(--sunken); }
.pagination [aria-current="page"] { background: var(--accent); border-color: var(--accent); color: var(--on-solid); font-weight: 700; }
.pagination .is-disabled { color: var(--muted); border-style: dashed; }

/* ---------- progress ---------- */
.progress { display: block; width: 100%; height: .75rem; appearance: none; border: 1px solid var(--line); border-radius: 999px; background: var(--sunken); overflow: hidden; }
.progress::-webkit-progress-bar { background: var(--sunken); }
.progress::-webkit-progress-value { background: var(--accent); }
.progress::-moz-progress-bar { background: var(--accent); }
.progress-row { display: flex; align-items: center; gap: var(--s-3); font-size: var(--t-sm); color: var(--muted); }
.progress-row .progress { flex: 1; }

/* ---------- dialog ---------- */
dialog {
  width: min(32rem, calc(100vw - 2 * var(--gutter))); max-height: calc(100vh - 2 * var(--gutter)); overflow: auto;
  padding: var(--s-4); color: var(--text); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 12px 32px rgba(0, 0, 0, .35);
}
dialog::backdrop { background: rgba(8, 12, 18, .6); }
dialog h2 { margin-top: 0; }
dialog .actions { justify-content: flex-end; margin-top: var(--s-4); }

/* ---------- print / motion ---------- */
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
@media print { .site-header, .banner, .actions, .skip-link { display: none; } body { background: #fff; color: #000; } }
