/* sitdef viewer — no framework, no build step. */

:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1b1f24;
  --muted: #6b7280;
  --line: #d9dde3;
  --accent: #2f6fb0;
  --accent-ink: #ffffff;
  --zero-bg: #eef2f6;
  --group-a: #ffffff;
  --group-b: #fafbfc;
  --warn-bg: #fdf3d7;
  --warn-ink: #7a5c00;
  --alarm-bg: #fbdcd8;
  --alarm-ink: #8f2517;
  --danger: #a8382a;
  --radius: 8px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.warn { color: var(--warn-ink); background: var(--warn-bg); padding: .4rem .6rem; border-radius: 6px; }
.spacer { flex: 1; }
.sep { width: 1px; align-self: stretch; background: var(--line); }

/* --- controls ----------------------------------------------------------- */

button {
  font: inherit;
  padding: .42rem .8rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
button:hover { border-color: #b9c0c9; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { filter: brightness(1.07); }
button.ghost { background: transparent; }
button.small { padding: .2rem .5rem; font-size: .85em; }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: not-allowed; }

select, input[type="email"], input[type="password"] {
  font: inherit;
  padding: .35rem .5rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
}

label { display: inline-flex; align-items: center; gap: .4rem; }
label.check { gap: .35rem; }
label > span { color: var(--muted); white-space: nowrap; }

.lang-row { display: inline-flex; gap: .25rem; }
button.lang { padding: .2rem .45rem; font-size: .8em; color: var(--muted); }
button.lang.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* --- login -------------------------------------------------------------- */

.login { display: grid; place-items: center; min-height: 100vh; padding: 1rem; }
.login-card {
  display: flex; flex-direction: column; gap: .85rem;
  width: min(380px, 100%);
  background: var(--panel);
  padding: 2rem;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.login-card h1 { margin: 0; font-size: 1.35rem; }
.login-card p { margin: 0; }
.login-card label { flex-direction: column; align-items: stretch; gap: .25rem; }
.login-card input { width: 100%; }

/* --- chrome ------------------------------------------------------------- */

.topbar, .toolbar {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  padding: .6rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar { position: sticky; top: 0; z-index: 30; }
.brand { display: flex; align-items: baseline; gap: .5rem; }
.selectors { display: flex; gap: .75rem; flex-wrap: wrap; }

main { padding: 1rem; display: flex; flex-direction: column; gap: 1rem; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .6rem .9rem;
  border-bottom: 1px solid var(--line);
}
.panel-head h2 { margin: 0; font-size: .95rem; font-weight: 600; }

.empty { padding: 2rem; text-align: center; color: var(--muted); }

/* --- the table ---------------------------------------------------------- */

.table-wrap { overflow: auto; max-height: 62vh; }

table.deformation {
  border-collapse: separate;
  border-spacing: 0;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

table.deformation th, table.deformation td {
  padding: .3rem .55rem;
  border-bottom: 1px solid var(--line);
  border-right: 1px solid var(--line);
  text-align: right;
}

table.deformation thead th {
  position: sticky;
  background: var(--zero-bg);
  font-weight: 600;
  text-align: center;
  z-index: 2;
}
table.deformation thead tr:first-child th { top: 0; }
table.deformation thead tr:nth-child(2) th { top: 3.15rem; }

/* The point column stays put while epochs scroll away to the right. */
.point-col {
  position: sticky; left: 0; z-index: 3;
  background: var(--panel);
  text-align: left !important;
  font-weight: 600;
  border-right: 2px solid var(--line) !important;
}
table.deformation thead .point-col { z-index: 4; background: var(--zero-bg); }

td.num { font-family: var(--mono); font-size: .92em; }

/* The Δ columns are too narrow for "SOLL − IST", so the formula sits under the
   epoch name in the group header where it is always visible, including in the
   printed PDF. */
.group-note {
  display: block;
  font-weight: 400;
  font-size: .78em;
  color: var(--muted);
  letter-spacing: .02em;
}
.convention {
  font-size: .85em;
  color: var(--muted);
  font-family: var(--mono);
}
.e0 { background: var(--group-a); }
.e1 { background: var(--group-b); }
.zero-group { background: var(--zero-bg); }

/* A missing measurement is blank with a subtle hatch, so it reads as "no
   data" rather than as an empty-looking zero. */
td.missing, td.no_datum {
  background-image: repeating-linear-gradient(
    45deg, transparent, transparent 4px, rgba(0, 0, 0, .05) 4px, rgba(0, 0, 0, .05) 5px
  );
}

td.band-warn { background: var(--warn-bg); color: var(--warn-ink); font-weight: 600; }
td.band-alarm { background: var(--alarm-bg); color: var(--alarm-ink); font-weight: 700; }

tbody tr:hover td { filter: brightness(.97); }
tbody tr.selected .point-col { box-shadow: inset 3px 0 0 var(--accent); }
tbody tr.selected td { background: #eaf1f9; }
tbody tr { cursor: pointer; }

/* --- chart -------------------------------------------------------------- */

.chart-panel #chart-wrap { padding: .75rem .9rem 1rem; }
svg.chart { width: 100%; height: auto; display: block; }
svg.chart .grid { stroke: var(--line); stroke-width: 1; }
svg.chart .grid-zero { stroke: #9aa3ae; stroke-dasharray: 3 3; }
svg.chart .tick { font-size: 11px; fill: var(--muted); }
svg.chart .crosshair { stroke: var(--accent); stroke-width: 1; stroke-dasharray: 3 3; }
svg.chart .dot:hover { r: 5; }
svg.chart .chart-axis-label { font-size: 11px; fill: var(--muted); }
.chart-readout { min-height: 1.4em; font-family: var(--mono); font-size: .85em; color: var(--muted); }
.chart-legend { display: flex; flex-wrap: wrap; gap: .75rem; list-style: none; margin: .5rem 0 0; padding: 0; font-size: .85em; }
.chart-legend li { display: flex; align-items: center; gap: .35rem; }
.swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }

/* --- epochs ------------------------------------------------------------- */

#epoch-list { padding: .4rem .9rem .9rem; display: flex; flex-direction: column; }
.epoch-row {
  display: flex; align-items: center; gap: .75rem;
  padding: .4rem 0;
  border-bottom: 1px solid var(--line);
}
.epoch-row:last-child { border-bottom: none; }
.epoch-label { font-weight: 600; min-width: 9rem; }
.epoch-row .muted { flex: 1; font-size: .9em; }
.badge {
  background: var(--accent); color: #fff;
  padding: .1rem .45rem; border-radius: 999px; font-size: .75em;
}

/* --- dialog ------------------------------------------------------------- */

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  width: min(560px, 92vw);
  color: var(--ink);
  background: var(--panel);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dialog-body { padding: 1.2rem; display: flex; flex-direction: column; gap: .8rem; }
.dialog-body h2 { margin: 0; font-size: 1.1rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin: 0; padding: 0; }

/* --- platform admin ------------------------------------------------------ */

.admin-main { max-width: 960px; margin: 0 auto; width: 100%; }

table.data-table { width: 100%; border-collapse: collapse; }
table.data-table th, table.data-table td {
  padding: .5rem .7rem;
  border-bottom: 1px solid var(--line);
  text-align: left;
}
table.data-table th { color: var(--muted); font-weight: 600; font-size: .85em; }
table.data-table tbody tr:hover { background: var(--group-b); }
table.data-table td.mono { font-family: var(--mono); font-size: .9em; }

.device-actions { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.device-actions select.device-move { max-width: 12rem; }

/* The customer name doubles as the way into that customer's own view. */
button.link {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}
button.link:hover { text-decoration: none; }

.client-head {
  display: flex;
  align-items: baseline;
  gap: .7rem;
  flex-wrap: wrap;
  margin-bottom: .2rem;
}
.client-head h1 { margin: 0; font-size: 1.3rem; }
.client-head .mono { font-family: var(--mono); font-size: .9em; }
.admin-main .small { font-size: .85em; }

.token-value {
  font-family: var(--mono);
  font-size: 1.05rem;
  background: var(--zero-bg);
  padding: .8rem 1rem;
  border-radius: 6px;
  word-break: break-all;
  white-space: pre-wrap;
  user-select: all;
}

/* --- toast -------------------------------------------------------------- */

.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%);
  background: #21262d; color: #fff;
  padding: .55rem 1rem; border-radius: 999px;
  z-index: 100; font-size: .9em;
}
.toast.error { background: var(--danger); }

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171b;
    --panel: #1b1f24;
    --ink: #e6e9ed;
    --muted: #97a1ad;
    --line: #2e343c;
    --zero-bg: #232930;
    --group-a: #1b1f24;
    --group-b: #1f242a;
    --warn-bg: #4a3d12;
    --warn-ink: #f3d98a;
    --alarm-bg: #4d201a;
    --alarm-ink: #f5b3a8;
    --danger: #e07a6c;
  }
  button, select, input { background: #232930; color: var(--ink); }
  button.primary { background: var(--accent); }
  tbody tr.selected td { background: #23303d; }
  td.missing, td.no_datum {
    background-image: repeating-linear-gradient(
      45deg, transparent, transparent 4px, rgba(255, 255, 255, .06) 4px, rgba(255, 255, 255, .06) 5px
    );
  }
}

/* --- print / PDF -------------------------------------------------------- */

.print-header { display: none; }

@media print {
  /* Landscape A4 fits roughly six epoch groups across; beyond that the table
     scales down rather than clipping, because a clipped column is a silently
     missing measurement on a signed document. */
  @page { size: A4 landscape; margin: 12mm; }

  /* Paper is white whatever the screen was: the dark palette below is a
     screen preference, and a printer that honours it produces pale grey text
     on white — or nothing at all. */
  :root {
    --bg: #f4f5f7;
    --panel: #ffffff;
    --ink: #1b1f24;
    --muted: #6b7280;
    --line: #d9dde3;
    --zero-bg: #eef2f6;
    --group-a: #ffffff;
    --group-b: #fafbfc;
    --warn-bg: #fdf3d7;
    --warn-ink: #7a5c00;
    --alarm-bg: #fbdcd8;
    --alarm-ink: #8f2517;
  }

  body { background: #fff; }
  .topbar, .toolbar, .epochs-panel, .no-print, .toast, dialog { display: none !important; }
  main { padding: 0; gap: .6rem; }
  .panel { border: none; border-radius: 0; overflow: visible; }
  .panel-head { display: none; }
  .print-header { display: block; margin-bottom: .5rem; }
  .print-header h1 { margin: 0 0 .2rem; font-size: 14pt; }
  .print-header p { margin: 0; font-size: 8pt; }

  .table-wrap { overflow: visible; max-height: none; }
  table.deformation { font-size: 7.5pt; width: 100%; }
  table.deformation th, table.deformation td { padding: 1.5px 3px; }

  /* Repeat the header on every printed page. */
  table.deformation thead { display: table-header-group; }
  table.deformation tr { break-inside: avoid; }

  .point-col, table.deformation thead th { position: static; }
  td.band-warn, td.band-alarm, .zero-group, .e1 { -webkit-print-color-adjust: exact; print-color-adjust: exact; }

  .chart-panel { break-inside: avoid; page-break-inside: avoid; }
  .chart-readout, .chart-legend { font-size: 7pt; }
  svg.chart { max-height: 120mm; }
  /* Which line is which point is carried by colour alone, so the series and
     their legend swatches have to print even with backgrounds switched off. */
  svg.chart, svg.chart *, .chart-legend .swatch {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  /* A swatch is a coloured box and nothing else; give it an outline so it is
     still a swatch on a printer that drops the fill anyway. */
  .chart-legend .swatch { border: 1px solid rgba(0, 0, 0, .35); }
}
