/*
 * Palette: "paper & ink". Light mode is warm paper with a terracotta accent;
 * dark mode is warm charcoal with the burnt orange accent (#c9490b, white text).
 * Theme follows the OS unless the user picked one ([data-theme] on <html>).
 * All text pairs >= 4.5:1, controls >= 3:1.
 */
:root {
  color-scheme: light;
  --bg: #f7f3ec;
  --surface: #fffdf9;
  --sidebar: #efe9df;
  --text: #231e1a;
  --muted: #6d645b;
  --border: #e2d9cc;
  --border-strong: #cfc3b3;
  --accent: #b4460f;        /* fills: primary button, checkboxes, selected theme */
  --accent-text: #ffffff;
  --accent-ink: #a13e0c;    /* accent-coloured text on surfaces */
  --accent-soft: #f7e4d6;   /* tinted backgrounds: hover, badges */
  --danger: #b3263e;
  --danger-bg: #fbe7ea;
  --warn-bg: #fbefcc;
  --warn-text: #6b5000;
  --code-bg: #f4eee5;
  --shadow: 0 1px 2px rgb(60 40 20 / .05), 0 6px 20px rgb(60 40 20 / .06);
  --ring: 0 0 0 3px rgb(180 70 15 / .25);
  --font-ui: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  /* Citations read like a paper's reference list. */
  /* Latin fonts first: CJK fonts draw curly quotes “ ” full-width. CJK fonts
     remain as fallback for Chinese characters in titles. */
  --font-serif: Georgia, "Times New Roman", "Liberation Serif", "Noto Serif", "DejaVu Serif",
    "Noto Serif TC", "Source Han Serif TC", "Songti TC", "PMingLiU", serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #151311;
    --surface: #1e1b18;
    --sidebar: #191614;
    --text: #ede7e0;
    --muted: #a59c92;
    --border: #35302b;
    --border-strong: #4a433c;
    --accent: #c9490b;
    --accent-text: #ffffff;
    --accent-ink: #f39a62;
    --accent-soft: #3b2416;
    --danger: #fb7f95;
    --danger-bg: #3a1c22;
    --warn-bg: #372d17;
    --warn-text: #f1d68e;
    --code-bg: #27231f;
    --shadow: 0 1px 2px rgb(0 0 0 / .35), 0 8px 24px rgb(0 0 0 / .25);
    --ring: 0 0 0 3px rgb(243 154 98 / .3);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #151311;
  --surface: #1e1b18;
  --sidebar: #191614;
  --text: #ede7e0;
  --muted: #a59c92;
  --border: #35302b;
  --border-strong: #4a433c;
  --accent: #c9490b;
  --accent-text: #ffffff;
  --accent-ink: #f39a62;
  --accent-soft: #3b2416;
  --danger: #fb7f95;
  --danger-bg: #3a1c22;
  --warn-bg: #372d17;
  --warn-text: #f1d68e;
  --code-bg: #27231f;
  --shadow: 0 1px 2px rgb(0 0 0 / .35), 0 8px 24px rgb(0 0 0 / .25);
  --ring: 0 0 0 3px rgb(243 154 98 / .3);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--accent-soft); color: var(--text); }

/* Layout: history on the left, main content on the right; each scrolls on its own. */
.layout {
  display: grid;
  grid-template-columns: minmax(280px, 380px) 1fr;
  height: 100vh;
  height: 100dvh;
}
.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}
.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 22px 18px 14px;
  border-bottom: 1px solid var(--border);
}
.sidebar-head h2 { font-size: .8rem; letter-spacing: .12em; color: var(--muted); }
.sidebar-scroll { flex: 1; overflow-y: auto; padding: 6px 10px 16px; }
.content { min-height: 0; overflow-y: auto; }
.sidebar-scroll, .content { scrollbar-color: var(--border-strong) transparent; scrollbar-width: thin; }
.top, main, footer { max-width: 900px; margin: 0 auto; padding: 0 24px; }

@media (max-width: 800px) {
  /* Narrow screens: one column, normal page scroll, history below the form. */
  .layout { display: flex; flex-direction: column; height: auto; }
  .content { order: 1; overflow: visible; }
  .sidebar { order: 2; border-right: none; border-top: 1px solid var(--border); }
  .sidebar-scroll { overflow: visible; }
  .top, main, footer { padding: 0 16px; }
}

