:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #efede7;
  --text: #1c2226;
  --muted: #5c6770;
  --border: #e0ddd4;
  --accent: #0f766e;
  --accent-soft: #d9efec;
  --on-accent: #ffffff;
  --danger: #b42318;
  --good: #1a7f4b;
  --c1: #0f766e; --c2: #b45309; --c3: #6d4bd1; --c4: #be185d; --c5: #2563eb; --c6: #65800b;
  --reader-size: 18px;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
}
:root[data-theme="dark"] {
  --bg: #111517;
  --surface: #1a2024;
  --surface-2: #232b30;
  --text: #e8edef;
  --muted: #9aa7ae;
  --border: #2c363b;
  --accent: #46c9b8;
  --accent-soft: #1c3a37;
  --on-accent: #08201d;
  --danger: #ff8a7a;
  --good: #5fd391;
  --c1: #46c9b8; --c2: #f0a44b; --c3: #a58bff; --c4: #f472b6; --c5: #6ea8ff; --c6: #a3c93a;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
.hidden { display: none !important; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

button { font: inherit; color: inherit; cursor: pointer; }
button.primary, button.ghost, button.seg {
  border-radius: 8px; padding: 8px 14px; border: 1px solid transparent; line-height: 1.2;
}
button.primary { background: var(--accent); color: var(--on-accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.primary:disabled { opacity: .5; cursor: default; }
button.ghost { background: transparent; border-color: var(--border); }
button.ghost:hover { background: var(--surface-2); }
button.small { padding: 5px 10px; font-size: 13px; }
button.link { background: none; border: 0; color: var(--accent); text-decoration: underline; padding: 0; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}

input[type=text], input[type=url], input[type=password], textarea, select {
  width: 100%; font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px;
}
textarea { resize: vertical; }

/* header */
.top {
  display: flex; align-items: center; gap: 8px; padding: 10px 20px;
  border-bottom: 1px solid var(--border); background: var(--surface); flex: none;
}
.brand { background: none; border: 0; font-size: 18px; font-weight: 700; color: var(--accent); padding: 4px 0; letter-spacing: -.01em; }

.progress { background: var(--surface); border-bottom: 1px solid var(--border); padding: 8px 20px; flex: none; }
.progress-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 14px; }
.progress-track { height: 5px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: 6px; }
#progressFill { height: 100%; width: 0; background: var(--accent); transition: width .25s; }

main { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.view { flex: 1; min-height: 0; }
#home { overflow: auto; padding: 32px 20px 60px; max-width: 880px; width: 100%; margin: 0 auto; }

.hero h1 { font-size: 34px; line-height: 1.15; margin: 8px 0 10px; letter-spacing: -.02em; }
.hero p { color: var(--muted); font-size: 17px; margin: 0 0 22px; max-width: 60ch; }
.banner { background: var(--accent-soft); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 16px; }

.add-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.add-tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.add-tab { background: transparent; border: 1px solid transparent; border-radius: 8px; padding: 7px 12px; color: var(--muted); }
.add-tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.add-pane { display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }
.add-pane input, .add-pane textarea { width: 100%; }
.dropzone {
  width: 100%; border: 2px dashed var(--border); border-radius: var(--radius); padding: 34px 16px;
  display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; cursor: pointer;
}
.dropzone span { color: var(--muted); font-size: 13px; }
.dropzone:hover, .dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.dropzone:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.section-title { font-size: 18px; margin: 32px 0 12px; }
.library { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.lib-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; gap: 6px; text-align: left; cursor: pointer;
}
.lib-card:hover { border-color: var(--accent); }
.lib-card h3 { margin: 0; font-size: 16px; line-height: 1.3; overflow-wrap: anywhere; }
.lib-card .row { display: flex; align-items: center; justify-content: space-between; margin-top: auto; padding-top: 6px; }
.empty-lib { color: var(--muted); }

/* workspace */
#workspace { display: flex; flex-direction: column; }
.ws-bar { display: flex; align-items: center; gap: 12px; padding: 10px 20px; border-bottom: 1px solid var(--border); background: var(--surface); flex: none; }
.ws-title { display: flex; flex-direction: column; min-width: 0; }
.ws-title strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 46vw; }
.seg-mobile { display: none; gap: 0; }
button.seg { background: var(--surface); border-color: var(--border); border-radius: 0; padding: 6px 12px; }
button.seg:first-child { border-radius: 8px 0 0 8px; }
button.seg:last-child { border-radius: 0 8px 8px 0; margin-left: -1px; }
button.seg.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

.split { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); }
.reader { overflow: auto; border-right: 1px solid var(--border); background: var(--surface); }
.reader-tools { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; gap: 6px; padding: 8px 20px; background: var(--surface); border-bottom: 1px solid var(--border); }
.reader-body { max-width: 68ch; margin: 0 auto; padding: 24px 22px 80px; font: var(--reader-size)/1.7 Georgia, "Times New Roman", serif; }
.reader-body p { margin: 0 0 1em; }
.reader-body h3 { font-family: system-ui, sans-serif; font-size: 1.1em; margin: 1.8em 0 .6em; line-height: 1.3; }

.panel { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.tabs { display: flex; gap: 2px; padding: 8px 12px 0; border-bottom: 1px solid var(--border); overflow-x: auto; flex: none; background: var(--bg); }
.tab { background: transparent; border: 0; border-bottom: 3px solid transparent; padding: 8px 12px; color: var(--muted); white-space: nowrap; position: relative; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }
.tab.done::after { content: ""; position: absolute; top: 9px; right: 3px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); }
.panel-body { flex: 1; overflow: auto; padding: 20px; }

.bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.bar h2 { margin: 0; font-size: 20px; }
.empty, .state { text-align: center; padding: 48px 16px; color: var(--muted); display: flex; flex-direction: column; align-items: center; gap: 14px; }
.empty p { max-width: 46ch; margin: 0; }
.err { color: var(--danger); background: var(--surface); border: 1px solid var(--danger); border-radius: 8px; padding: 10px 12px; text-align: left; max-width: 52ch; }
.spinner { width: 26px; height: 26px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 3s; } }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 12px; }
.card h3 { margin: 0 0 6px; font-size: 16px; }
.seg-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 14px; }
.chip { background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 5px 12px; font-size: 13px; }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.chip:hover:not(.active) { border-color: var(--accent); }

