/* Manuscript — calm, typography-first styles. No external fonts: system fonts
   (with Japanese fallbacks) so the app looks right offline. */

:root {
  --bg: #f6f4ef;
  --surface: #fffdf9;
  --surface-2: #efece5;
  --ink: #23211d;
  --ink-2: #5e5a52;
  --ink-3: #8b867c;
  --line: #e2ddd3;
  --line-2: #d3cdc1;
  --accent: #3f5f8a;
  --accent-ink: #ffffff;
  --accent-soft: #e4eaf3;
  --good: #3d7a52;
  --warn: #9a6512;
  --warn-soft: #f8edd9;
  --bad: #a3392f;
  --bad-soft: #f7e3e0;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(30, 25, 15, .06), 0 6px 24px rgba(30, 25, 15, .06);
  --font-ui: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", "Noto Sans JP", "Noto Sans CJK JP", sans-serif;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", "Noto Serif JP", "Noto Serif CJK JP", serif;
  --font-sans: var(--font-ui);
  --editor-font: var(--font-serif);
  --editor-size: 19px;
  --editor-width: 720px;
  --vvh: 100dvh;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1b1a18; --surface: #22211e; --surface-2: #2b2a26; --ink: #e9e5dc; --ink-2: #b6b0a4; --ink-3: #8a857b;
    --line: #34322d; --line-2: #45423b; --accent: #8fb0dc; --accent-ink: #14202f; --accent-soft: #263243;
    --good: #7cc495; --warn: #e0b060; --warn-soft: #3a3020; --bad: #ec8b80; --bad-soft: #3d2522;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 28px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1b1a18; --surface: #22211e; --surface-2: #2b2a26; --ink: #e9e5dc; --ink-2: #b6b0a4; --ink-3: #8a857b;
  --line: #34322d; --line-2: #45423b; --accent: #8fb0dc; --accent-ink: #14202f; --accent-soft: #263243;
  --good: #7cc495; --warn: #e0b060; --warn-soft: #3a3020; --bad: #ec8b80; --bad-soft: #3d2522;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 28px rgba(0, 0, 0, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: anywhere;
}
:lang(ja) { line-break: strict; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { font-weight: 600; letter-spacing: -.01em; margin: 0; }
.boot { padding: 48px 16px; color: var(--ink-3); text-align: center; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.tiny { font-size: 12px; color: var(--ink-3); }
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
  min-height: 38px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line-2);
  background: var(--surface); color: var(--ink); cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible, .icon-btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .nav-item:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: 8px;
  border: 0; background: transparent; color: var(--ink-2); cursor: pointer; flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn svg, .btn svg { width: 18px; height: 18px; }
label.field { display: grid; gap: 6px; margin-bottom: 14px; }
label.field > span { font-size: 13px; color: var(--ink-2); font-weight: 500; }
input[type=text], input[type=email], input[type=password], input[type=url], select, textarea.input {
  width: 100%; min-height: 40px; padding: 8px 11px; border-radius: 8px; border: 1px solid var(--line-2);
  background: var(--surface); color: var(--ink);
}
textarea.input { min-height: 80px; resize: vertical; }
.check { display: flex; gap: 8px; align-items: center; margin: 8px 0; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  height: 52px; padding: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.topbar .brand { font-family: var(--font-serif); font-size: 18px; font-weight: 600; text-decoration: none; color: var(--ink); }
.topbar .spacer { flex: 1; }
.pill {
  display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 10px; border-radius: 99px;
  font-size: 12.5px; color: var(--ink-2); background: var(--surface-2); border: 0; cursor: pointer; white-space: nowrap;
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ink-3); flex: none; }
.pill.good .dot { background: var(--good); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.warn .dot { background: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.bad .dot { background: var(--bad); }
.pill.busy .dot { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .pill.busy .dot { animation: none; } }

/* ---------- Pages ---------- */
.page { max-width: 880px; margin: 0 auto; padding: 28px max(16px, env(safe-area-inset-left)) 80px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { font-family: var(--font-serif); font-size: 28px; }
.section { margin: 28px 0; }
.section > h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); margin-bottom: 10px; font-weight: 600; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.card + .card { margin-top: 10px; }
.banner {
  display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; flex-wrap: wrap;
  padding: 12px 14px; border-radius: var(--radius); margin-bottom: 12px; background: var(--accent-soft);
}
.banner.warn { background: var(--warn-soft); }
.banner.bad { background: var(--bad-soft); }
.banner p { margin: 0; flex: 1 1 260px; }
.continue {
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 20px 22px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow);
}
.continue .t { font-family: var(--font-serif); font-size: 21px; font-weight: 600; }
.project-list { display: grid; gap: 10px; }
.project-row {
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); text-decoration: none; color: inherit;
}
.project-row:hover { border-color: var(--line-2); }
.project-row .main { flex: 1; min-width: 0; }
.project-row .name { font-weight: 600; font-size: 16px; }
.project-row .meta { font-size: 13px; color: var(--ink-3); }
.tag { display: inline-block; font-size: 11.5px; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); color: var(--ink-2); margin-left: 6px; vertical-align: 1px; }
.empty { text-align: center; padding: 48px 16px; color: var(--ink-2); }
.empty h2 { font-family: var(--font-serif); font-size: 24px; color: var(--ink); margin-bottom: 8px; }
.kv { display: grid; grid-template-columns: minmax(140px, 220px) 1fr; gap: 10px 16px; align-items: baseline; }
.kv dt { color: var(--ink-2); font-size: 13.5px; }
.kv dd { margin: 0; }
.status-good { color: var(--good); font-weight: 600; }
.status-warn { color: var(--warn); font-weight: 600; }
.status-bad { color: var(--bad); font-weight: 600; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

/* ---------- Writing view ---------- */
.writer { display: grid; grid-template-columns: 280px 1fr; height: calc(var(--vvh) - 52px); }
.writer.nav-closed { grid-template-columns: 0 1fr; }
.navigator {
  border-right: 1px solid var(--line); overflow-y: auto; background: var(--bg); padding: 10px 8px 40px;
  overscroll-behavior: contain;
}
.writer.nav-closed .navigator { visibility: hidden; border: 0; }
.nav-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 10px; }
.nav-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); }
.chapter { margin-bottom: 6px; }
.chapter-row, .scene-row { display: flex; align-items: center; gap: 2px; border-radius: 8px; }
.chapter-row:hover, .scene-row:hover { background: var(--surface-2); }
.nav-item {
  flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; padding: 7px 8px; border-radius: 8px;
  cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink);
}
.chapter-row .nav-item { font-weight: 600; }
.scene-row { margin-left: 14px; }
.scene-row .nav-item { color: var(--ink-2); font-size: 14px; }
.scene-row.active { background: var(--accent-soft); }
.scene-row.active .nav-item { color: var(--ink); font-weight: 500; }
.scene-row.conflict .nav-item { color: var(--warn); }
.row-menu { width: 30px; height: 30px; opacity: .55; }
.chapter-row:hover .row-menu, .scene-row:hover .row-menu, .scene-row.active .row-menu, .row-menu:focus-visible { opacity: 1; }
.done-mark { color: var(--good); font-size: 12px; margin-right: 2px; }
.add-scene { margin-left: 14px; font-size: 13px; color: var(--ink-3); }
.editor-pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--surface); }
.editor-head {
  display: flex; align-items: center; gap: 8px; width: 100%; max-width: calc(var(--editor-width) + 48px);
  margin: 0 auto; padding: 14px 24px 4px;
}
.scene-title {
  flex: 1; min-width: 0; border: 0; background: transparent; font-family: var(--editor-font); font-size: 15px;
  color: var(--ink-2); padding: 6px 0; border-bottom: 1px solid transparent;
}
.scene-title:hover, .scene-title:focus { border-bottom-color: var(--line-2); outline: none; }
.editor-banner { max-width: calc(var(--editor-width) + 48px); width: 100%; margin: 0 auto; padding: 0 24px; }
.editor-banner .banner { margin: 6px 0; }
.editor-wrap { flex: 1; min-height: 0; display: flex; }
textarea.manuscript {
  flex: 1; width: 100%; height: 100%; border: 0; resize: none; outline: none; background: transparent;
  font-family: var(--editor-font); font-size: var(--editor-size); line-height: 1.75; color: var(--ink);
  padding: 18px max(24px, calc((100% - var(--editor-width)) / 2)) 45vh;
  tab-size: 4; caret-color: var(--accent); overscroll-behavior: contain;
}
textarea.manuscript:lang(ja) { line-height: 1.95; letter-spacing: .02em; }
textarea.manuscript[readonly] { color: var(--ink-2); }
textarea.manuscript::placeholder { color: var(--ink-3); font-style: italic; }
.editor-foot {
  display: flex; align-items: center; gap: 12px; justify-content: space-between; flex-wrap: wrap;
  padding: 6px 16px calc(6px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line);
  font-size: 12.5px; color: var(--ink-3); background: var(--surface);
}
.save-state { display: inline-flex; align-items: center; gap: 6px; }
.save-state .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--good); }
.save-state.saving .dot { background: var(--accent); }
.save-state.error { color: var(--bad); font-weight: 600; }
.save-state.error .dot { background: var(--bad); }

