:root { font-size: calc(16px * var(--user-text-scale, 1)); }

:root[data-theme="paper"] {
  color-scheme: light;
  --bg: #faf7f2; --raised: #fffdf9; --card: #f2ebdd;
  --fg: #211c15; --muted: #6a6152;
  --border: #e6ddcc; --border-strong: #9a8a63;
  --accent: #8a3324; --accent-hover: #732a1d; --on-accent: #fdf6f0;
  --link: #8a3324; --warn: #8f4310;
}

:root[data-theme="midnight"] {
  color-scheme: dark;
  --bg: #0d1120; --raised: #1b2138; --card: #151a2b;
  --fg: #ece6da; --muted: #9aa1b8;
  --border: #242a40; --border-strong: #656d96;
  --accent: #8891f0; --accent-hover: #9aa3f5; --on-accent: #0d1120;
  --link: #e3ab5e; --warn: #eab04a;
}

:root[data-theme="zinc"] {
  color-scheme: dark;
  --bg: #111216;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #e8e9ec; --muted: #b3b5bc;
  --border: rgba(205, 209, 216, 0.16);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #b5b8bd; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #e4e5e7; --warn: #dfba66;
}
:root[data-theme="crimson"] {
  color-scheme: dark;
  --bg: #200b12;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #f7dadd; --muted: #c99ea4;
  --border: rgba(238, 111, 123, 0.18);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #dd5d68; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #ff9b9f; --warn: #e2b667;
}
:root[data-theme="rose"] {
  color-scheme: dark;
  --bg: #210c1a;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #f6e2e9; --muted: #cca7b6;
  --border: rgba(241, 150, 185, 0.18);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #df709b; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #f7b1ca; --warn: #e1b66b;
}
:root[data-theme="amber"] {
  color-scheme: dark;
  --bg: #1b140b;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #f5e8d3; --muted: #cbb598;
  --border: rgba(235, 190, 114, 0.18);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #d58b4b; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #f8c58d; --warn: #e5b45a;
}
:root[data-theme="forest"] {
  color-scheme: dark;
  --bg: #0d1c12;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #e0f2d8; --muted: #a8c1ad;
  --border: rgba(139, 201, 111, 0.17);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #73b85c; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #b9e88f; --warn: #d9bd6b;
}
:root[data-theme="sky"] {
  color-scheme: dark;
  --bg: #0d2133;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #e6f3ff; --muted: #b2c9db;
  --border: rgba(175, 218, 249, 0.19);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #89c9f2; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #c6e4ff; --warn: #dfbd68;
}
:root[data-theme="aqua"] {
  color-scheme: dark;
  --bg: #061c2e;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #d9f3ff; --muted: #a8bfd0;
  --border: rgba(112, 203, 244, 0.18);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #55b9e9; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #9ee5ff; --warn: #e4bd6a;
}
:root[data-theme="royal"] {
  color-scheme: dark;
  --bg: #170d29;
  --card: color-mix(in srgb, var(--bg) 90%, #fff);
  --raised: color-mix(in srgb, var(--bg) 84%, #fff);
  --fg: #f0e8f7; --muted: #b8a9c8;
  --border: rgba(231, 203, 104, 0.19);
  --border-strong: color-mix(in srgb, var(--muted) 78%, var(--bg));
  --accent: #a27ae2; --accent-hover: color-mix(in srgb, var(--accent) 82%, #fff);
  --on-accent: #000; --link: #e6c05e; --warn: #f0d66e;
}

:root[data-font="serif"] { font-family: var(--serif); }
:root[data-font="mono"]  { font-family: var(--mono); }


/* Reduced animation */
:root[data-reduce-motion="on"] *,
:root[data-reduce-motion="on"] *::before,
:root[data-reduce-motion="on"] *::after {
  animation-duration: 0.001ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.001ms !important;
  scroll-behavior: auto !important;
}

/* High contrast */
:root[data-contrast="high"] {
  --muted: var(--fg);
  --border: var(--border-strong);
}
:root[data-contrast="high"] a { text-decoration: underline; }
:root[data-contrast="high"] input,
:root[data-contrast="high"] textarea,
:root[data-contrast="high"] button,
:root[data-contrast="high"] .editor-heading { border-width: 2px; }
:root[data-contrast="high"] :focus-visible { outline-width: 3px; }

/* Focus highlighting  */
:root[data-focus="strong"] :focus-visible {
  outline: 4px solid var(--link) !important;
  outline-offset: 3px !important;
}

/* Grayscale */
#grayscale-layer { display: none; }
:root[data-grayscale="on"] #grayscale-layer {
  display: block; position: fixed; inset: 0;
  z-index: 2147483646; pointer-events: none;
  -webkit-backdrop-filter: grayscale(1); backdrop-filter: grayscale(1);
}

/* Dyslexia-friendly */
:root[data-dyslexia="on"],
:root[data-dyslexia="on"] #editor .ProseMirror {
  font-family: "Atkinson Hyperlegible", "Comic Sans MS", "Segoe UI", Verdana, sans-serif;
  letter-spacing: 0.03em;
  word-spacing: 0.08em;
  line-height: 1.7;
}