/* Header */
.top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  padding-top: 32px;
}
h1 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  font: 600 1.75rem/1.2 var(--font-serif);
  letter-spacing: -.01em;
}
.logo { width: 1.15em; height: 1.15em; flex: none; }
.logo-page { fill: var(--accent); }
.logo-fold { fill: var(--accent-text); opacity: .4; }
.logo-line { fill: var(--accent-text); }
h2 { margin: 0; font-size: 1.05rem; }
.sub, .muted { color: var(--muted); }
.sub { margin: 6px 0 20px; }

.theme-switch {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
}
.theme-switch button {
  border: none;
  border-radius: 999px;
  padding: 3px 12px;
  font-size: .82rem;
  background: transparent;
  color: var(--muted);
}
.theme-switch button:hover { color: var(--text); background: var(--accent-soft); }
.theme-switch button[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  margin-bottom: 18px;
  box-shadow: var(--shadow);
}

/* Form */
label { font-weight: 600; }
textarea {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 12px;
  font: 14px/1.6 var(--font-mono);
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  resize: vertical;
  transition: border-color .15s, box-shadow .15s;
}
textarea::placeholder { color: var(--muted); opacity: .8; }
textarea:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }

.controls { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; margin-top: 16px; }
.inline { display: flex; gap: 10px; align-items: center; font-weight: normal; max-width: 100%; cursor: pointer; }
.inline select { max-width: 100%; }
input[type="checkbox"] { accent-color: var(--accent); width: 17px; height: 17px; margin: 0; cursor: pointer; }

select, button {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 5px 12px;
  cursor: pointer;
  transition: background-color .15s, border-color .15s, color .15s, box-shadow .15s;
}
select:focus-visible, button:focus-visible, input[type="checkbox"]:focus-visible, summary:focus-visible {
  outline: none;
  box-shadow: var(--ring);
}
button:hover { border-color: var(--accent); background: var(--accent-soft); }
button:disabled { opacity: .6; cursor: progress; }
button.primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  padding: 8px 22px;
  font-weight: 600;
  box-shadow: 0 1px 2px rgb(0 0 0 / .15);
}
button.primary:hover { background: var(--accent); filter: brightness(1.08); }
button.danger { color: var(--danger); }
button.danger:hover { border-color: var(--danger); background: transparent; }
button.small { font-size: .8rem; padding: 3px 10px; }

/* Results */
.section-head { display: flex; flex-wrap: wrap; gap: 8px; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }

.items { list-style: none; margin: 0; padding: 0; }
.item { border-top: 1px solid var(--border); padding: 14px 0; }
.item:first-child { border-top: none; }
.cite {
  font: 1.02rem/1.65 var(--font-serif);
  overflow-wrap: anywhere;
}
.label {
  font: 700 .85rem var(--font-mono);
  color: var(--accent-ink);
  margin-right: .4em;
}
.label:empty { display: none; }
.error { color: var(--danger); margin: 4px 0; }
.error:empty { display: none; }
.warnings { margin: 8px 0; padding: 0; list-style: none; }
.warnings li {
  background: var(--warn-bg);
  color: var(--warn-text);
  border-radius: 8px;
  padding: 4px 10px;
  margin-top: 4px;
  font-size: .88rem;
}
.meta { color: var(--muted); font-size: .82rem; overflow-wrap: anywhere; margin-top: 4px; }
.buttons { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.buttons button { font-size: .82rem; padding: 3px 10px; }
details { margin-top: 8px; }
summary { cursor: pointer; color: var(--muted); font-size: .85rem; border-radius: 4px; width: fit-content; }
summary:hover { color: var(--accent-ink); }
pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font: .8rem/1.55 var(--font-mono);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

/* History sidebar: compact cards with a hover highlight. */
.compact .item {
  border-top: none;
  border-radius: 10px;
  padding: 10px;
  margin-top: 4px;
  transition: background-color .15s;
}
.compact .item:hover { background: var(--surface); }
.compact .cite { font-size: .9rem; line-height: 1.55; }
.compact .meta { font-size: .76rem; }
.compact .buttons { gap: 4px; margin-top: 8px; }
.compact .buttons button { font-size: .76rem; padding: 2px 8px; }
.compact summary { font-size: .78rem; }
.compact pre { font-size: .72rem; }
#history-empty { padding: 10px; font-size: .9rem; }

footer { padding-bottom: 32px; font-size: .78rem; }

#toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 8px);
  background: var(--text);
  color: var(--bg);
  padding: 8px 18px;
  border-radius: 999px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

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

/* Notices: paused sources, request / load failures. */
.notice {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
  border-radius: 10px;
  padding: 10px 14px;
  margin-bottom: 14px;
  font-size: .9rem;
}
.notice .msg { flex: 1 1 240px; }
.notice.warn { background: var(--warn-bg); color: var(--warn-text); }
.notice.error { background: var(--danger-bg); color: var(--danger); }
.notice button { color: inherit; border-color: currentColor; background: transparent; }
.sidebar .notice { margin: 8px 0; }

.summary-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: -4px 0 6px; }
.summary-row p { margin: 0; font-size: .85rem; }