/* Notes panel beside the editor */
.notes-panel { display: none; }
.writer.notes-open { grid-template-columns: 280px 1fr 320px; }
.writer.nav-closed.notes-open { grid-template-columns: 0 1fr 320px; }
.writer.notes-open .notes-panel {
  display: flex; flex-direction: column; gap: 4px; border-left: 1px solid var(--line); background: var(--bg);
  padding: 12px 14px 24px; overflow-y: auto; min-height: 0;
}
.notes-panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.notes-panel-head h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); }
.note-box {
  width: 100%; min-height: 160px; resize: vertical; border: 1px solid var(--line-2); border-radius: 8px; padding: 10px;
  background: var(--surface); font-family: var(--editor-font); font-size: 15px; line-height: 1.7;
}
.nav-tools { display: flex; gap: 4px; padding: 0 0 8px; margin-bottom: 4px; border-bottom: 1px solid var(--line); }
.nav-tools .btn { flex: 1; min-height: 34px; font-size: 13.5px; }
body.focus .notes-panel { display: none !important; }
body.focus .writer.notes-open { grid-template-columns: 0 1fr; }

/* Notes screen */
.notes-page { display: grid; grid-template-columns: 320px 1fr; height: calc(var(--vvh) - 52px); }
.notes-list { border-right: 1px solid var(--line); overflow-y: auto; padding: 12px 10px 40px; }
.notes-list-head { display: flex; gap: 8px; margin-bottom: 10px; }
.notes-list-head select { flex: 1; min-width: 0; }
.note-item { display: block; padding: 9px 12px; border-radius: 8px; text-decoration: none; color: var(--ink); }
.note-item:hover { background: var(--surface-2); }
.note-item.active { background: var(--accent-soft); }
.note-item-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.note-editor { display: flex; flex-direction: column; min-height: 0; background: var(--surface); padding: 16px 24px; }
.note-editor-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
.note-title { flex: 1; min-width: 180px; border: 0; border-bottom: 1px solid var(--line-2); background: transparent; font-family: var(--editor-font); font-size: 20px; padding: 6px 0; }
.note-title:focus { outline: none; border-bottom-color: var(--accent); }
.note-editor-head select { width: auto; }
.note-attached { font-weight: 600; flex: 1; }
.note-attached a { font-weight: 400; margin-left: 8px; }
.note-body { flex: 1; min-height: 200px; width: 100%; border: 0; resize: none; outline: none; background: transparent;
  font-family: var(--editor-font); font-size: 17px; line-height: 1.8; color: var(--ink); padding: 8px 0; }
