/* HVAC Protocol Tool - mobile first; 3-pane layout on wide screens. */

:root {
  --bg: #eef1f5;
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --text: #17202a;
  --muted: #5b6876;
  --border: #d3dae2;
  --primary: #1565c0;
  --primary-text: #ffffff;
  --primary-soft: #e3effc;
  --ok: #2e7d32;
  --nok: #c62828;
  --warn: #a5560a;
  --warn-bg: #fff4e0;
  --warn-border: #f0c27a;
  --err: #c62828;
  --err-bg: #ffebee;
  --note: #e8650a;
  --note-bg: #fff3e6;
  --sel: #fff59d;
  --sel-border: #e2c300;
  --sel-text: #1f1f1f;
  --calc: #1565c0;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 1px 3px rgba(16, 24, 40, 0.07);
  --appbar-h: 60px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #181c22;
    --surface-2: #20252d;
    --text: #e6e9ee;
    --muted: #9aa5b1;
    --border: #313843;
    --primary: #5aa9f0;
    --primary-text: #08121e;
    --primary-soft: #16283b;
    --ok: #4caf50;
    --nok: #ef5350;
    --warn: #f0b35a;
    --warn-bg: #2b2112;
    --warn-border: #6b4c1e;
    --err: #ff6b6b;
    --err-bg: #3a1618;
    --note: #ff9a3c;
    --note-bg: #33220f;
    --sel: #3d3912;
    --sel-border: #8a7d1c;
    --sel-text: #fff8c4;
    --calc: #7cc0ff;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  min-height: 100vh;
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, h4 { line-height: 1.25; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.loading { padding: 48px 16px; text-align: center; color: #888; }

.icon {
  width: 1.25em;
  height: 1.25em;
  flex-shrink: 0;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* --- App bar ------------------------------------------------------------------ */

.appbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: var(--appbar-h);
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
body.save-error .appbar { border-bottom: 3px solid var(--err); }

.appbar-logo {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--primary);
  color: var(--primary-text);
}
.appbar-title { flex: 1; min-width: 0; }
.appbar-title h1 {
  margin: 0;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.appbar-sub {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}
.appbar-sub.done { color: var(--ok); font-weight: 700; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-left: -6px;
  border: 0;
  border-radius: 50%;
  background: transparent;
}
.icon-btn:active { background: var(--surface-2); }

.save-state {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}
.save-state.saved .icon { color: var(--ok); }
.save-state .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
  animation: pulse 0.8s ease-in-out infinite alternate;
}
.save-state.error { background: var(--err-bg); color: var(--err); font-weight: 700; }
@keyframes pulse { from { opacity: 0.3; } to { opacity: 1; } }
@media (max-width: 420px) {
  .save-state:not(.error) span:last-child { display: none; }
}

.lang-toggle {
  display: inline-flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
}
.lang-toggle button {
  min-width: 40px;
  min-height: 36px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 700;
}
.lang-toggle button.active { background: var(--primary); color: var(--primary-text); }

/* --- Page / buttons ------------------------------------------------------------- */

.page {
  width: 100%;
  max-width: 980px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: calc(40px + env(safe-area-inset-bottom));
}
.page.narrow { max-width: 580px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.big { min-height: 52px; flex: 1 1 180px; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.9rem; }
.btn.danger { background: var(--nok); border-color: var(--nok); color: #fff; }
.btn.danger-text { background: transparent; border-color: transparent; color: var(--nok); margin-right: auto; }
.btn.danger-outline { width: 100%; background: transparent; border-color: var(--nok); color: var(--nok); }

.actions-row { display: flex; flex-wrap: wrap; gap: 10px; margin: 0 0 16px; }
.actions-row.end { justify-content: flex-end; margin: 20px 0 0; }
.actions-col { display: grid; gap: 10px; margin: 0 0 16px; }

.banner {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 12px;
  padding: 12px 14px;
  margin-bottom: 16px;
  border-radius: var(--radius);
}
.banner > .icon { align-self: flex-start; margin-top: 2px; }
.banner-text { flex: 1 1 200px; }
.banner.warn { background: var(--warn-bg); border: 1px solid var(--warn-border); }
.banner.warn > .icon { color: var(--warn); }
.banner.error { background: var(--err-bg); color: var(--err); }
.banner pre { margin: 6px 0 0; white-space: pre-wrap; font-size: 0.8rem; }

/* --- Home ------------------------------------------------------------------------ */

.cards { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
.card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 10px 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: inherit;
  text-decoration: none;
}
.card:active { background: var(--surface-2); }
.card > .icon { color: var(--muted); }
.card-main { flex: 1; min-width: 0; }
.card-title { font-size: 1.15rem; font-weight: 700; }
.card-sub {
  color: var(--muted);
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-side { width: 112px; flex-shrink: 0; }

.chip {
  display: inline-block;
  margin-top: 6px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.chip.warn { background: var(--warn-bg); color: var(--warn); border: 1px solid var(--warn-border); }

.progress { display: flex; align-items: center; gap: 8px; }
.progress-track {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  overflow: hidden;
}
.progress-fill { height: 100%; background: var(--primary); border-radius: 3px; }
.progress.done .progress-fill { background: var(--ok); }
.progress-text {
  min-width: 3.4em;
  text-align: right;
  font-size: 0.8rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.progress.done .progress-text { color: var(--ok); font-weight: 700; }

.empty { padding: 40px 16px; text-align: center; color: var(--muted); }

.page-footer {
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.88rem;
}
.page-footer p { margin: 6px 0; }
.page-footer .actions-row { margin-top: 12px; }
.storage-line { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.storage-line.persistent { color: var(--ok); }
.storage-line.best-effort, .storage-line.unsupported { color: var(--warn); }

/* --- Forms ----------------------------------------------------------------------- */

.card-box {
  margin-bottom: 16px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-box > h2 {
  margin: 0 0 12px;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.field { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; margin-bottom: 14px; }
.field-label { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.field-static { font-weight: 600; }
.form-sub {
  margin: 20px 0 10px;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

input[type="text"], input[type="date"], textarea {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 1.05rem;
}
input:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: var(--primary); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.7; }

.uic-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0 12px; }

.stepper { display: flex; align-items: center; gap: 14px; }
.stepper .btn { width: 52px; padding: 0; font-size: 1.4rem; }
.stepper-value { min-width: 2ch; text-align: center; font-size: 1.35rem; font-weight: 700; }

.num-wrap { display: flex; gap: 6px; min-width: 0; }
.num-wrap input.num { flex: 1; min-width: 0; }
input.num {
  text-align: right;
  font-size: 1.15rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}
input.num.invalid { border-color: var(--warn); background: var(--warn-bg); }
.pm-btn {
  width: 44px;
  flex-shrink: 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: 1.15rem;
  font-weight: 700;
}

/* --- Protocol overview -------------------------------------------------------- */

.protocol-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 0 16px; }
@media (min-width: 820px) {
  .protocol-grid { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); align-items: start; }
}
.carriage h3 { margin: 16px 0 8px; font-size: 0.95rem; }
.carriage:first-child h3 { margin-top: 0; }
.unit-link {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 52px;
  margin-bottom: 8px;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: inherit;
  text-decoration: none;
}
.unit-link:active { background: var(--primary-soft); }
.unit-link .unit-name { width: 5.5em; font-weight: 600; }
.unit-link .progress { flex: 1; }
.unit-link > .icon { color: var(--muted); }

/* --- Unit form ------------------------------------------------------------------- */

.unit-side, .instr-panel { display: none; }
.unit-main { max-width: 780px; }

@media (min-width: 1000px) {
  .unit-layout { display: grid; grid-template-columns: minmax(0, 1fr) 360px; }
  .instr-panel {
    display: block;
    position: sticky;
    top: calc(var(--appbar-h) + 16px);
    align-self: start;
    max-height: calc(100vh - var(--appbar-h) - 32px);
    overflow: auto;
    margin: 16px 16px 16px 0;
    padding: 16px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
  }
}
@media (min-width: 1300px) {
  .unit-layout { grid-template-columns: 250px minmax(0, 1fr) 390px; }
  .unit-side {
    display: block;
    position: sticky;
    top: var(--appbar-h);
    align-self: start;
    max-height: calc(100vh - var(--appbar-h));
    overflow: auto;
    padding: 16px 0 16px 16px;
  }
}
.unit-side h2 { margin: 4px 10px 10px; font-size: 0.8rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.side-link {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  color: inherit;
  font-size: 0.92rem;
  text-decoration: none;
}
.side-link:hover { background: var(--surface-2); }
.side-link.current { background: var(--primary-soft); font-weight: 700; }
.side-count { color: var(--muted); font-variant-numeric: tabular-nums; }
.side-link.done .side-count { color: var(--ok); font-weight: 700; }

.group-title {
  margin: 24px 4px 8px;
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.unit-main > .group-title:first-child { margin-top: 4px; }

.steps { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }

.step {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "label label"
    "control note";
  align-items: center;
  gap: 8px 10px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  scroll-margin: calc(var(--appbar-h) + 16px) 0 160px;
  transition: background-color 0.15s;
}
.step.filled { box-shadow: inset 4px 0 0 var(--ok); }
.step.out { box-shadow: inset 4px 0 0 var(--nok); }
.step.selected { background: var(--sel); border-color: var(--sel-border); color: var(--sel-text); }

@media (min-width: 700px) {
  .step {
    grid-template-columns: minmax(0, 1fr) 15rem auto;
    grid-template-areas: "label control note";
  }
}

.step-label {
  grid-area: label;
  display: flex;
  align-items: flex-start;
  gap: 6px;
  padding: 2px 0;
  border: 0;
  background: none;
  color: inherit;
  font-weight: 600;
  text-align: left;
}
.step-label .icon { width: 1.05em; height: 1.05em; margin-top: 0.15em; color: var(--primary); }
.step.selected .step-label .icon { color: inherit; opacity: 0.7; }

.okn-group { grid-area: control; display: flex; gap: 8px; }
.okn {
  min-width: 76px;
  min-height: 46px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
}
.okn.ok[aria-pressed="true"] { background: var(--ok); border-color: var(--ok); color: #fff; }
.okn.nok[aria-pressed="true"] { background: var(--nok); border-color: var(--nok); color: #fff; }

.meas-control { grid-area: control; display: flex; align-items: center; gap: 8px; min-width: 0; }
.meas-control .num-wrap { flex: 1; max-width: 230px; }
.unit { min-width: 2.4em; color: var(--muted); font-weight: 600; }
.step.selected .unit { color: inherit; opacity: 0.75; }
.step.out input.num { border: 2px solid var(--nok); background: var(--err-bg); color: var(--nok); }

.note-btn {
  grid-area: note;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 46px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
}
.note-btn.has-note { background: var(--note); border-color: var(--note); color: #fff; }
@media (max-width: 360px) { .note-btn span { display: none; } }

.step-msg { grid-column: 1 / -1; color: var(--nok); font-size: 0.85rem; font-weight: 600; }
.step-msg:empty { display: none; }

.note-preview {
  grid-column: 1 / -1;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  padding: 6px 10px;
  border: 0;
  border-left: 3px solid var(--note);
  border-radius: 6px;
  background: var(--note-bg);
  color: var(--text);
  font-size: 0.9rem;
  text-align: left;
  white-space: pre-wrap;
}

.calc {
  padding: 4px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.calc .muted { margin: 10px 0; font-style: italic; }
.calc-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto 2.4em;
  gap: 10px;
  padding: 9px 0;
  border-bottom: 1px dashed var(--border);
}
.calc-row:last-child { border-bottom: 0; }
.calc-val {
  font-family: ui-monospace, Consolas, "Roboto Mono", monospace;
  font-weight: 700;
  color: var(--calc);
  text-align: right;
}

.unit-nav { display: flex; justify-content: space-between; gap: 10px; margin-top: 24px; }
.unit-nav .btn { max-width: 49%; min-width: 0; }
.unit-nav .btn span { overflow: hidden; text-overflow: ellipsis; }

/* --- Instructions -------------------------------------------------------------- */

.instr-panel > h2 { margin: 0 0 4px; font-size: 0.8rem; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.instr-step { margin-bottom: 10px; font-weight: 700; color: var(--primary); }
.instr-summary { margin: 0 0 8px; font-style: italic; color: var(--muted); }
.instr-body h4, .dlg-body h4 { margin: 14px 0 4px; font-size: 0.92rem; color: var(--primary); }
.instr-body ul, .instr-body ol, .sheet ul, .sheet ol { margin: 0; padding-left: 1.4em; }
.instr-body li, .sheet li { margin: 4px 0; white-space: pre-line; }
.instr-safety {
  margin-top: 14px;
  padding: 10px 12px;
  border-left: 4px solid var(--nok);
  border-radius: 6px;
  background: var(--err-bg);
}
.instr-safety strong { color: var(--nok); }

/* --- Dialogs --------------------------------------------------------------------- */

dialog.dlg {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
}
dialog.dlg[open] { display: flex; flex-direction: column; }
dialog.dlg::backdrop { background: rgba(0, 0, 0, 0.45); }
.dlg-head h2 { margin: 0; padding: 16px 18px 6px; font-size: 1.1rem; }
.dlg-body { flex: 1; overflow: auto; padding: 4px 18px 14px; }
.dlg-body > p:first-child { margin-top: 6px; }
.dlg-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 18px calc(14px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
}

@media (max-width: 700px) {
  dialog.sheet {
    width: 100%;
    max-width: 100%;
    max-height: 88dvh;
    margin: auto 0 0;
    border-radius: 18px 18px 0 0;
  }
  dialog.note-dlg { margin-top: 12px; }
}

.note-text { min-height: 150px; resize: vertical; line-height: 1.4; }

.check-list { display: grid; grid-template-columns: minmax(0, 1fr); margin-top: 8px; }
.check-line {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 48px;
  padding: 6px 2px;
  border-bottom: 1px solid var(--border);
}
.check-line input { width: 22px; height: 22px; accent-color: var(--primary); }
.check-line span:nth-of-type(1) { flex: 1; }

details summary { margin: 8px 0; color: var(--primary); font-weight: 600; cursor: pointer; }
.missing-unit { margin: 8px 0; }
.missing-unit ul { margin: 4px 0; padding-left: 1.3em; color: var(--muted); }

table.cmp { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
table.cmp th, table.cmp td { padding: 6px 4px; text-align: left; border-bottom: 1px solid var(--border); }

.file-name {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface-2);
  font-weight: 600;
  word-break: break-all;
}
.warn-text { display: flex; align-items: center; gap: 6px; color: var(--warn); font-weight: 600; }

/* --- Toast / busy / update ------------------------------------------------------ */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 1000;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 10px;
  background: #222831;
  color: #fff;
  font-weight: 600;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--nok); }

.busy {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  background: rgba(0, 0, 0, 0.35);
}
.busy-box {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 20px 24px;
  border-radius: 14px;
  background: var(--surface);
  font-weight: 600;
}
.spinner {
  width: 22px;
  height: 22px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.update-banner {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 560px;
  margin: 0 auto;
  padding: 10px 12px 10px 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--primary-soft);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  font-weight: 600;
}