/* Failed or waiting-for-choice items get a coloured edge. */
.item.is-failed, .item.is-choice { padding-left: 12px; border-left: 3px solid var(--danger); }
.item.is-choice { border-left-color: var(--accent); }
.item.is-choice .error { color: var(--accent-ink); font-weight: 600; }
.unavailable { margin: 6px 0; padding: 0; list-style: none; }
.unavailable li {
  background: var(--warn-bg);
  color: var(--warn-text);
  border-radius: 8px;
  padding: 4px 10px;
  margin-top: 4px;
  font-size: .85rem;
}
.fail-actions { margin-top: 8px; }

.candidates { list-style: none; margin: 8px 0 0; padding: 0; }
.candidate {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  margin-top: 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.cand-main { flex: 1; min-width: 0; }
.cand-title { font: 1rem/1.45 var(--font-serif); overflow-wrap: anywhere; }
.cand-meta { color: var(--muted); font-size: .8rem; margin-top: 2px; overflow-wrap: anywhere; }
.choose-btn { flex: none; }
.hint { font-size: .8rem; margin: 8px 0 0; }
@media (max-width: 500px) {
  .candidate { flex-direction: column; align-items: stretch; }
}

/* Accounts: login screen, user menu, dialogs, text inputs. */
input[type="text"], input[type="password"], input:not([type]), select.field {
  font: inherit;
  width: 100%;
  padding: 8px 10px;
  margin: 4px 0 12px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
}
input[type="text"]:focus, input[type="password"]:focus, input:not([type]):focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: var(--ring);
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; }
.login-card label { font-weight: 600; font-size: .9rem; }
.login-card .primary { margin-top: 4px; }
.login-title { font: 600 1.6rem/1.2 var(--font-serif); margin: 0; }
code { font-family: var(--font-mono); font-size: .9em; }

.top-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.user-menu { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px; }
.user-name { font-size: .85rem; color: var(--muted); margin-right: 4px; }
.button-link {
  display: inline-block;
  color: var(--text);
  text-decoration: none;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: var(--surface);
}
.button-link.small { font-size: .8rem; padding: 3px 10px; }
.button-link:hover { border-color: var(--accent); background: var(--accent-soft); }
@media (max-width: 800px) {
  .top-actions { align-items: flex-start; }
}

dialog {
  width: min(460px, calc(100vw - 32px));
  padding: 0;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.dialog-form { display: flex; flex-direction: column; padding: 20px; }
.dialog-form h2 { margin-bottom: 12px; }
.dialog-form label { font-weight: 600; font-size: .9rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

/* Admin page */
.admin-page main, .admin-top { max-width: 1000px; }
.admin-page { min-height: 100vh; }
.admin-page .sub a { color: var(--accent-ink); }
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.data-table th, .data-table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.data-table th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.row-actions { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; }
.compact-select { width: auto; margin: 0; padding: 3px 8px; }
.inline-form { display: grid; grid-template-columns: 1fr 1fr auto auto; gap: 8px; align-items: center; margin-top: 16px; }
.inline-form h3 { grid-column: 1 / -1; margin: 0; font-size: .95rem; }
.inline-form input, .inline-form select { margin: 0; }
@media (max-width: 700px) { .inline-form { grid-template-columns: 1fr; } }

/* Reference editor */
dialog:has(.wide) { width: min(720px, calc(100vw - 32px)); }
.ref-grid { display: grid; grid-template-columns: 11em 1fr; gap: 6px 12px; align-items: center; }
.ref-grid label { font-weight: 600; font-size: .85rem; }
.ref-grid input, .ref-grid select, .ref-grid textarea { margin: 0; }
.ref-grid textarea, .dialog-form textarea { margin-top: 0; }
.mono { font-family: var(--font-mono); }
@media (max-width: 600px) { .ref-grid { grid-template-columns: 1fr; } }
.data-table td { white-space: pre-line; overflow-wrap: anywhere; }
.admin-page .hint { margin: 0 0 10px; }