/* Enhanced cursor  */
:root[data-cursor="large"],
:root[data-cursor="large"] * {
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><path d="M5 2 L5 27 L12 20 L16 29 L20 27 L16 18 L25 18 Z" fill="black" stroke="white" stroke-width="1.5" stroke-linejoin="round"/></svg>') 3 2, auto;
}
:root[data-cursor="large"] a,
:root[data-cursor="large"] button,
:root[data-cursor="large"] [role="switch"],
:root[data-cursor="large"] summary {
  cursor: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32"><path d="M12 2 a3 3 0 0 1 3 3 v7 h1 v-3 a3 3 0 0 1 6 0 v3 h1 v-1 a3 3 0 0 1 6 0 v9 a6 6 0 0 1 -6 6 h-6 a6 6 0 0 1 -5 -3 l-5 -8 a3 3 0 0 1 5 -3 l1 1 v-11 a3 3 0 0 1 3 -3 z" fill="black" stroke="white" stroke-width="1.5" stroke-linejoin="round"/></svg>') 10 2, pointer;
}

/* Underline links */
:root[data-underline-links="on"] a { text-decoration: underline !important; }

/* Reading guide */
#reading-guide {
  position: fixed; left: 0; right: 0; height: 0;
  border-top: 2px solid var(--link);
  opacity: 0.55; pointer-events: none; z-index: 60;
  transform: translateY(-100px);
}

#settings-view,
#preview-view {
  position: fixed; inset: 0; z-index: 45;
  overflow-y: auto; background: var(--bg);
  padding: env(safe-area-inset-top) 1.25rem 3rem;
}
#settings-view[hidden],
#preview-view[hidden] { display: none; }
.settings-inner { max-width: 42rem; margin: 0 auto; }

.settings-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: 1.1rem 0 0.9rem; margin-bottom: 0.5rem;
  border-bottom: 1px solid var(--border);
}
.settings-head h2 { margin: 0; }
.settings-section { margin: 1.75rem 0; }
.settings-section > h3 {
  margin: 0 0 0.9rem; padding-bottom: 0.55rem;
  font-family: var(--sans); font-size: 0.95rem; font-weight: 700;
  letter-spacing: 0; color: var(--fg);
  border-bottom: 1px solid var(--border-strong);
}

.setting-row {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 1rem; padding: 0.7rem 0;
}
.setting-row + .setting-row { border-top: 1px solid var(--border); }
.setting-copy { min-width: 0; }
.setting-copy .setting-name { margin: 0; font-weight: 600; font-size: 0.95rem; }
.setting-copy .setting-desc { margin: 0.15rem 0 0; font-size: 0.82rem; color: var(--muted); }
.setting-control { flex: none; }
.setting-control select {
  width: auto; min-width: 11rem; margin: 0;
  padding: 0.55rem 0.7rem; font: inherit; color: var(--fg);
  background: var(--raised); border: 1px solid var(--border-strong);
  border-radius: var(--r-sm);
}

.setting-row.is-stacked { flex-direction: column; align-items: stretch; gap: 0.5rem; }
.setting-row.is-stacked .setting-control { align-self: start; max-width: 100%; }
.setting-row.is-stacked .setting-control select { max-width: 100%; }
.setting-range { display: flex; align-items: center; gap: 0.75rem; margin-top: 0.5rem; }
.setting-range input[type="range"] { flex: 1; width: auto; margin: 0; accent-color: var(--accent); }
.setting-range output { font-variant-numeric: tabular-nums; min-width: 3.5rem; text-align: right; color: var(--muted); }

.setting-field { display: flex; gap: 0.6rem; align-items: flex-start; margin-top: 0.6rem; }
.setting-field input[type="text"] { flex: 1; min-width: 0; margin: 0; }
.setting-field .setting-save { flex: none; }
.setting-status { margin: 0.45rem 0 0; min-height: 1.15em; font-size: 0.82rem; color: var(--muted); }

