:root {
  color-scheme: dark;
  --surface-desk: #13161c;
  --surface-bar: #1a1f28;
  --surface-panel: #171b23;
  --surface-card: #1f2530;
  --surface-hover: #28303e;
  --border-subtle: rgba(226, 232, 240, 0.11);
  --border-strong: rgba(226, 232, 240, 0.22);
  --text-primary: #eef1f6;
  --text-secondary: #a2aebf;
  --text-muted: #707d91;
  --accent-warm: #d4944c;
  --accent-warm-soft: rgba(212, 148, 76, 0.16);
  --accent-ink: #c84b31;
  --accent-teal: #3f9f94;
  --paper-shadow: 0 20px 54px rgba(0, 0, 0, 0.56), 0 4px 14px rgba(0, 0, 0, 0.36);

  /* Syntax highlight palette */
  --syn-block: #e8b36b;
  --syn-keyword: #8ab4dc;
  --syn-dialogue: #df826e;
  --syn-actor: #78c2ad;
  --syn-bracket: #c6b694;
  --syn-string: #d7e2b6;
  --syn-number: #a9c6e8;
  --syn-operator: #e5a35e;
  --syn-at: #92c8c2;
  --syn-comment: #6c7789;
}

* {
  box-sizing: border-box;
}

html,
body,
#root {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--surface-desk);
  color: var(--text-primary);
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", "Noto Sans JP", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.manga-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(ellipse at 50% 45%, rgba(255, 250, 240, 0.03), transparent 68%),
    var(--surface-desk);
}

/* Top Toolbar */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 14px;
  background: var(--surface-bar);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 20;
  transition: transform 180ms ease, opacity 180ms ease;
  flex-wrap: wrap;
}

.manga-shell.chrome-hidden .topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  transform: translateY(-100%);
  opacity: 0;
  pointer-events: none;
}

.topbar-left,
.topbar-center,
.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.brand-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 4px 0 0;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--accent-warm-soft);
  border: 1px solid rgba(212, 148, 76, 0.42);
  color: var(--accent-warm);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  letter-spacing: 0.12em;
}

.doc-title {
  max-width: 170px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text-primary);
}

.select-pill {
  height: 30px;
  max-width: 210px;
  padding: 0 8px;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-card);
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
}

.tool-btn,
.mini-btn,
.nav-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 30px;
  padding: 0 10px;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-card);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
  white-space: nowrap;
}

.tool-btn:hover,
.mini-btn:hover,
.nav-pill:hover:not(:disabled) {
  background: var(--surface-hover);
  color: var(--text-primary);
  border-color: var(--border-strong);
}

.tool-btn.active {
  background: var(--accent-warm-soft);
  color: #f4d3a1;
  border-color: rgba(212, 148, 76, 0.55);
}

.accent-btn,
.accent-mini {
  background: #2b3545;
  color: #f4e6d0;
  border-color: rgba(212, 148, 76, 0.48);
}

.accent-btn:hover,
.accent-mini:hover {
  background: #364357;
  border-color: rgba(212, 148, 76, 0.72);
}

.mini-btn {
  height: 27px;
  padding: 0 9px;
  font-size: 11.5px;
}

.nav-pill:disabled,
.mini-btn:disabled {
  opacity: 0.36;
  cursor: default;
}

.segmented {
  display: inline-flex;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  overflow: hidden;
  background: var(--surface-panel);
}

.segmented button {
  height: 28px;
  padding: 0 9px;
  border: none;
  border-right: 1px solid var(--border-subtle);
  background: transparent;
  color: var(--text-secondary);
  font-size: 11.5px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}

.segmented button:last-child {
  border-right: none;
}

.segmented button:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.segmented button.active {
  background: var(--accent-warm-soft);
  color: #f4d3a1;
  font-weight: 600;
}

.divider {
  width: 1px;
  height: 18px;
  background: var(--border-subtle);
  margin: 0 2px;
}

.count-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 17px;
  height: 17px;
  padding: 0 4px;
  border-radius: 999px;
  background: rgba(200, 75, 49, 0.25);
  color: #f4b2a4;
  font-size: 10.5px;
  font-weight: 700;
}

/* Main Workspace */
.workspace-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  position: relative;
}