.note-body:lang(ja) { line-height: 1.95; }
.note-body:focus-visible { outline: none; }
.note-foot { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--line); padding-top: 8px; font-size: 12.5px; color: var(--ink-3); }

/* Search */
.search-box input { width: 100%; min-height: 48px; font-size: 18px; padding: 10px 14px; border-radius: 10px; border: 1px solid var(--line-2); background: var(--surface); }
.result { display: block; text-decoration: none; color: inherit; }
.result-head { margin-bottom: 6px; }
.snippet-link { display: block; text-decoration: none; color: inherit; border-radius: 6px; padding: 4px 6px; margin: 0 -6px; }
.snippet-link:hover { background: var(--surface-2); }
.snippet { font-family: var(--font-serif); color: var(--ink-2); line-height: 1.7; }
mark { background: color-mix(in srgb, var(--warn) 30%, transparent); color: var(--ink); border-radius: 3px; padding: 0 1px; }

/* Focus mode: only the words */
body.focus .topbar, body.focus .navigator, body.focus .editor-head .hide-in-focus { display: none; }
body.focus .writer { grid-template-columns: 0 1fr; height: var(--vvh); }
body.focus .editor-foot { opacity: .35; transition: opacity .2s; }
body.focus .editor-foot:hover { opacity: 1; }
.focus-exit { display: none; }
body.focus .focus-exit { display: inline-grid; }

