/* ============================================================
   PTA Audiogram Generator — styles
   Screen = two-pane workspace (control panel + A4 preview).
   Print  = only the .sheet, rendered as a clean A4 page.
   ============================================================ */

:root {
  /* dralwrdi.online identity v2.0 — "Digital Cochlea" */
  --bg: #F7FBFB;
  --panel-bg: #ffffff;
  --ink: #0A2533;
  --muted: #64798A;
  --line: #D8E5E8;
  --accent: #12A5A5;
  --accent-ink: #ffffff;
  --aqua: #E9F6F6;
  --right: #d32f2f; /* right ear — clinical standard, do not change */
  --left:  #1565c0; /* left ear  — clinical standard, do not change */
  --radius: 12px;
  --shadow: 0 1px 3px rgba(10, 37, 51, .07), 0 10px 28px rgba(10, 37, 51, .09);
  --font: "IBM Plex Sans", "IBM Plex Sans Arabic", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
}

body {
  display: grid;
  grid-template-columns: 360px 1fr;
  min-height: 100vh;
}

/* ---------------- Control panel ---------------- */
.panel {
  background: var(--panel-bg);
  border-right: 1px solid var(--line);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  overflow-y: auto;
}

.panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.panel__head h1 { font-size: 18px; margin: 0; letter-spacing: -.01em; }

.field { display: flex; flex-direction: column; gap: 5px; font-weight: 600; }
.field > span { font-size: 12px; color: var(--muted); }
.field > span em { font-weight: 400; font-style: normal; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

input[type="text"], input[type="date"], textarea, select {
  font: inherit;
  font-weight: 500;
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #F4FAFA;
  color: var(--ink);
  width: 100%;
}
/* iOS Safari gives date inputs an intrinsic width that ignores `width: 100%`,
   so the field overflows its column and the value renders centred; under an RTL
   device UI it also bidi-reorders to "252026/07/". The appearance reset makes it
   size like every other input, and `direction: ltr` pins the segment order. */
input[type="date"] {
  -webkit-appearance: none;
  appearance: none;
  min-width: 0;
  max-width: 100%;
  direction: ltr;
  text-align: left;
}
input[type="date"]::-webkit-date-and-time-value { text-align: left; }

input:focus, textarea:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(18, 165, 165, .18);
}
textarea { resize: vertical; font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 13px; }

