/* =============================================================================
   Reading Journal Booklet Generator — UI
   Mobile-first, modern design drawing on Apple HIG (clarity, depth, generous
   spacing, iOS switches/segmented controls) and Material 3 (tonal buttons,
   elevation, state layers). Light + dark mode via prefers-color-scheme.
   ============================================================================= */

:root {
  /* Brand / accent */
  --accent: #0a84ff;          /* iOS system blue */
  --accent-press: #0060df;
  --accent-soft: rgba(10, 132, 255, 0.12);

  /* Neutrals (light) */
  --bg: #f2f2f7;              /* iOS grouped background */
  --surface: #ffffff;
  --surface-2: #f7f7fb;
  --text: #1c1c1e;
  --text-2: #6b6b70;
  --text-3: #9a9aa0;
  --border: rgba(60, 60, 67, 0.12);
  --border-strong: rgba(60, 60, 67, 0.22);
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.08), 0 16px 40px rgba(0,0,0,.10);

  --radius: 16px;
  --radius-sm: 11px;
  --radius-pill: 999px;

  --appbar-h: 56px;
  --tabs-h: 52px;
  --maxw: 1080px;

  --ease: cubic-bezier(.4, 0, .2, 1);

  font-synthesis: none;
  -webkit-text-size-adjust: 100%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #0a84ff;
    --accent-press: #409cff;
    --accent-soft: rgba(10, 132, 255, 0.20);
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-2: #2c2c2e;
    --text: #f5f5f7;
    --text-2: #aeaeb2;
    --text-3: #8e8e93;
    --border: rgba(255, 255, 255, 0.12);
    --border-strong: rgba(255, 255, 255, 0.20);
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.5);
    --shadow-lg: 0 4px 12px rgba(0,0,0,.5), 0 16px 40px rgba(0,0,0,.6);
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI",
               Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------------------------------------------------------------- App bar -- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: color-mix(in srgb, var(--surface) 80%, transparent);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}
