/* Colors live in one place so light and dark mode stay in step. */
:root {
  color-scheme: light dark;
  --bg: #f3f2ee;
  --surface: #ffffff;
  --ink: #1b1d20;
  --muted: #565a61;
  --line: #d3d1c9;
  --accent: #0b4f6c;
  --on-accent: #ffffff;
  --danger: #b3261e;
  --danger-soft: #fbe9e7;
  --gutter: 16px;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1f2228;
    --ink: #f1f1ee;
    --muted: #a9adb5;
    --line: #3a3e46;
    --accent: #5cb6dc;
    --on-accent: #08242f;
    --danger: #ff8a80;
    --danger-soft: #3a1d1b;
  }
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-padding-top: 76px;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 17px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.app {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0 max(var(--gutter), env(safe-area-inset-right)) calc(32px + env(safe-area-inset-bottom)) max(var(--gutter), env(safe-area-inset-left));
}

/* Spanish always sits on its own line under the English, quieter and in italics. */
.es {
  display: block;
  color: var(--muted);
  font-size: 0.82em;
  font-style: italic;
  font-weight: 400;
}

h1, h2, p { margin: 0; }

.page-header { padding: calc(16px + env(safe-area-inset-top)) 0 12px; }
h1 { font-size: 26px; line-height: 1.15; }
h1 .es { font-size: 16px; margin-top: 2px; }
.intro { margin-top: 8px; font-size: 15px; }

h2 { font-size: 21px; line-height: 1.2; }
h2 .es { font-size: 15px; }

/* Buttons */
button { font: inherit; color: inherit; }

