/* ============================================================
   CGA / Linearity Audit Tracker — styles
   Touch-first layout for phone & tablet, with print stylesheet.
   ============================================================ */

:root {
  --navy: #0f3d5c;
  --navy-700: #0c324c;
  --accent: #1d7fb8;
  --accent-700: #166395;
  --ok: #1f8a4c;
  --danger: #c0392b;
  --bg: #eef1f4;
  --surface: #ffffff;
  --line: #d4dbe2;
  --text: #1c2630;
  --muted: #66727e;
  --radius: 14px;
  --tap: 48px;            /* minimum touch target */
  --shadow: 0 2px 10px rgba(15, 61, 92, .08);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

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

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 16px;
  line-height: 1.4;
  -webkit-tap-highlight-color: transparent;
}

h1, h2, h3, p { margin: 0; }
.muted { color: var(--muted); }

/* ---------- App bar ---------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(var(--safe-top) + 8px) 10px 8px;
  background: var(--navy);
  color: #fff;
  box-shadow: 0 1px 6px rgba(0, 0, 0, .18);
}
.appbar__title {
  font-size: 1.15rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.appbar__spacer { flex: 1; }

.iconbtn {
  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: #fff;
  border-radius: 50%;
  cursor: pointer;
}
.iconbtn:active { background: rgba(255, 255, 255, .18); }
.iconbtn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Main / views ---------- */
#main {
  max-width: 820px;
  margin: 0 auto;
  padding: 14px 14px calc(120px + var(--safe-bottom));
}
.view { animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* ---------- Search & filters ---------- */
.search {
  position: relative;
  margin-bottom: 12px;
}
.search__icon {
  position: absolute;
  left: 14px; top: 50%;
  transform: translateY(-50%);
  width: 20px; height: 20px;
  fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round;
}
.search input {
  width: 100%;
  height: var(--tap);
  padding: 0 16px 0 44px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  font-size: 16px;
}

.filter { display: flex; gap: 8px; margin-bottom: 14px; }
.chip {
  flex: 1;
  min-height: 42px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  font-size: .95rem;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.chip.is-active { background: var(--navy); border-color: var(--navy); color: #fff; }

/* ---------- Record list ---------- */
.list { display: flex; flex-direction: column; gap: 10px; }
.rec {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.rec:active { transform: scale(.99); }
.rec__badge {
  flex: 0 0 auto;
  width: 46px; height: 46px;
  border-radius: 12px;
  display: grid; place-items: center;
  font-weight: 700; font-size: .72rem; color: #fff; letter-spacing: .5px;
}
.badge--cga { background: var(--accent); }
.badge--lin { background: #7a5cc0; }
.rec__body { flex: 1; min-width: 0; }
.rec__title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec__meta { font-size: .85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rec__chev { width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: 0 0 auto; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 48px 16px;
}
.empty svg { width: 56px; height: 56px; fill: none; stroke: var(--line); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; margin-bottom: 12px; }
.empty p { margin: 4px 0; }

/* ---------- Editor ---------- */
.editor { display: flex; flex-direction: column; gap: 14px; }
.sheet-sub { color: var(--muted); font-size: .9rem; text-align: center; }
.formula {
  text-align: center;
  font-style: italic;
  background: #e8f1f8;
  border: 1px dashed var(--accent);
  color: var(--accent-700);
  padding: 8px;
  border-radius: 10px;
  font-size: .95rem;
}

.card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 14px;
  margin: 0;
}
.card > legend {
  font-weight: 700;
  color: var(--navy);
  padding: 0 6px;
  font-size: 1rem;
}
.card__head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin: -4px 0 10px;
}
.card__head h3 { color: var(--navy); font-size: 1rem; }

.grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}
@media (min-width: 560px) {
  .grid { grid-template-columns: 1fr 1fr; }
  .grid .grid--full { grid-column: 1 / -1; }
}

label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: .82rem;
  font-weight: 600;
  color: var(--muted);
}
input, select {
  height: var(--tap);
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  font-size: 16px;            /* >=16px prevents iOS zoom-on-focus */
  color: var(--text);
  width: 100%;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 127, 184, .18);
}
input[readonly] {
  background: #f1f4f7;
  color: var(--navy-700);
  font-weight: 700;
  border-style: dashed;
}

/* ---------- Reading tables (CGA & Linearity) ---------- */
.readings {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}
.readings__row {
  display: grid;
  gap: 8px;
  align-items: end;
}
.cga-row { grid-template-columns: 70px 1fr 1fr 1fr; }
.cga-row .rowlabel { align-self: center; }
.rowlabel { font-weight: 700; color: var(--navy); font-size: .85rem; padding-bottom: 12px; }
.colhead {
  font-size: .72rem; font-weight: 700; color: var(--muted);
  text-transform: uppercase; letter-spacing: .3px;
}

.result {
  display: flex; flex-wrap: wrap; gap: 10px;
  margin-top: 12px; padding-top: 12px;
  border-top: 1px dashed var(--line);
}
.result .stat {
  flex: 1; min-width: 130px;
  background: #f1f4f7; border-radius: 10px; padding: 8px 12px;
}
.result .stat small { display: block; font-size: .72rem; color: var(--muted); font-weight: 700; text-transform: uppercase; }
.result .stat b { font-size: 1.25rem; color: var(--navy); }
.stat.pass b { color: var(--ok); }
.stat.fail b { color: var(--danger); }

.cycle {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin-top: 8px;
}
.cycle__title { font-weight: 700; color: var(--navy); font-size: .85rem; margin-bottom: 8px; }
.cycle .grid3 {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

/* ---------- Buttons ---------- */
.btn {
  min-height: var(--tap);
  padding: 0 18px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn:active { transform: scale(.98); }
.btn--primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn--ghost { background: transparent; }
.btn--block { width: 100%; margin-bottom: 10px; }
#deleteBtn { color: var(--danger); }

/* ---------- FAB ---------- */
.fab {
  position: fixed;
  right: 18px;
  bottom: calc(20px + var(--safe-bottom));
  z-index: 25;
  height: 56px;
  padding: 0 22px 0 18px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 1.05rem;
  font-weight: 700;
  display: inline-flex; align-items: center; gap: 8px;
  box-shadow: 0 6px 18px rgba(29, 127, 184, .4);
  cursor: pointer;
}
.fab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; }
.fab:active { transform: scale(.96); }

/* ---------- Sticky action bar ---------- */
.actionbar {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  z-index: 25;
  display: flex;
  gap: 10px;
  padding: 10px 14px calc(10px + var(--safe-bottom));
  background: rgba(255, 255, 255, .96);
  backdrop-filter: blur(6px);
  border-top: 1px solid var(--line);
}
.actionbar .btn { flex: 1; }
.actionbar #saveBtn { flex: 1.4; }

/* ---------- Modal / bottom sheet ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
/* The `hidden` attribute must always win — several components set `display`
   (FAB, action bar, sheets, icon buttons), which would otherwise stay visible
   when JS toggles `hidden` (e.g. the + FAB showing behind the action bar). */
[hidden] { display: none !important; }
.modal__backdrop { position: absolute; inset: 0; background: rgba(8, 22, 33, .45); }
.modal__panel {
  position: relative;
  width: 100%;
  max-width: 520px;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 18px 18px calc(18px + var(--safe-bottom));
  box-shadow: 0 -6px 30px rgba(0, 0, 0, .25);
  animation: slideup .22s ease;
}
@media (min-width: 560px) {
  .modal { align-items: center; }
  .modal__panel { border-radius: 20px; }
}
@keyframes slideup { from { transform: translateY(100%); } to { transform: none; } }
.modal__title { font-size: 1.2rem; color: var(--navy); margin-bottom: 12px; }
.modal__note { margin-bottom: 14px; font-size: .9rem; }
.modal__cancel { width: 100%; margin-top: 6px; }

/* ---------- Managed dropdowns (inline add-new) ---------- */
.dd__new { display: flex; gap: 6px; margin-top: 6px; }
.dd__new input { flex: 1; min-width: 0; }
.dd__new .btn { flex: 0 0 auto; width: auto; padding: 0 14px; }
.btn--danger { background: #c0392b; border-color: #c0392b; color: #fff; }

/* ---------- Configure page lists ---------- */
.cfg-list { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.cfg-item { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.cfg-item__main { flex: 1 1 auto; min-width: 0; }
.cfg-item__main--edit { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 0; font: inherit; cursor: pointer; -webkit-tap-highlight-color: rgba(15,61,92,.08); }
.cfg-item__main--edit:active { opacity: .55; }
.cfg-item__title { font-weight: 600; color: var(--text); }
.cfg-item__sub { font-size: .85rem; color: var(--muted); }
.cfg-item__del { flex: 0 0 auto; width: 30px; height: 30px; border: 0; border-radius: 8px; background: #f1f4f7; color: #c0392b; font-size: 1.25rem; line-height: 1; cursor: pointer; }
.cfg-subhead { font-weight: 600; color: var(--text); margin: 18px 0 8px; }
.cfg-tag { display: inline-block; font-size: .68rem; font-weight: 700; letter-spacing: .02em; padding: 1px 7px; border-radius: 999px; background: #e7eef5; color: #0f3d5c; margin-left: 8px; vertical-align: middle; }

/* ---------- List manager (add / remove dropdown entries) ---------- */
.listmgr { display: flex; flex-direction: column; gap: 8px; max-height: 44vh; overflow-y: auto; margin-bottom: 14px; }
.listmgr__item { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.listmgr__item span { font-size: .95rem; color: var(--text); word-break: break-word; }
.listmgr__del { flex: 0 0 auto; width: 30px; height: 30px; border: 0; border-radius: 8px; background: #f1f4f7; color: #c0392b; font-size: 1.25rem; line-height: 1; cursor: pointer; }
.listmgr__add { display: flex; gap: 8px; margin-bottom: 12px; }
.listmgr__add input { flex: 1; }
.listmgr__add .btn { flex: 0 0 auto; width: auto; }

.bigchoice {
  display: flex; align-items: center; gap: 14px;
  width: 100%;
  text-align: left;
  padding: 14px;
  margin-bottom: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
}
.bigchoice:active { background: #f1f4f7; }
.bigchoice__badge {
  flex: 0 0 auto;
  width: 52px; height: 52px;
  border-radius: 12px;
  display: grid; place-items: center;
  color: #fff; font-weight: 700; font-size: .8rem;
}
.bigchoice strong { display: block; font-size: 1.05rem; color: var(--text); }
.bigchoice small { color: var(--muted); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 60;
  background: var(--navy-700);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: .92rem;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .3);
  animation: fade .2s ease;
}

/* ============================================================
   PRINT — clean report layout
   ============================================================ */
#printRoot { display: none; }

@media print {
  body { background: #fff; }
  .appbar, .fab, .actionbar, .modal, .toast, #main, .search, .filter { display: none !important; }

  #printRoot {
    display: block;
    color: #000;
    font-size: 11pt;
    padding: 14mm;   /* page margin (since @page margin is 0 to kill the browser header/footer) */
    /* keep cell shading + borders even when the browser's "Background graphics" is off,
       so the table boxes/lines always print solidly */
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  /* margin:0 removes the browser's print header/footer (URL, title, date, page #);
     the real page margin is supplied by #printRoot padding below */
  @page { margin: 0; }

  .p-sheet { page-break-after: always; }
  .p-sheet:last-child { page-break-after: auto; }

  .p-head { text-align: center; margin-bottom: 6mm; }
  .p-head h1 { font-size: 15pt; margin: 0; text-transform: uppercase; letter-spacing: .5px; }
  .p-head h2 { font-size: 12pt; margin: 2px 0 0; font-weight: normal; }
  .p-head .p-ref { font-size: 9pt; color: #333; }

  .p-info {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 5mm;
  }
  .p-info td { border: 1px solid #000; padding: 4px 6px; font-size: 10pt; vertical-align: top; }
  .p-info .k { font-weight: bold; background: #eee; width: 24%; white-space: nowrap; }

  .p-table { width: 100%; border-collapse: collapse; margin-bottom: 4mm; }
  .p-table th, .p-table td { border: 1px solid #000; padding: 4px 6px; font-size: 9.5pt; text-align: center; }
  .p-table th { background: #e4e4e4; }
  .p-table .lbl { text-align: left; font-weight: bold; background: #f4f4f4; }
  .p-table .calc { font-weight: bold; }

  .p-section { font-weight: bold; font-size: 11pt; margin: 4mm 0 2mm; border-bottom: 2px solid #000; padding-bottom: 1mm; }

  .p-sign { margin-top: 8mm; display: flex; gap: 12mm; }
  .p-sign div { flex: 1; border-top: 1px solid #000; padding-top: 2px; font-size: 9pt; }

  .p-fail { color: #000; font-weight: bold; }
  .p-note { font-size: 8.5pt; color: #333; font-style: italic; }
}