.appbar-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  height: var(--appbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  border-radius: 11px;
  color: #fff;
  background: linear-gradient(135deg, #0a84ff, #5e5ce6);
  box-shadow: 0 4px 12px rgba(10,132,255,.35);
}
.brand-text h1 { margin: 0; font-size: 17px; font-weight: 650; letter-spacing: -0.01em; }
.brand-text p { margin: 0; font-size: 12px; color: var(--text-2); }

/* ------------------------------------------------------------------ Tabs -- */
/* Visible only on mobile; desktop shows both panels side-by-side. */
.tabs {
  position: sticky;
  top: calc(var(--appbar-h) + env(safe-area-inset-top));
  z-index: 20;
  display: flex;
  gap: 4px;
  padding: 8px 16px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}
.tab {
  flex: 1;
  appearance: none;
  border: none;
  background: var(--surface-2);
  color: var(--text-2);
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  padding: 9px 12px;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: background .2s var(--ease), color .2s var(--ease);
}
.tab.is-active { background: var(--accent); color: #fff; }

/* ---------------------------------------------------------------- Layout -- */
.layout {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 12px 16px calc(40px + env(safe-area-inset-bottom));
  display: block;
}

.panel { display: block; }
.panel-create { display: flex; flex-direction: column; gap: 14px; }

/* On mobile only one panel shows at a time. */
@media (max-width: 859px) {
  .panel[data-panel]:not(.is-active) { display: none; }
}

/* ----------------------------------------------------------------- Cards -- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-title {
  margin: 0 0 14px;
  font-size: 13px;
  font-weight: 650;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-2);
}

/* ---------------------------------------------------------------- Fields -- */
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.field label,
.toggle-label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.muted { color: var(--text-3); font-weight: 400; }

input[type="text"],
input[type="number"],
input[type="date"],
textarea {
  width: 100%;
  font: inherit;
  font-size: 16px;            /* >=16px prevents iOS zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease), background .15s var(--ease);
  -webkit-appearance: none;
  appearance: none;
}
textarea { resize: vertical; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--text-3); }

input:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px var(--accent-soft);
  background: var(--surface);
}
input[type="date"] { min-height: 46px; }

/* Stepper (chapter count) */
.stepper {
  display: flex;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.stepper input {
  border: none;
  background: transparent;
  text-align: center;
  box-shadow: none;
  border-radius: 0;
  font-weight: 600;
  -moz-appearance: textfield;
}
.stepper input::-webkit-outer-spin-button,
.stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper-btn {
  flex: 0 0 52px;
  align-self: stretch;
  border: none;
  background: transparent;
  color: var(--accent);
  font-size: 22px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s var(--ease);
}
.stepper-btn:active { background: var(--accent-soft); }

/* --------------------------------------------------------- Segmented ctl -- */
.segmented {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 3px;
  isolation: isolate;
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label {
  position: relative;
  z-index: 2;
  margin: 0;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-2);
  padding: 8px 10px;
  border-radius: calc(var(--radius-sm) - 3px);
  cursor: pointer;
  transition: color .2s var(--ease);
}
.segmented-thumb {
  position: absolute;
  z-index: 1;
  top: 3px; bottom: 3px; left: 3px;
  width: calc(50% - 3px);
  background: var(--surface);
  border-radius: calc(var(--radius-sm) - 3px);
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
  transition: transform .25s var(--ease);
}
#qm-custom:checked ~ .segmented-thumb,
#ps-a5:checked ~ .segmented-thumb { transform: translateX(100%); }
#qm-default:checked ~ label[for="qm-default"],
#qm-custom:checked ~ label[for="qm-custom"],
#ps-letter:checked ~ label[for="ps-letter"],
#ps-a5:checked ~ label[for="ps-a5"] { color: var(--text); }

.hint { margin: 10px 2px 0; font-size: 13px; color: var(--text-2); }

/* ----------------------------------------------------- Toggle (iOS switch) */
.toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.toggle:first-of-type { padding-top: 0; }
.toggle:last-of-type { padding-bottom: 0; border-bottom: none; }
.toggle-text { display: flex; flex-direction: column; }
.toggle-label { margin: 0; }
.toggle-sub { font-size: 12.5px; color: var(--text-2); margin-top: 2px; }

.switch { position: relative; flex: 0 0 auto; width: 51px; height: 31px; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch-track {
  position: absolute; inset: 0;
  background: var(--border-strong);
  border-radius: var(--radius-pill);
  transition: background .25s var(--ease);
}
.switch-track::before {
  content: "";
  position: absolute;
  top: 2px; left: 2px;
  width: 27px; height: 27px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0,0,0,.25);
  transition: transform .25s var(--ease);
}
.switch input:checked + .switch-track { background: #34c759; }   /* iOS green */
.switch input:checked + .switch-track::before { transform: translateX(20px); }
.switch input:focus-visible + .switch-track { box-shadow: 0 0 0 4px var(--accent-soft); }

/* --------------------------------------------------------------- Buttons -- */
.actions { display: flex; flex-direction: column; gap: 10px; }
.actions-split { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  appearance: none;
  border: none;
  font: inherit;
  font-weight: 600;
  font-size: 15px;
  min-height: 48px;                 /* comfortable touch target */
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform .08s var(--ease), background .15s var(--ease), opacity .15s var(--ease);
  user-select: none;
}
.btn:active { transform: scale(.975); }
.btn-primary { background: var(--accent); color: #fff; box-shadow: 0 4px 14px rgba(10,132,255,.3); }
.btn-primary:active { background: var(--accent-press); }
/* The headline output: clearly the file to print. */
.btn-download {
  background: var(--accent-soft);
  color: var(--accent);
  border: 1.5px solid var(--accent);
  font-weight: 700;
}
.btn-download:active { background: color-mix(in srgb, var(--accent) 22%, transparent); }
.btn-tonal { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn[disabled] { opacity: .5; cursor: progress; }
.btn.is-busy { position: relative; color: transparent !important; }
.btn.is-busy::after {
  content: "";
  position: absolute;
  width: 18px; height: 18px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  color: #fff;
  animation: spin .7s linear infinite;
}
.btn-tonal.is-busy::after,
.btn-download.is-busy::after { color: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }

.actions-hint { margin-top: 12px; line-height: 1.5; }
.actions-hint strong { color: var(--text); }

.status { margin: 12px 2px 0; font-size: 13.5px; color: var(--text-2); min-height: 18px; }
.status.error { color: #ff3b30; }
.status.ok { color: #34c759; }

/* ------------------------------------------------------------------ Help -- */
.help summary {
  cursor: pointer; font-weight: 600; font-size: 14px; color: var(--accent);
  list-style: none;
}
.help summary::-webkit-details-marker { display: none; }
.help summary::before { content: "ⓘ "; }
.help[open] summary { margin-bottom: 8px; }
.help ol { margin: 0; padding-left: 20px; line-height: 1.7; font-size: 14px; color: var(--text); }

/* --------------------------------------------------------------- Preview -- */
.preview-card { padding: 14px; }
.preview-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.preview-head .card-title { margin: 0; }
.chip {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2);
  color: var(--accent); font: inherit; font-weight: 600; font-size: 13px;
  padding: 6px 14px; border-radius: var(--radius-pill); cursor: pointer;
}
.chip:active { background: var(--accent-soft); }

.preview-stage {
  position: relative;
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  overflow: hidden;
  min-height: 60vh;
}
.preview-empty {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; color: var(--text-3); text-align: center; padding: 24px;
}
.preview-empty p { margin: 0; font-size: 14px; }
#preview-frame { display: block; width: 100%; height: 100%; min-height: 60vh; border: none; background: #e9e9ec; }

.hidden { display: none !important; }

/* =========================================================== Desktop ≥860 */
@media (min-width: 860px) {
  .tabs { display: none; }
  .layout {
    display: grid;
    grid-template-columns: minmax(360px, 420px) 1fr;
    gap: 20px;
    padding-top: 20px;
    align-items: start;
  }
  /* Both panels always visible on desktop. */
  .panel[data-panel] { display: block; }
  .panel-create { display: flex; }
  .panel-preview { position: sticky; top: calc(var(--appbar-h) + 20px); }
  .preview-stage, #preview-frame { min-height: calc(100vh - var(--appbar-h) - 120px); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

.help-note { margin: 10px 0 0; font-size: 13px; color: var(--text-2); line-height: 1.5; }