/* Manga Spread Stage */
.stage-viewport {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  user-select: none;
  cursor: pointer;
}

.stage-viewport.zoomed {
  overflow: auto;
  align-items: flex-start;
  justify-content: flex-start;
  padding: 20px;
  cursor: grab;
}

.book-frame {
  position: relative;
  display: flex;
  align-items: stretch;
  justify-content: center;
  box-shadow: var(--paper-shadow);
  background: #faf9f5;
  border-radius: 2px;
  overflow: hidden;
  transition: width 140ms ease, height 140ms ease;
  flex: 0 0 auto;
}

.page-surface {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: stretch;
  justify-content: stretch;
}

.book-frame.is-spread .page-left,
.book-frame.is-spread .page-right {
  width: 50%;
  height: 100%;
}

.page-surface svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* Physical book gutter/spine shadow down the center of 2-page spreads */
.spine-crease {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 34px;
  transform: translateX(-50%);
  pointer-events: none;
  background: linear-gradient(
    90deg,
    rgba(0, 0, 0, 0) 0%,
    rgba(0, 0, 0, 0.08) 34%,
    rgba(0, 0, 0, 0.22) 50%,
    rgba(0, 0, 0, 0.08) 66%,
    rgba(0, 0, 0, 0) 100%
  );
}

/* Edge Turn Hover Buttons */
.edge-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 88px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: rgba(20, 24, 32, 0.34);
  color: rgba(240, 244, 250, 0.72);
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  opacity: 0.45;
  transition: opacity 150ms ease, background 150ms ease, border-color 150ms ease;
}

.edge-nav:hover:not(:disabled) {
  opacity: 1;
  background: rgba(20, 24, 32, 0.82);
  border-color: var(--border-strong);
  color: #ffffff;
}

.edge-nav:disabled {
  opacity: 0.08;
  cursor: default;
}

.edge-left {
  left: 12px;
}

.edge-right {
  right: 12px;
}

/* Side Drawer (Syntax-Highlighted DSL + Review Inspector + Daiwari) */
.side-drawer {
  flex: 0 0 400px;
  width: 400px;
  max-width: 400px;
  min-width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--surface-panel);
  border-left: 1px solid var(--border-subtle);
  z-index: 15;
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-subtle);
  background: var(--surface-bar);
}

.drawer-tabs {
  display: flex;
  gap: 4px;
}

.drawer-tabs button {
  height: 28px;
  padding: 0 10px;
  border-radius: 5px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
}

.drawer-tabs button.active {
  background: var(--surface-card);
  border-color: var(--border-strong);
  color: var(--text-primary);
  font-weight: 600;
}

.icon-close {
  width: 28px;
  height: 28px;
  border-radius: 5px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
  line-height: 1;
}

.icon-close:hover {
  background: var(--surface-hover);
  color: var(--text-primary);
}