.switch {
  --sw-w: 46px; --sw-h: 26px;
  position: relative; flex: none;
  width: var(--sw-w); height: var(--sw-h); padding: 0; min-height: 0;
  background: var(--card); border: 1px solid var(--border-strong);
  border-radius: 999px; cursor: pointer;
  transition: background-color 140ms ease, border-color 140ms ease;
}
.switch::after {
  content: ""; position: absolute; top: 50%; left: 3px;
  width: calc(var(--sw-h) - 8px); height: calc(var(--sw-h) - 8px);
  transform: translateY(-50%);
  background: var(--fg); border-radius: 50%;
  transition: left 140ms ease, background-color 140ms ease;
}
.switch[aria-checked="true"] { background: var(--accent); border-color: var(--accent); }
.switch[aria-checked="true"]::after { left: calc(var(--sw-w) - var(--sw-h) + 5px); background: var(--on-accent); }
.switch:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

.preview-section { margin: 1.5rem 0; }
.preview-section > h3 {
  margin: 0 0 0.8rem; padding-bottom: 0.55rem;
  font-family: var(--sans); font-size: 0.95rem; font-weight: 700;
  letter-spacing: 0; color: var(--fg);
  border-bottom: 1px solid var(--border-strong);
}
.preview-rendered {
  background: var(--card); border: 1px solid var(--border);
  border-radius: var(--r-sm); padding: 0.9rem 1.1rem;
}
.preview-rendered > :first-child { margin-top: 0; }
.preview-rendered .preview-week-title {
  font-size: 1.15rem; font-weight: 700; margin: 0 0 0.6rem;
  padding-bottom: 0.5rem; border-bottom: 1px solid var(--border);
}
.preview-rendered > :last-child { margin-bottom: 0; }
.preview-rendered h3, .preview-rendered h4, .preview-rendered h5, .preview-rendered h6 {
  font-size: 1rem; margin: 1rem 0 0.4rem;
}
.preview-rendered blockquote {
  margin-inline: 0; padding-left: 0.75rem;
  border-left: 2px solid var(--border-strong); color: var(--muted);
}
.preview-rendered pre { overflow-x: auto; }

.preview-ok { margin: 0; color: var(--muted); }
.preview-note { margin: 0.5rem 0 0; font-size: 0.85rem; color: var(--muted); }
.preview-empty { color: var(--muted); }
.preview-warn { margin: 0 0 0.6rem; color: var(--fg); font-weight: 600; }
.preview-list { margin: 0.4rem 0 0; padding-left: 1.2rem; }
.preview-list li { margin: 0.35rem 0; }
.preview-list li strong { font-weight: 700; }
.preview-context {
  display: block; margin-top: 0.15rem;
  font-size: 0.85rem; color: var(--muted); font-style: italic;
}

.preview-export {
  display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem;
  margin-top: 0.85rem;
}
.preview-export-label { font-size: 0.85rem; color: var(--muted); margin-right: 0.1rem; }
.preview-export-btn {
  min-height: 0; margin: 0; padding: 0.4rem 0.75rem;
  font: inherit; font-size: 0.85rem; line-height: 1;
  color: var(--fg); background: var(--raised);
  border: 1px solid var(--border-strong); border-radius: var(--r-sm);
  cursor: pointer; transition: background-color 120ms ease, color 120ms ease, border-color 120ms ease;
}
.preview-export-btn:hover { background: color-mix(in srgb, var(--fg) 8%, transparent); }
.preview-export-btn:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }
.preview-export-btn.is-done {
  color: var(--on-accent); background: var(--accent); border-color: var(--accent);
}

#open-settings {
  color: var(--muted); font-weight: 500;
  display: inline-flex; align-items: center; gap: 0.35rem;
}
#open-settings:hover { color: var(--fg); background: color-mix(in srgb, var(--fg) 8%, transparent); }
.header-actions { display: inline-flex; align-items: center; gap: 0.4rem; }

@media (max-width: 26rem) {
  .setting-control select { min-width: 8.5rem; }
}

@media (min-width: 100rem) {
  :root { --shell-max: 100rem; --shell-gutter: max(0px, calc((100vw - var(--shell-max)) / 2)); }
  body.app-shell header { left: var(--shell-gutter); right: var(--shell-gutter); }
  body.app-shell #week-sidebar { left: var(--shell-gutter); }
  body.app-shell #capture-main > * { max-width: 46rem; }
  body.app-shell #settings-view .settings-inner,
  body.app-shell #preview-view .settings-inner { max-width: 46rem; }
}