.btn {
  min-height: 56px;
  padding: 8px 14px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  font-weight: 600;
  line-height: 1.15;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.btn .es { margin-top: 2px; }
.btn:active { background: var(--line); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary .es { color: inherit; opacity: 0.85; }
.btn-primary:active { background: var(--accent); opacity: 0.8; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: var(--surface); }
.btn-danger .es { color: inherit; opacity: 0.9; }
.btn-danger:active { background: var(--danger); opacity: 0.8; }

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

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.actions-bottom { margin-top: 24px; }

.hint { margin-top: 10px; color: var(--muted); font-size: 14px; }

/* Alerts */
.alert {
  margin: 8px 0;
  padding: 12px 14px;
  border: 1.5px solid var(--danger);
  border-radius: var(--radius);
  background: var(--danger-soft);
  font-size: 15px;
}

/* Running counts bar */
.summary {
  position: sticky;
  top: 0;
  z-index: 5;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin: 0 calc(var(--gutter) * -1);
  padding: calc(6px + env(safe-area-inset-top)) var(--gutter) 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.summary a {
  display: block;
  min-height: 52px;
  padding: 5px 2px;
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.15;
  text-align: center;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.summary .count { display: block; font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.summary .over { background: var(--danger-soft); color: var(--danger); }

/* Cards */
.card {
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
}

/* Session details */
.details { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 14px; }
.field { min-width: 0; }
.field-date, .field-route, .field-warning { grid-column: 1 / -1; }
.field-warning { color: var(--danger); font-size: 14px; font-weight: 600; }
.field-warning .es { color: inherit; }

label { display: block; margin-bottom: 4px; font-size: 14px; font-weight: 600; }
label .es { display: inline; margin-left: 4px; }

/* 16px or larger text stops iOS from zooming the page when a field is tapped. */
input[type="date"], input[type="time"], input[type="text"], textarea {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 48px;
  padding: 10px 12px;
  border: 1.5px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--ink);
  font: inherit;
  font-size: 17px;
  -webkit-appearance: none;
  appearance: none;
}
input::-webkit-date-and-time-value { text-align: left; }
textarea { min-height: 44px; font-size: 16px; line-height: 1.3; resize: none; overflow: hidden; }

/* The three parts */
.part { margin-top: 26px; scroll-margin-top: 76px; }
.part-note { margin-top: 4px; font-size: 15px; font-weight: 600; }

.skill-list { display: grid; gap: 10px; margin-top: 10px; }

.skill {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px 10px;
  align-items: center;
}
.skill-name { font-size: 18px; font-weight: 700; line-height: 1.2; }
.skill-name .es { font-size: 14px; }
.skill-hint, .skill-notes { grid-column: 1 / -1; }
.skill-hint { color: var(--ink); font-size: 14px; }
.skill-notes label { margin-bottom: 3px; font-size: 13px; color: var(--muted); }

.tally { display: flex; align-items: center; }
.tally-btn {
  width: 62px;
  height: 62px;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  background: transparent;
  color: var(--accent);
  font-size: 34px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  /* Stops a quick double tap from zooming the page instead of counting twice. */
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.tally-plus { background: var(--accent); color: var(--on-accent); }
.tally-btn:active { opacity: 0.65; }
.tally-btn:disabled { border-color: var(--line); color: var(--line); cursor: default; }
.tally-count {
  width: 44px;
  font-size: 26px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.limit {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 16px;
  margin-top: 10px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--line);
  font-size: 15px;
  font-weight: 600;
}
.limit-total { font-variant-numeric: tabular-nums; }
.limit.over { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }
.limit.over .es { color: inherit; }

/* Automatic fail */
.autofail-list { display: grid; gap: 8px; margin-top: 10px; }
.autofail {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 62px;
  margin: 0;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.autofail .es { display: block; margin: 0; font-size: 14px; }
.autofail input {
  flex: none;
  width: 30px;
  height: 30px;
  margin: 0;
  accent-color: var(--danger);
}
.autofail.checked { border-color: var(--danger); background: var(--danger-soft); }

/* Top 3 */
.top3-list { display: grid; gap: 10px; margin: 10px 0 0; padding-left: 40px; }
.top3-list li { font-weight: 700; }

.page-footer { margin-top: 18px; color: var(--muted); font-size: 13px; }
.save-status { margin-bottom: 8px; font-weight: 600; }

/* Confirmation box */
.dialog {
  width: min(440px, calc(100vw - 32px));
  padding: 20px;
  border: 0;
  border-radius: 16px;
  background: var(--surface);
  color: var(--ink);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.dialog p { margin-top: 10px; font-size: 16px; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 18px; }

/* iPad and wider: use the extra room for two columns of skills */
@media (min-width: 720px) {
  h1 { font-size: 32px; }
  .page-header { display: grid; grid-template-columns: 1fr auto; column-gap: 24px; align-items: start; }
  .page-header .actions { grid-column: 2; grid-row: 1 / span 2; margin-top: 0; min-width: 300px; }
  .intro { grid-column: 1; }
  .details { grid-template-columns: 1.2fr 1fr 1fr 2fr; }
  .field-date, .field-route { grid-column: auto; }
  .skill-list, .autofail-list { grid-template-columns: 1fr 1fr; }
  .top3-list { grid-template-columns: 1fr; }
  .actions-bottom { max-width: 520px; }
}

/* Points and Pass/Fail */
.scoreboard { display: flex; gap: 10px; margin-top: 12px; }
.score-box {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 12px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: var(--surface);
}
.score-label { font-size: 13px; font-weight: 700; line-height: 1.1; }
.score-value { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.result {
  display: block;
  flex: 1;
  padding: 6px 12px;
  border: 2px solid var(--accent);
  border-radius: 8px;
  color: var(--accent);
  font-size: 18px;
  font-weight: 700;
  line-height: 1.1;
  text-align: center;
}
.result .es { color: inherit; font-size: 12px; }
.result.fail { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }

/* Only used on the iPad sheet below */
.skill-label, .col-label, .limit-box { display: none; }

/* ---------- iPad sheet: every part on one screen, laid out like the paper form ----------
   --row is how tall each skill row is. It grows to use whatever height the screen has,
   and the text and buttons grow with it. */
@media screen and (min-width: 700px) and (min-height: 600px) {
  :root {
    --row: clamp(26px, min(calc((100dvh - 444px) / 19), calc((100dvh - 310px) / 22.5)), 72px);
    --auto-row: max(26px, calc(var(--row) * 0.7));
    --btn-w: clamp(54px, calc(var(--row) * 1.3), 84px);
    --count-w: clamp(32px, calc(var(--row) * 0.8), 48px);
    --sheet-cols: minmax(0, 1.25fr) calc(var(--btn-w) * 2 + var(--count-w) + 4px) minmax(0, 1fr);
    --gutter: 12px;
  }
  html { scroll-padding-top: 0; }
  body { font-size: 13px; }
  .app { max-width: none; padding-bottom: env(safe-area-inset-bottom); }

  .summary, .intro, .hint, .actions-bottom, .save-status { display: none; }

  .page-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: calc(6px + env(safe-area-inset-top)) 0 4px;
  }
  h1 { flex: 1; font-size: 20px; }
  h1 .es { font-size: 12px; margin-top: 0; }
  .scoreboard { flex: none; margin: 0; gap: 6px; }
  .score-box { flex: none; gap: 8px; padding: 2px 8px; height: 40px; }
  .score-label { font-size: 12px; }
  .score-label .es { font-size: 10px; }
  .score-value { min-width: 28px; font-size: 20px; text-align: right; }
  .result { flex: none; min-width: 70px; height: 40px; padding: 3px 8px; font-size: 14px; }
  .result .es { font-size: 10px; }
  .page-header .actions { display: flex; gap: 6px; margin: 0; min-width: 0; }
  .btn { min-height: 40px; padding: 2px 10px; font-size: 12px; }
  .btn .es { margin-top: 0; font-size: 10px; }
  .dialog .btn { min-height: 56px; font-size: 15px; }

  .details {
    display: flex;
    gap: 10px;
    margin: 0;
    padding: 0 0 4px;
    border: 0;
    border-radius: 0;
    background: none;
  }
  .field { display: flex; align-items: center; gap: 5px; }
  .field-route { flex: 1; }
  .field label { margin: 0; font-size: 11px; line-height: 1.1; }
  .field label .es { display: block; margin: 0; font-size: 10px; }
  .field-warning { display: none; }
  .details input { min-height: 30px; height: 30px; padding: 0 6px; font-size: 16px; }
  .field-date input { width: 132px; }
  .field:not(.field-date):not(.field-route) input { width: 104px; }

  /* The three parts */
  .part { margin-top: 4px; }
  .part-head {
    display: grid;
    grid-template-columns: var(--sheet-cols);
    align-items: end;
    border-bottom: 1.5px solid var(--ink);
  }
  h2 { font-size: 15px; line-height: 1.25; }
  h2 .es { display: inline; margin-left: 5px; font-size: 12px; }
  .col-label { display: block; color: var(--muted); font-size: 10px; font-weight: 600; text-align: center; }
  .col-label:last-child { padding-left: 6px; text-align: left; }

  .skill-list { display: block; margin: 0; }
  .skill {
    display: grid;
    grid-template-columns: var(--sheet-cols);
    gap: 0;
    height: var(--row);
    padding: 0;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    background: none;
  }
  .skill-name, .skill-hint { display: none; }
  .skill-label {
    display: block;
    overflow: hidden;
    font-size: clamp(12px, calc(var(--row) * 0.33), 18px);
    font-weight: 700;
    line-height: 1.1;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .skill-label .es { font-size: clamp(10px, calc(var(--row) * 0.26), 14px); }
  .tally { justify-content: center; height: 100%; padding: clamp(1px, calc(var(--row) * 0.05), 3px) 0; }
  .tally-btn {
    width: var(--btn-w);
    height: 100%;
    border-width: 1.5px;
    border-radius: 6px;
    font-size: clamp(20px, calc(var(--row) * 0.5), 30px);
  }
  .tally-count { width: var(--count-w); font-size: clamp(16px, calc(var(--row) * 0.42), 24px); }

  /* Notes stay one line tall here. The PDF still shows the whole note. */
  .skill-notes { grid-column: auto; height: 100%; padding: 1px 0 1px 6px; }
  .skill-notes label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
  }
  .skill-notes textarea {
    height: 100% !important;
    min-height: 0;
    padding: 0 6px;
    border-width: 1px;
    border-radius: 4px;
    font-size: 16px;
    line-height: 1.2;
    overflow: auto;
  }

  .limit {
    display: grid;
    grid-template-columns: var(--sheet-cols);
    align-items: center;
    gap: 0;
    margin: 0;
    padding: 2px 0;
    border: 0;
    border-radius: 0;
    background: none;
    color: var(--muted);
    font-size: 11px;
  }
  .limit .es { display: inline; margin-left: 4px; font-size: 10px; }
  .limit-total { display: none; }
  .limit-box { display: flex; align-items: center; justify-content: center; gap: 6px; color: var(--ink); font-size: 11px; font-weight: 700; }
  .limit-sum {
    min-width: 40px;
    border: 1.5px solid var(--ink);
    border-radius: 4px;
    font-size: 14px;
    line-height: 18px;
    text-align: center;
    font-variant-numeric: tabular-nums;
  }
  .limit.over { background: none; color: var(--danger); }
  .limit.over .limit-box { color: var(--danger); }
  .limit.over .limit-sum { border-color: var(--danger); background: var(--danger-soft); }

  /* Automatic fail and Top 3 */
  .part-note { display: inline; margin-left: 6px; font-size: 11px; font-weight: 400; }
  .part-note .es { display: inline; margin-left: 3px; font-size: 10px; }
  #auto h2, #top3 h2 { display: inline; }
  .autofail-list { grid-template-columns: 1fr 1fr; gap: 0 10px; margin-top: 2px; }
  .autofail {
    gap: 7px;
    min-height: var(--auto-row);
    padding: 0 2px;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    background: none;
    font-size: clamp(12px, calc(var(--auto-row) * 0.36), 16px);
    line-height: 1.1;
  }
  .autofail .es { font-size: clamp(10px, calc(var(--auto-row) * 0.28), 13px); }
  .autofail input { width: clamp(20px, calc(var(--auto-row) * 0.55), 28px); height: clamp(20px, calc(var(--auto-row) * 0.55), 28px); }
  .autofail.checked { background: var(--danger-soft); }

  .top3-list {
    grid-template-columns: repeat(3, 1fr);
    gap: 26px;
    margin: 2px 0 0;
    padding: 0 0 0 22px;
    border: 0;
    border-radius: 0;
    background: none;
  }
  .top3-list input { min-height: 30px; height: var(--auto-row); padding: 0 6px; font-size: 16px; }

  .page-footer { margin-top: 3px; font-size: 9px; line-height: 1.15; }
  .page-footer .es { display: inline; font-size: inherit; }
}

/* Landscape: on-street on the left, everything else on the right */
@media screen and (min-width: 980px) and (min-height: 600px) and (orientation: landscape) {
  :root {
    --row: clamp(26px, calc((100dvh - 192px) / 13), 80px);
  }
  main {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto auto auto 1fr auto;
    column-gap: 20px;
  }
  #parts { display: contents; }
  .details, .page-footer { grid-column: 1 / -1; }
  #street { grid-column: 1; grid-row: 2 / span 4; }
  #backing, #parking, #auto, #top3 { grid-column: 2; }
  .top3-list { gap: 24px; }

  /* Columns are narrower here, so use the short skill names. */
  .skill-label { display: none; }
  .skill-name { display: block; font-size: clamp(13px, calc(var(--row) * 0.3), 20px); }
  .skill-name .es { font-size: clamp(11px, calc(var(--row) * 0.22), 15px); }

  /* Put the Spanish on its own line so a single word never wraps by itself. */
  .limit .es { display: block; margin-left: 0; }
}

/* Wide landscape screens: headings stay on one line, leaving more room for rows */
@media screen and (min-width: 1580px) and (min-height: 600px) and (orientation: landscape) {
  :root { --row: clamp(26px, calc((100dvh - 160px) / 13), 80px); }
}

/* Tall landscape screens: the right column has room for Top 3 as a stacked list */
@media screen and (min-width: 980px) and (min-height: 1000px) and (orientation: landscape) {
  :root { --auto-row: calc(var(--row) * 0.75); }
  .top3-list { grid-template-columns: 1fr; gap: 0; }
}

.print-sheet { display: none; }

/* ---------- The PDF: a plain black-on-white sheet, always in light colors ---------- */
@page { size: letter; margin: 0.4in 0.5in; }

@media print {
  :root { color-scheme: light; }
  html, body { background: #fff; color: #111; font-size: 8pt; }
  .app, .dialog { display: none !important; }

  /* Fixed line heights keep the sheet the same size whichever font the device uses. */
  .print-sheet { display: block; color: #111; font-size: 8pt; line-height: 9pt; }
  .print-sheet .es { color: #555; font-size: 6.5pt; line-height: 7.5pt; }
  .print-sheet h1 { font-size: 15pt; line-height: 17pt; }
  .print-sheet h1 .es { display: inline; margin-left: 6pt; font-size: 9pt; line-height: 17pt; }
  .print-sheet h2 { margin-top: 6pt; font-size: 10pt; line-height: 12pt; break-after: avoid; page-break-after: avoid; }
  .print-sheet h2 .es { display: inline; margin-left: 4pt; font-size: 8.5pt; line-height: 12pt; }

  .print-meta { display: flex; flex-wrap: wrap; gap: 1pt 12pt; margin-top: 3pt; font-size: 8.5pt; line-height: 10.5pt; }
  .print-meta b { font-weight: 700; }

  /* A part, or a whole automatic-fail row, moves to the next page rather than splitting. */
  .print-part { break-inside: avoid; page-break-inside: avoid; }
  .print-sheet tr { break-inside: avoid; page-break-inside: avoid; }

  .print-sheet table { width: 100%; margin-top: 1pt; border-collapse: collapse; table-layout: fixed; }
  .print-sheet th { padding: 0 3pt; border-bottom: 1pt solid #111; font-size: 7pt; font-weight: 600; text-align: left; white-space: nowrap; }
  .print-sheet td { padding: 0.5pt 3pt; border-bottom: 0.5pt solid #bbb; vertical-align: top; overflow-wrap: anywhere; white-space: pre-wrap; }
  .print-sheet td .es { white-space: normal; }
  .print-sheet .col-skill { width: 50%; }
  .print-sheet .col-count { width: 14%; text-align: center; }
  .print-sheet td.col-count { font-size: 9pt; font-weight: 700; }

  .print-limit { display: flex; justify-content: space-between; gap: 10pt; margin-top: 1pt; font-size: 7pt; font-weight: 600; }
  .print-limit .es { display: inline; margin-left: 4pt; font-weight: 400; }
  .print-limit .over { text-decoration: underline; }

  .print-sheet .print-auto td { padding: 1pt 3pt 1pt 0; border: 0; width: 50%; white-space: normal; }
  .print-auto-item { display: flex; gap: 4pt; }
  .print-box { flex: none; width: 9pt; height: 9pt; border: 1pt solid #111; font-size: 8pt; font-weight: 700; line-height: 8pt; text-align: center; }
  .print-checked { font-weight: 700; }

  .print-top3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10pt; margin: 2pt 0 0; padding: 0; list-style-position: inside; font-size: 8pt; }
  .print-top3 li { min-height: 12pt; border-bottom: 0.5pt solid #bbb; overflow-wrap: anywhere; }
  .print-source { margin-top: 4pt; color: #555; font-size: 6pt; line-height: 7pt; }
}