.drawer-content {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.drawer-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* Read-only Syntax-Highlighted DSL Viewer */
.code-viewer {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: #101319;
  padding: 8px 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  line-height: 1.58;
}

.code-line {
  display: flex;
  padding: 0 12px 0 6px;
  cursor: pointer;
  white-space: pre;
}

.code-line:hover {
  background: rgba(255, 255, 255, 0.04);
}

.code-line.is-active-page {
  background: rgba(212, 148, 76, 0.1);
  border-left: 2px solid var(--accent-warm);
  padding-left: 4px;
}

.line-no {
  flex: 0 0 34px;
  text-align: right;
  padding-right: 10px;
  color: var(--text-muted);
  user-select: none;
  font-size: 11px;
}

.line-code {
  flex: 1 1 auto;
}

.tok-block {
  color: var(--syn-block);
  font-weight: 700;
}
.tok-keyword {
  color: var(--syn-keyword);
}
.tok-dialogue {
  color: var(--syn-dialogue);
  font-weight: 600;
}
.tok-actor {
  color: var(--syn-actor);
  font-weight: 600;
}
.tok-bracket {
  color: var(--syn-bracket);
}
.tok-string {
  color: var(--syn-string);
}
.tok-number {
  color: var(--syn-number);
}
.tok-operator {
  color: var(--syn-operator);
  font-weight: 700;
}
.tok-at {
  color: var(--syn-at);
}
.tok-comment {
  color: var(--syn-comment);
  font-style: italic;
}

.diag-box {
  border-radius: 6px;
  border: 1px solid rgba(212, 148, 76, 0.35);
  background: rgba(212, 148, 76, 0.08);
  padding: 8px 10px;
  font-size: 11.5px;
}

.diag-title {
  font-weight: 700;
  color: #f0cf99;
  margin-bottom: 4px;
}

.diag-item {
  display: flex;
  gap: 6px;
  margin-top: 3px;
  color: var(--text-secondary);
}

.diag-line {
  font-family: ui-monospace, monospace;
  color: var(--accent-warm);
}

/* Review & Intent Inspector */
.inspector-section {
  background: var(--surface-card);
  border: 1px solid var(--border-subtle);
  border-radius: 8px;
  padding: 12px;
}

.inspector-section h3 {
  margin: 0 0 8px;
  font-size: 12.5px;
  font-weight: 700;
  color: #e6ebf4;
  letter-spacing: 0.03em;
}

.meta-dl {
  margin: 0 0 10px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 10px;
  font-size: 12px;
}

.meta-dl dt {
  color: var(--text-muted);
}

.meta-dl dd {
  margin: 0;
  color: var(--text-primary);
}

.stat-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.stat-chip {
  background: var(--surface-panel);
  border: 1px solid var(--border-subtle);
  border-radius: 6px;
  padding: 6px 4px;
  text-align: center;
}

.stat-val {
  display: block;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  font-family: ui-monospace, monospace;
}

.stat-key {
  font-size: 10px;
  color: var(--text-muted);
}

.relation-graph-svg {
  width: 100%;
  height: auto;
  display: block;
  background: #131720;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  margin-bottom: 10px;
}

.char-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.char-card {
  padding: 7px 9px;
  border-radius: 6px;
  background: var(--surface-panel);
  border: 1px solid var(--border-subtle);
  font-size: 11.5px;
}

.char-card-head {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 3px;
}

.char-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.char-id {
  font-family: ui-monospace, monospace;
  color: var(--text-muted);
  font-size: 10.5px;
}

.char-arc {
  margin-left: auto;
  padding: 1px 6px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-secondary);
  font-size: 10px;
}

.char-detail {
  margin: 2px 0 0;
  color: var(--text-secondary);
}

.inspector-ok,
.inspector-empty {
  margin: 0;
  font-size: 12px;
  color: var(--text-secondary);
}

.obs-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.obs-card {
  text-align: left;
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-panel);
  color: var(--text-primary);
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease;
}

.obs-card:hover {
  background: var(--surface-hover);
  border-color: var(--border-strong);
}

.obs-warning {
  border-left: 3px solid #d4884c;
}

.obs-info {
  border-left: 3px solid #5c8ec4;
}

.obs-head {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 3px;
}

.obs-page {
  font-family: ui-monospace, monospace;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--accent-warm);
}

.obs-title {
  font-size: 12px;
  font-weight: 600;
}

.obs-detail {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text-secondary);
}

/* Daiwari (Thumbnail Spread Index) */
.daiwari-hint {
  margin: 0;
  font-size: 11.5px;
  color: var(--text-secondary);
}

.daiwari-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.daiwari-card {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-card);
  color: var(--text-primary);
  cursor: pointer;
  transition: border-color 120ms ease;
}

.daiwari-card:hover {
  border-color: var(--border-strong);
}

.daiwari-card.active {
  border-color: var(--accent-warm);
  background: rgba(212, 148, 76, 0.1);
}

.daiwari-thumb {
  width: 100%;
  aspect-ratio: 1.41 / 1;
  background: #faf9f5;
  border-radius: 3px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.daiwari-thumb svg {
  width: 100%;
  height: 100%;
  display: block;
}

.daiwari-meta {
  display: flex;
  justify-content: space-between;
  font-size: 11.5px;
  color: var(--text-secondary);
}

.daiwari-beat {
  font-family: ui-monospace, monospace;
  color: var(--accent-warm);
}

/* Bottom Bar + RTL/LTR Scrubber */
.bottombar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 8px 14px;
  background: var(--surface-bar);
  border-top: 1px solid var(--border-subtle);
  z-index: 20;
  transition: transform 180ms ease, opacity 180ms ease;
}