.md p { margin: 0 0 .8em; }
.md ul, .md ol { margin: 0 0 .8em; padding-left: 1.3em; }
.md li { margin-bottom: .3em; }
.md h3, .md h4, .md h5 { margin: 1.1em 0 .4em; line-height: 1.3; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.lead { font-size: 18px; line-height: 1.5; font-weight: 500; }
.keypoints { margin: 0; padding-left: 1.2em; }
.keypoints li { margin-bottom: .45em; }

/* mind map */
.mm-wrap { overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px; max-height: calc(100vh - 230px); min-height: 300px; }
.mm-wrap svg { display: block; }
.mm-wrap path { fill: none; stroke-width: 2.2; opacity: .85; }
.mm-wrap .node rect { fill: var(--surface); stroke: var(--bc); stroke-width: 2; }
.mm-wrap .node text { fill: var(--text); font: 13px system-ui, sans-serif; pointer-events: none; }
.mm-wrap .node.has { cursor: pointer; }
.mm-wrap .node.has:hover rect { fill: var(--surface-2); }
.mm-wrap .node.root rect { fill: var(--accent); stroke: var(--accent); }
.mm-wrap .node.root text { fill: var(--on-accent); font-weight: 700; font-size: 14px; }
.mm-wrap .node .plus { fill: var(--bc); font-weight: 700; font-size: 15px; }

/* diagrams */
.diagram { overflow: auto; text-align: center; padding: 6px 0; max-height: 70vh; }
.diagram svg { max-width: 100%; height: auto; }

/* drag to move around, full screen */
.pan { cursor: grab; }
.pan.grabbing, .pan.grabbing * { cursor: grabbing !important; user-select: none; }
.pan svg { user-select: none; }
.tabs .fs { position: sticky; right: 0; margin: 0 0 4px auto; flex: none; background: var(--bg); align-self: center; }
.panel:fullscreen, .panel.max { background: var(--bg); }
.panel.max { position: fixed; inset: 0; z-index: 40; display: flex; }
.panel:fullscreen .panel-body, .panel.max .panel-body { padding: 24px max(24px, 4vw); }
.panel:fullscreen .mm-wrap, .panel.max .mm-wrap { max-height: calc(100vh - 190px); }
.panel:fullscreen .diagram, .panel.max .diagram { max-height: calc(100vh - 250px); }
.panel:fullscreen .chart-box, .panel.max .chart-box { height: 50vh; }

/* timeline */
.timeline { list-style: none; margin: 0; padding: 0 0 0 18px; border-left: 3px solid var(--border); }
.timeline li { position: relative; padding: 0 0 20px 18px; }
.timeline li::before { content: ""; position: absolute; left: -27px; top: 5px; width: 13px; height: 13px; border-radius: 50%; background: var(--accent); border: 3px solid var(--bg); }
.timeline .when { font-weight: 700; color: var(--accent); font-size: 13px; letter-spacing: .02em; }
.timeline .ttl { font-weight: 600; margin: 1px 0 2px; }

/* charts */
.chart-box { position: relative; height: 280px; }

/* study */
.q-opt { display: block; width: 100%; text-align: left; margin-top: 8px; padding: 9px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.q-opt:hover:not(:disabled) { border-color: var(--accent); }
.q-opt.right { border-color: var(--good); background: color-mix(in srgb, var(--good) 14%, var(--surface)); }
.q-opt.wrong { border-color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, var(--surface)); }
.q-opt:disabled { cursor: default; color: var(--text); }
.q-expl { margin-top: 10px; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; font-size: 14px; }
.score { font-weight: 600; margin-bottom: 12px; }
.flash { perspective: 900px; margin: 6px auto 14px; max-width: 520px; }
.flash-inner { min-height: 220px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; display: flex; align-items: center; justify-content: center; text-align: center; font-size: 19px; cursor: pointer; width: 100%; }
.flash-inner.back { background: var(--accent-soft); font-size: 17px; }
.flash-inner small { display: block; color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.flash-nav { display: flex; justify-content: center; align-items: center; gap: 8px; }

/* glossary */
.term { padding: 10px 0; border-bottom: 1px solid var(--border); }
.term b { color: var(--accent); }

/* chat */
.chat { display: flex; flex-direction: column; height: 100%; min-height: 360px; }
.chat-log { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 10px; padding-bottom: 10px; }
.msg { max-width: 90%; padding: 10px 14px; border-radius: 14px; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--accent); color: var(--on-accent); border-bottom-right-radius: 4px; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-right-radius: 14px; border-bottom-left-radius: 4px; }
.msg.assistant .md > :last-child { margin-bottom: 0; }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding-top: 8px; border-top: 1px solid var(--border); }
.chat-form textarea { flex: 1; }

/* selection bar, dialogs, toast */
.selbar { position: fixed; z-index: 30; display: flex; gap: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 5px; box-shadow: var(--shadow); }
dialog { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; width: min(560px, 92vw); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; font-size: 17px; }
.dlg-actions { display: flex; align-items: center; gap: 8px; margin-top: 16px; }
.field { display: block; margin-bottom: 12px; font-weight: 600; }
.field input, .field select { margin-top: 5px; font-weight: 400; }
blockquote { margin: 0 0 12px; padding: 8px 12px; border-left: 3px solid var(--accent); background: var(--surface-2); border-radius: 0 8px 8px 0; font-family: Georgia, serif; max-height: 140px; overflow: auto; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 50; max-width: 90vw; box-shadow: var(--shadow); }

@media (max-width: 900px) {
  .seg-mobile { display: flex; }
  .split { grid-template-columns: minmax(0, 1fr); }
  .split.show-reader .panel { display: none; }
  .split.show-panel .reader { display: none; }
  .reader { border-right: 0; }
  .ws-title strong { max-width: 30vw; }
  #genAllBtn { display: none; }
  .hero h1 { font-size: 28px; }
  .top, .ws-bar { padding-left: 14px; padding-right: 14px; }
}