.hint { font-size: 11.5px; color: var(--muted); margin: -4px 0 0; line-height: 1.5; }
.hint code { background: #E9F6F6; padding: 1px 5px; border-radius: 4px; font-size: 11px; }

.linklike {
  background: none; border: none; color: var(--accent);
  font: inherit; font-weight: 600; cursor: pointer; padding: 0;
}
.linklike:hover { text-decoration: underline; }

/* `.field`/`.check` set display:flex, which otherwise overrides the [hidden]
   attribute — force hidden to win so progressive-disclosure rows stay collapsed. */
.field[hidden], .check[hidden] { display: none !important; }

.check { display: flex; align-items: flex-start; gap: 8px; font-size: 12.5px; color: var(--muted); font-weight: 600; cursor: pointer; }
.check input { width: auto; margin: 2px 0 0; flex: none; }
.check em { font-weight: 400; font-style: normal; color: var(--muted); }

.panel__actions { display: flex; flex-direction: column; gap: 8px; margin-top: auto; padding-top: 8px; }

.btn {
  font: inherit; font-weight: 600;
  padding: 10px 14px; border-radius: 8px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  cursor: pointer; transition: background .12s, border-color .12s;
}
.btn:hover { background: #F1F8F8; }
.btn--primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn--primary:hover { background: #0E8C8C; }
.btn--ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn--ghost:hover { background: #E9F6F6; }

.parse-status { font-size: 12px; border-radius: 8px; padding: 8px 10px; }
.parse-status.ok   { background: #E4F7EE; color: #128A5D; }
.parse-status.note { background: #FDF3E2; color: #9A6516; }
.parse-status.err  { background: #fdecec; color: #b3261e; }
.parse-status ul { margin: 4px 0 0; padding-left: 18px; }

/* ---------------- Stage + A4 sheet ---------------- */
/* min-width:0 lets this grid track shrink below the A4 sheet's min-content width
   instead of pushing the page sideways — fitSheet() then scales the preview. */
.stage { padding: 28px; display: flex; flex-direction: column; align-items: center; gap: 8px; min-width: 0; }

/* Zoom toggle: shown by fitSheet() only when the preview doesn't fit 1:1. */
.stage__bar { display: none; width: 100%; justify-content: flex-end; }
.stage--scalable .stage__bar { display: flex; }
/* Actual-size mode: the sheet is wider than the viewport, so pan it here rather
   than letting it widen the page. */
.stage--actual { align-items: flex-start; overflow-x: auto; }

/* Scaling wrapper: app.js (fitSheet) scales .sheet down to the stage width on
   narrow screens and sets this box to the resulting size, so the shrunk sheet
   still occupies the right amount of layout space. */
.sheet-wrap { transform-origin: top left; }
.sheet-wrap > .sheet { transform-origin: top left; }

.sheet {
  background: #fff;
  width: 210mm;
  min-height: 297mm;
  padding: 14mm 14mm 12mm;
  box-shadow: var(--shadow);
  color: #111;
  display: flex;
  flex-direction: column;
}

/* Header */
.ag-head { display: flex; align-items: center; gap: 14px; border-bottom: 2px solid #222; padding-bottom: 10px; }
.ag-head__logo { height: 54px; width: auto; max-width: 120px; object-fit: contain; }
.ag-head__titles { flex: 1; }
.ag-head__hospital { font-size: 20px; font-weight: 800; letter-spacing: -.01em; }
.ag-head__dept { font-size: 12.5px; color: #444; font-weight: 600; }
.ag-head__addr { font-size: 11px; color: #666; }
.ag-head__doc { text-align: right; }
.ag-head__doc h2 { margin: 0; font-size: 15px; letter-spacing: .12em; text-transform: uppercase; color: #333; }

/* Patient meta */
.ag-meta { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px 18px; margin: 12px 0 6px; }
.ag-meta__item { border-bottom: 1px solid #ccc; padding: 4px 2px; font-size: 13px; }
.ag-meta__item b { color: #555; font-weight: 700; font-size: 10.5px; text-transform: uppercase; letter-spacing: .05em; display: block; }

/* Arabic report: RTL text + Arabic-capable font stack. */
.sheet--ar {
  font-family: "Geeza Pro", "Al Bayan", "Noto Sans Arabic", "Segoe UI", Tahoma, var(--font);
}
/* SVG axis labels inherit the sheet font (so Arabic captions render correctly). */
.sheet svg text { font-family: inherit; }

/* Charts — always standard clinical orientation (frequency low→high, LTR),
   even inside an RTL report. */
.ag-charts { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px; direction: ltr; }
/* Arabic report: place the panels right-to-left so the Right ear sits on the
   right and the Left ear on the left. Only the panel order flips — each chart
   <svg> keeps direction="ltr", so the audiogram itself never mirrors. */
.sheet--ar .ag-charts { direction: rtl; }
.ag-chart { border: 1px solid #ddd; border-radius: 6px; padding: 6px 4px 2px; }
.ag-chart__title { text-align: center; font-weight: 800; font-size: 13px; padding: 2px 0 4px; }
.ag-chart__title.right { color: var(--right); }
.ag-chart__title.left  { color: var(--left);  }
.ag-chart svg { display: block; width: 100%; height: auto; }

/* Summary + legend */
.ag-lower { display: grid; grid-template-columns: 1.1fr 1fr; gap: 14px; margin-top: 12px; }
.ag-summary table { width: 100%; border-collapse: collapse; font-size: 12px; }
.ag-summary th, .ag-summary td { border: 1px solid #d0d0d0; padding: 4px 7px; text-align: center; }
.ag-summary th { background: #f4f5f8; font-size: 11px; }
.ag-summary caption { text-align: left; font-weight: 800; font-size: 12px; padding-bottom: 4px; }

/* Pre-op PTA row is muted to echo the lighter overlay on the chart. */
.ag-summary tfoot .pta-pre th, .ag-summary tfoot .pta-pre td { color: #8a8a8a; font-weight: 600; }

.ag-legend { font-size: 11.5px; }
.ag-legend__note { margin: 5px 0 0; color: #777; font-size: 11px; }
.ag-legend h3 { margin: 0 0 5px; font-size: 12px; }
.ag-legend ul { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 3px 10px; }
.ag-legend li { display: flex; align-items: center; gap: 7px; }
.ag-legend svg { flex: none; }

/* Interpretation summary carried on the ear header lines ("Rt: <text>" / "Lt: <text>") */
.ag-interpretation { margin-top: 12px; font-size: 12px; }
.ag-interpretation h3 { margin: 0 0 4px; font-size: 12px; }
.ag-interpretation p { margin: 2px 0; }
.ag-interpretation p b { color: #555; }

.ag-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid #ccc; font-size: 11px; color: #555; display: flex; justify-content: space-between; gap: 12px; }

.sheet--empty { align-items: center; justify-content: center; color: #9aa4b6; text-align: center; min-height: 297mm; }
.sheet--empty p { max-width: 320px; font-size: 15px; }

/* Settings dialog */
.dlg { border: none; border-radius: 14px; padding: 0; box-shadow: var(--shadow); max-width: 560px; width: 92vw; max-height: 92vh; }
.dlg::backdrop { background: rgba(10, 37, 51, .55); }
.dlg__body { padding: 22px; display: flex; flex-direction: column; gap: 12px; max-height: 92vh; overflow-y: auto; }
.dlg__body h2 { margin: 0; font-size: 17px; }
.dlg__actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.dlg__sep { border: none; border-top: 1px solid var(--line); margin: 6px 0 2px; }
.dlg__subhead { margin: 0; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.sec-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.logo-preview { display: flex; align-items: center; gap: 12px; min-height: 20px; }
.logo-preview img { max-height: 44px; max-width: 120px; object-fit: contain; }
.logo-preview img:not([src]), .logo-preview img[src=""] { display: none; }

/* ---------------- Print ---------------- */
@media print {
  @page { size: A4; margin: 0; }
  body { display: block; background: #fff; }
  .no-print, .panel { display: none !important; }
  /* Plain block flow so the sheet fills the page width (a flex item would
     shrink-to-fit) and any on-screen fit-to-width scaling is undone. */
  .stage { padding: 0; display: block; overflow: visible; }
  .sheet-wrap { display: block; width: auto !important; height: auto !important; transform: none !important; }
  .sheet { box-shadow: none; width: auto; min-height: auto; margin: 0; transform: none !important; }
}

/* ---------------- Responsive (screen) ----------------
   The sheet itself never reflows — it is the print target and must stay A4.
   Only the workspace around it adapts; the preview is scaled to fit by
   fitSheet() in app.js. */

/* Tablet / small laptop: stack the panel above the preview. */
@media (max-width: 1000px) {
  body { display: block; min-height: 100dvh; }
  .panel {
    position: static;
    height: auto;
    overflow: visible;
    border-right: none;
    border-bottom: 1px solid var(--line);
    padding: 18px max(18px, env(safe-area-inset-left)) 18px max(18px, env(safe-area-inset-right));
  }
  .panel__actions {
    flex-direction: row;
    margin-top: 6px;
  }
  .panel__actions .btn { flex: 1; }
  .stage { padding: 18px 12px 24px; }
}

/* Phones. */
@media (max-width: 640px) {
  /* Nothing should be able to push the page sideways once stacked. */
  body { overflow-x: hidden; }

  .panel { padding: 14px max(14px, env(safe-area-inset-left)) 16px max(14px, env(safe-area-inset-right)); gap: 12px; }
  .panel__head h1 { font-size: 17px; }

  /* 16px inputs stop iOS Safari zooming the page on focus. */
  input[type="text"], input[type="date"], input[type="password"],
  input[type="file"], textarea, select { font-size: 16px; }
  textarea { font-size: 14px; }
  input[type="file"] { max-width: 100%; }

  /* Side-by-side field pairs get too cramped below ~640px. */
  .field-row { grid-template-columns: 1fr; gap: 10px; }

  /* Comfortable touch targets. */
  .btn { min-height: 44px; padding: 10px 12px; }
  .check { padding: 2px 0; }
  .check input { width: 20px; height: 20px; }

  .stage {
    padding: 12px max(8px, env(safe-area-inset-left)) max(20px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-right));
  }
  .sheet { box-shadow: 0 1px 3px rgba(10, 37, 51, .1), 0 6px 18px rgba(10, 37, 51, .1); }

  /* Dialogs: near full-screen sheets with the actions always reachable. */
  .dlg { width: 100%; max-width: none; max-height: 100dvh; height: 100dvh; border-radius: 0; margin: 0; }
  .dlg__body { padding: 18px; max-height: 100dvh; height: 100dvh; }
  .dlg__actions {
    position: sticky;
    bottom: 0;
    margin: auto -18px -18px;
    padding: 12px 18px max(12px, env(safe-area-inset-bottom));
    background: var(--panel-bg);
    border-top: 1px solid var(--line);
  }
  .dlg__actions .btn { flex: 1; }
  .sec-row { flex-wrap: wrap; }
}