.manga-shell.chrome-hidden .bottombar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  transform: translateY(100%);
  opacity: 0;
  pointer-events: none;
}

.bottom-left,
.bottom-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.page-counter {
  min-width: 78px;
  text-align: center;
  font-family: ui-monospace, monospace;
  font-size: 12px;
  color: var(--text-primary);
}

.scrubber-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 140px;
  max-width: 560px;
}

.scrubber-end {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

.scrubber-track {
  position: relative;
  flex: 1 1 auto;
  height: 18px;
  display: flex;
  align-items: center;
  cursor: pointer;
}

.scrubber-track::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  height: 4px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
}

.scrubber-fill {
  position: absolute;
  height: 4px;
  border-radius: 999px;
  background: var(--accent-warm);
}

.scrubber-thumb {
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #faf9f5;
  border: 2px solid var(--accent-warm);
  transform: translateX(-50%);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
}

.zoom-group {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.zoom-label {
  min-width: 40px;
  text-align: center;
  font-family: ui-monospace, monospace;
  font-size: 11px;
  color: var(--text-secondary);
}

/* Modal & Drop Overlay */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 14, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}

.modal-card {
  width: min(760px, 100%);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  border-radius: 10px;
  background: var(--surface-bar);
  border: 1px solid var(--border-strong);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.6);
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-head h2 {
  margin: 0;
  font-size: 15px;
}

.modal-desc {
  margin: 0;
  font-size: 12.5px;
  color: var(--text-secondary);
}

.paste-area {
  width: 100%;
  min-height: 320px;
  flex: 1 1 auto;
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--border-subtle);
  background: #0f1218;
  color: var(--text-primary);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.55;
  resize: vertical;
}

.modal-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.modal-foot-right {
  display: flex;
  gap: 8px;
}

.file-label {
  cursor: pointer;
}

.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(12, 16, 22, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 60;
  pointer-events: none;
}

.drop-box {
  padding: 28px 40px;
  border-radius: 12px;
  border: 2px dashed var(--accent-warm);
  background: var(--surface-bar);
  font-size: 16px;
  font-weight: 700;
  color: #f4d3a1;
}

.toast {
  position: fixed;
  bottom: 58px;
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 8px;
  background: rgba(24, 30, 40, 0.94);
  border: 1px solid var(--border-strong);
  color: var(--text-primary);
  font-size: 12.5px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  z-index: 70;
}

.empty-stage {
  color: var(--text-secondary);
  font-size: 14px;
}

.spread-metrics-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.spread-metric-row {
  width: 100%;
  text-align: left;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border-subtle);
  background: var(--surface-panel);
  color: var(--text-primary);
  cursor: pointer;
}

.spread-metric-row:hover {
  background: var(--surface-hover);
  border-color: var(--border-strong);
}

.spread-metric-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 11px;
  color: var(--text-secondary);
  margin-bottom: 5px;
}

.spread-metric-head strong {
  color: var(--text-primary);
  font-family: ui-monospace, monospace;
}

.ink-bar {
  display: flex;
  height: 7px;
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.ink-dark {
  background: #141820;
}

.ink-mid {
  background: #6c7a8f;
}

.ink-white {
  background: #ebe9e1;
}

.turn-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 8px;
}

.turn-chip {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 5px;
  background: var(--surface-panel);
  border: 1px solid var(--border-subtle);
}

.turn-strong {
  border-left: 3px solid var(--accent-teal);
  color: #bce4de;
}

.turn-weak-payoff {
  border-left: 3px solid #d4884c;
  color: #f0cf99;
}

.turn-weak-hook {
  border-left: 3px solid #5c8ec4;
  color: var(--text-secondary);
}

/* Narrow Screen Adaptations */
@media (max-width: 860px) {
  .side-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(400px, 94vw);
    max-width: 94vw;
    box-shadow: -12px 0 40px rgba(0, 0, 0, 0.55);
  }
  .scrubber-end,
  .zoom-group {
    display: none;
  }
  .scrubber-wrap {
    min-width: 64px;
  }
  .bottombar {
    gap: 8px;
    padding: 8px 10px;
    flex-wrap: wrap;
  }
  .edge-nav {
    width: 34px;
    height: 64px;
    font-size: 22px;
  }
}