/* ---------- Dialogs ---------- */
dialog {
  border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--ink);
  width: min(520px, calc(100vw - 24px)); box-shadow: var(--shadow); max-height: calc(var(--vvh) - 24px);
}
dialog::backdrop { background: rgba(20, 18, 14, .35); }
.dlg { padding: 20px 20px 16px; }
.dlg h2 { font-size: 18px; margin-bottom: 14px; }
.dlg p { margin: 0 0 12px; }
.dlg .buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.sheet-list { display: grid; gap: 2px; }
.sheet-list button {
  text-align: left; border: 0; background: transparent; padding: 11px 12px; border-radius: 8px; cursor: pointer; font-size: 15px;
}
.sheet-list button:hover { background: var(--surface-2); }
.sheet-list button.danger { color: var(--bad); }
.sheet-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.snapshots { max-height: 50vh; overflow: auto; display: grid; gap: 8px; }
.snapshot { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; }
.snapshot pre { white-space: pre-wrap; font-family: var(--editor-font); font-size: 14px; max-height: 120px; overflow: hidden; margin: 6px 0; color: var(--ink-2); }

/* ---------- Export ---------- */
.export-grid { display: grid; grid-template-columns: 300px 1fr; gap: 20px; align-items: start; }
.preview {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 32px 36px;
  max-height: 70vh; overflow: auto; font-family: var(--font-serif); line-height: 1.8;
}
.preview h1 { font-size: 26px; text-align: center; margin: 8px 0 28px; }
.preview h2 { font-size: 20px; margin: 28px 0 12px; }
.preview h3 { font-size: 16px; margin: 18px 0 8px; }
.preview p { margin: 0 0 .6em; }
.preview .ms-break { text-align: center; color: var(--ink-3); margin: 1em 0; }
.chapter-picks { max-height: 260px; overflow: auto; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }

/* ---------- Toasts ---------- */
#toasts { position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 100; display: grid; gap: 8px; width: min(520px, calc(100vw - 24px)); }
.toast { background: var(--ink); color: var(--bg); padding: 11px 14px; border-radius: 10px; box-shadow: var(--shadow); display: flex; gap: 10px; align-items: center; justify-content: space-between; }
.toast button { background: transparent; border: 1px solid currentColor; color: inherit; border-radius: 6px; padding: 4px 10px; cursor: pointer; }

/* ---------- Small screens ---------- */
@media (max-width: 1100px) {
  .writer.notes-open, .writer.nav-closed.notes-open { grid-template-columns: 280px 1fr; }
  .writer.nav-closed.notes-open { grid-template-columns: 0 1fr; }
  .writer.notes-open .notes-panel {
    position: fixed; z-index: 30; top: 52px; right: 0; bottom: 0; width: min(360px, 92vw); box-shadow: var(--shadow);
  }
}
@media (max-width: 760px) {
  .writer, .writer.nav-closed, .writer.notes-open, .writer.nav-closed.notes-open { grid-template-columns: 1fr; }
  .notes-page { grid-template-columns: 1fr; }
  .notes-list { border-right: 0; }
  .note-editor { padding: 12px 16px; }
  .navigator {
    position: fixed; z-index: 30; top: 52px; left: 0; bottom: 0; width: min(320px, 86vw);
    box-shadow: var(--shadow); transform: translateX(-105%); transition: transform .2s ease; visibility: visible;
    background: var(--bg);
  }
  .writer.nav-open .navigator { transform: none; }
  .writer.nav-closed .navigator { visibility: visible; }
  .nav-scrim { position: fixed; inset: 52px 0 0 0; z-index: 25; background: rgba(20, 18, 14, .3); }
  .editor-head { padding: 10px 16px 0; }
  .editor-banner { padding: 0 16px; }
  textarea.manuscript { padding: 12px 18px 40vh; font-size: max(17px, calc(var(--editor-size) - 1px)); }
  .export-grid { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; gap: 2px 0; }
  .kv dd { margin-bottom: 10px; }
  .page-head h1 { font-size: 24px; }
  .hide-mobile { display: none !important; }
  .preview { padding: 20px; }
}
@media (min-width: 761px) { .nav-scrim, .show-mobile { display: none !important; } }

@media (max-width: 480px) {
  .writer-bar .pill { padding: 0 9px; }
  .writer-bar .pill .label { display: none; } /* just the coloured dot; tap it for details */
}

/* ---------- Printing (PDF export) ---------- */
@media print {
  body * { visibility: hidden; }
  #print-root, #print-root * { visibility: visible; }
  #print-root { position: absolute; inset: 0; padding: 0; border: 0; max-height: none; overflow: visible; background: #fff; color: #000; }
  #print-root h2 { break-before: page; }
  #print-root section:first-of-type h2 { break-before: auto; }
  @page { margin: 22mm 20mm; }
}
