/* AI Narrative World Engine - zero-build UI (DECISIONS D-002).
   Ink-on-paper by day, lamplight by night. */

:root {
  --bg: #f4f1ea;
  --panel: #fffdf8;
  --panel-2: #efeade;
  --ink: #23201b;
  --ink-soft: #5f5849;
  --ink-faint: #8d8474;
  --line: #ddd5c5;
  --accent: #6b7f5e;
  --accent-soft: #e6ebe1;
  --warn: #a8632f;
  --danger: #9c3b34;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, monospace;
  --serif: "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", Georgia, serif;
  --sans: "PingFang SC", "Microsoft YaHei", -apple-system, "Segoe UI", sans-serif;
  --radius: 10px;
  --app-height: 100dvh;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171a;
    --panel: #1d1f23;
    --panel-2: #24272c;
    --ink: #e6e2d9;
    --ink-soft: #a8a396;
    --ink-faint: #7b766a;
    --line: #33373d;
    --accent: #8fa87e;
    --accent-soft: #262c24;
    --warn: #d18a4e;
    --danger: #cc6259;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
}

button, input, textarea { font: inherit; color: inherit; }

/* ---------- layout ---------- */
.app {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 280px;
  grid-template-rows: 1fr auto;
  grid-template-areas: "left main right" "bottom bottom bottom";
  gap: 12px;
  height: var(--app-height);
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}

.col-left   { grid-area: left; }
.col-main   { grid-area: main; }
.col-right  { grid-area: right; }
.bottom     { grid-area: bottom; }

.col-left, .col-right { overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.mobileInfoHeader, .mobileInfoBackdrop, .mobileInfoBtn { display: none; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 12px;
  letter-spacing: .14em;
  color: var(--ink-faint);
  font-weight: 600;
}

/* ---------- narrative ---------- */
.col-main { display: flex; flex-direction: column; min-height: 0; }

#story {
  flex: 1;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 26px 32px;
  font-family: var(--serif);
  font-size: 16.5px;
  line-height: 2;
  scroll-behavior: smooth;
}

.entry { margin-bottom: 26px; }
.entry + .entry { border-top: 1px dashed var(--line); padding-top: 22px; }

.entry .said {
  font-family: var(--sans);
  font-size: 13.5px;
  color: var(--accent);
  margin-bottom: 10px;
  padding-left: 12px;
  border-left: 3px solid var(--accent);
  line-height: 1.7;
}

.entry .prose p { margin: 0 0 .9em; text-indent: 2em; }
.entry .prose { overflow-wrap: anywhere; }
.entry .prose p:last-child { margin-bottom: 0; }

.entry .rejected {
  font-family: var(--sans);
  font-size: 13px;
  color: var(--warn);
  background: var(--panel-2);
  border-radius: 6px;
  padding: 6px 10px;
  margin-top: 10px;
  display: inline-block;
}

.entry .deltas {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-faint);
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.entry .deltas span {
  background: var(--panel-2);
  border-radius: 4px;
  padding: 2px 7px;
}
.entry .deltas span.up { color: var(--accent); }
.entry .deltas span.down { color: var(--danger); }

.cursor::after {
  content: "";
  display: inline-block;
  width: .5em; height: 1em;
  background: var(--accent);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ---------- stats ---------- */
.stat { margin-bottom: 12px; }
.stat:last-child { margin-bottom: 0; }
.stat .label {
  display: flex; justify-content: space-between;
  font-size: 12px; color: var(--ink-soft); margin-bottom: 4px;
}
.stat .label b { font-weight: 600; color: var(--ink); font-variant-numeric: tabular-nums; }

.bar { height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); transition: width .45s ease; }
.bar.hp > i { background: #a3564e; }
.bar.mp > i { background: #4e6f9c; }

.kv { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; padding: 3px 0; }
.kv span:first-child { color: var(--ink-soft); }
.kv span:last-child { text-align: right; }

.tag {
  display: inline-block; font-size: 11px; font-family: var(--mono);
  background: var(--accent-soft); color: var(--accent);
  border-radius: 4px; padding: 2px 6px; margin: 2px 4px 2px 0;
}

.npc { padding: 7px 0; border-bottom: 1px dashed var(--line); }
.npc:last-child { border-bottom: none; }
.npc .name { font-weight: 600; font-size: 13.5px; }
.npc .meta { font-size: 11.5px; color: var(--ink-faint); }

/* ---------- input ---------- */
.bottom {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px 14px;
}

.inputRow { display: flex; gap: 10px; align-items: flex-end; }

.lengthControl {
  color: var(--ink-soft);
  font-size: 12px;
}
.tag.leadTag { background: #efe2d3; color: #8a4f2d; margin-left: 6px; }
.lengthControl label { color: var(--ink-soft); }
.lengthInputs { display: flex; align-items: center; gap: 9px; }
.lengthControl input[type="range"] { accent-color: var(--accent); cursor: pointer; }
.lengthControl input[type="number"] {
  width: 76px;
  padding: 5px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}
.lengthControl small { display: block; margin-top: 5px; color: var(--ink-faint); line-height: 1.5; }
.compactLength {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
.compactLength input[type="range"] { width: min(300px, 34vw); }

#playerInput {
  flex: 1;
  resize: none;
  min-height: 46px;
  max-height: 130px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  line-height: 1.6;
}
#playerInput:focus { outline: none; border-color: var(--accent); }

button.primary {
  padding: 12px 22px;
  border: none; border-radius: 8px;
  background: var(--accent); color: #fff;
  cursor: pointer; font-weight: 600;
}
button.primary:disabled { opacity: .5; cursor: not-allowed; }
button, select, input, textarea { touch-action: manipulation; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.chip {
  font-size: 12.5px; padding: 5px 11px;
  background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 999px; cursor: pointer; color: var(--ink-soft);
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.primary { border-color: var(--accent); color: var(--accent); }

/* always-visible clock above the story */
.nowBar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: baseline; gap: 8px;
  padding: 10px 4px 12px;
  margin-bottom: 6px;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
  font-size: 12.5px; color: var(--ink-soft);
}
.nowBar .phase { font-size: 14px; color: var(--ink); letter-spacing: .06em; }
.nowBar .clock { font-variant-numeric: tabular-nums; color: var(--accent); }
.nowBar .sep { opacity: .4; }
.nowBar .spacer { flex: 1; }
.nowBar .place { opacity: .8; }
.barActions { display: flex; align-items: center; gap: 6px; }

/* shown while a chapter is still being played out */
.prose p.progress { color: var(--ink-soft); opacity: .6; font-style: italic; }

/* "the story ran on for a while" - quiet, never competing with the prose */
.runNote {
  margin-top: 8px; font-size: 11.5px; letter-spacing: .04em;
  color: var(--ink-soft); opacity: .55;
}

/* What the scene is waiting on, in the story's own voice. */
.beatQuestion {
  flex-basis: 100%;
  font-size: 13px; color: var(--ink); opacity: .85;
  margin-bottom: 2px;
}

.tabs { display: flex; gap: 4px; margin-bottom: 10px; flex-wrap: wrap; }
.tabs button {
  font-size: 12px; padding: 4px 10px;
  background: transparent; border: 1px solid var(--line);
  border-radius: 999px; cursor: pointer; color: var(--ink-soft);
}
.tabs button.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ---------- debug ---------- */
#debugPanel {
  position: fixed; inset: auto 0 0 0;
  height: 46vh; background: var(--panel);
  border-top: 2px solid var(--accent);
  display: none; flex-direction: column; z-index: 50;
}
#debugPanel.open { display: flex; }
#debugPanel header {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px; border-bottom: 1px solid var(--line);
  font-size: 12px; letter-spacing: .1em; color: var(--ink-faint);
}
#debugPanel header .spacer { flex: 1; }
#debugBody { flex: 1; overflow: auto; padding: 12px 14px; }

pre.json {
  font-family: var(--mono); font-size: 11.5px; line-height: 1.6;
  white-space: pre-wrap; word-break: break-word; margin: 0;
  color: var(--ink-soft);
}

details.dbg { border: 1px solid var(--line); border-radius: 6px; margin-bottom: 8px; }
details.dbg > summary {
  cursor: pointer; padding: 6px 10px; font-size: 12px;
  font-family: var(--mono); color: var(--ink);
  background: var(--panel-2); border-radius: 6px;
}
details.dbg[open] > summary { border-bottom: 1px solid var(--line); border-radius: 6px 6px 0 0; }
details.dbg > div { padding: 10px; }

.toggleDebug {
  position: fixed; right: 14px; bottom: 14px; z-index: 60;
  border: 1px solid var(--line); background: var(--panel);
  color: var(--ink-soft); border-radius: 999px;
  padding: 7px 14px; font-size: 12px; cursor: pointer;
}

/* ---------- setup ---------- */
#setup {
  position: fixed; inset: 0; z-index: 100;
  background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: max(16px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(16px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
#setup .card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 34px 38px; width: min(440px, 92vw);
}
#setup h1 { margin: 0 0 6px; font-family: var(--serif); font-size: 26px; letter-spacing: .1em; }
#setup .sub { color: var(--ink-faint); font-size: 13px; margin-bottom: 22px; line-height: 1.7; }
#setup label { display: block; font-size: 12px; color: var(--ink-soft); margin: 12px 0 5px; }
#setup input, #setup select {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
#setup button { width: 100%; margin-top: 22px; }

.err { color: var(--danger); font-size: 12.5px; margin-top: 10px; min-height: 1em; }

.muted { color: var(--ink-faint); font-size: 12.5px; }

@media (min-width: 721px) and (max-width: 1080px) {
  .app {
    grid-template-columns: 240px minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr) auto;
    grid-template-areas: "left main" "right main" "bottom bottom";
  }
}

/* save / load / restart, tucked into the clock bar */
.nowBar .barBtn {
  font-size: 11.5px; padding: 3px 9px; margin-left: 6px;
  background: transparent; border: 1px solid var(--line);
  border-radius: 999px; cursor: pointer; color: var(--ink-soft);
}
#setup .lengthControl input[type="range"] { flex: 1; width: auto; padding: 0; }
#setup .lengthControl input[type="number"] { width: 82px; padding: 7px 8px; }
.nowBar .barBtn:hover { border-color: var(--accent); color: var(--accent); }

.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.45);
  display: flex; align-items: center; justify-content: center;
}
.modalCard {
  width: min(560px, 92vw); max-height: 76vh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 16px;
}
.modalCard header { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.modalCard header .spacer { flex: 1; }

.saveRow {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 0; border-bottom: 1px solid var(--line);
}
.saveRow:last-child { border-bottom: none; }
.saveMain { flex: 1; min-width: 0; }
.saveName { font-size: 14px; color: var(--ink); }
.saveMeta { font-size: 11.5px; color: var(--ink-soft); opacity: .7; margin-top: 2px; }
.saveExcerpt {
  font-size: 12px; color: var(--ink-soft); margin-top: 4px; line-height: 1.6;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.saveActions { display: flex; flex-direction: column; gap: 6px; }

/* Offered while the reader has scrolled away from the streaming text. */
#jumpLatest {
  position: absolute; right: 34px; bottom: 22px; z-index: 6;
  font-size: 12.5px; padding: 6px 14px;
  background: var(--accent); color: #fff;
  border: none; border-radius: 999px; cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,.18);
}
.col-main { position: relative; }

/* ---------- phone layout ---------- */
@media (max-width: 720px) {
  :root { --radius: 8px; }

  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "main" "bottom";
    gap: 0;
    width: 100%;
    height: var(--app-height);
    padding: env(safe-area-inset-top) env(safe-area-inset-right)
      env(safe-area-inset-bottom) env(safe-area-inset-left);
  }

  .col-main { min-width: 0; min-height: 0; }

  .nowBar {
    position: relative;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 7px;
    min-height: 58px;
    margin: 0;
    padding: 8px 10px 7px;
    border-bottom: 1px solid var(--line);
  }
  .nowBar .phase { font-size: 13px; }
  .nowBar .spacer { display: none; }
  .nowBar .place {
    order: 3;
    flex: 0 0 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .barActions { order: 2; margin-left: auto; gap: 3px; }
  .nowBar .barBtn {
    min-width: 42px;
    min-height: 36px;
    margin-left: 0;
    padding: 6px 8px;
  }
  .mobileInfoBtn { display: inline-block; }

  #story {
    min-height: 0;
    border-width: 0;
    border-radius: 0;
    padding: 18px 16px 28px;
    font-size: 16px;
    line-height: 1.9;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  .entry { margin-bottom: 22px; }
  .entry + .entry { padding-top: 18px; }
  .entry .prose p { text-indent: 1.75em; }

  .bottom {
    min-width: 0;
    max-height: min(38vh, 260px);
    overflow-y: auto;
    border-width: 1px 0 0;
    border-radius: 0;
    padding: 8px 10px max(8px, env(safe-area-inset-bottom));
    box-shadow: 0 -4px 18px rgba(0, 0, 0, .08);
    overscroll-behavior: contain;
  }
  .compactLength { margin-bottom: 6px; gap: 6px; }
  .compactLength input[type="range"] { display: none; }
  .lengthControl input[type="number"] { width: 70px; min-height: 34px; }
  .inputRow { gap: 8px; align-items: stretch; }
  #playerInput {
    min-width: 0;
    min-height: 46px;
    max-height: 96px;
    padding: 10px 11px;
    font-size: 16px; /* prevents iOS from zooming the page on focus */
  }
  #sendBtn { flex: 0 0 70px; min-height: 46px; padding: 10px 12px; }
  .chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin: 7px -10px 0;
    padding: 0 10px 2px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  .chips::-webkit-scrollbar { display: none; }
  .chips .chip { flex: 0 0 auto; min-height: 38px; padding: 8px 12px; }
  .beatQuestion { position: sticky; left: 10px; flex: 0 0 auto; max-width: 78vw; }

  .mobileInfoBackdrop {
    position: fixed;
    inset: 0;
    z-index: 79;
    width: 100%;
    border: 0;
    background: rgba(0, 0, 0, .46);
  }
  .app.mobile-info-open ~ .mobileInfoBackdrop { display: block; }

  .mobileInfoHeader {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 82;
    width: min(88vw, 390px);
    height: calc(54px + env(safe-area-inset-top));
    padding: max(10px, env(safe-area-inset-top)) 12px 8px;
    align-items: center;
    justify-content: space-between;
    background: var(--panel);
    border-bottom: 1px solid var(--line);
    transform: translateX(105%);
    transition: transform .2s ease;
  }
  .app.mobile-info-open .mobileInfoHeader { display: flex; transform: translateX(0); }

  .col-left, .col-right {
    position: fixed;
    right: 0;
    z-index: 81;
    width: min(88vw, 390px);
    padding: 10px 10px 0;
    background: var(--bg);
    transform: translateX(105%);
    transition: transform .2s ease;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  .col-left {
    top: calc(54px + env(safe-area-inset-top));
    bottom: 43%;
  }
  .col-right {
    top: 57%;
    bottom: 0;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
  }
  .app.mobile-info-open .col-left,
  .app.mobile-info-open .col-right { transform: translateX(0); }

  .modal { align-items: flex-end; padding-top: env(safe-area-inset-top); }
  .modalCard {
    width: 100%;
    max-height: 88dvh;
    border-width: 1px 0 0;
    border-radius: 14px 14px 0 0;
    padding: 14px 12px max(14px, env(safe-area-inset-bottom));
  }
  .saveRow { gap: 7px; }
  .saveActions .chip { min-height: 38px; }

  #jumpLatest { right: 12px; bottom: 12px; min-height: 40px; }
  #debugPanel { height: 76dvh; padding-bottom: env(safe-area-inset-bottom); }
  .toggleDebug { display: none !important; }

  #setup { align-items: flex-start; }
  #setup .card {
    width: 100%;
    min-width: 0;
    max-width: 100%;
    margin: auto 0;
    padding: 24px 20px;
    border-radius: 12px;
  }
  #setup h1 { font-size: 24px; }
  #setup input, #setup select { min-height: 44px; font-size: 16px; }
  #setup button { min-height: 48px; }
  .setupLength .lengthInputs { min-width: 0; gap: 7px; }
  #setup .lengthControl input[type="range"] { min-width: 0; }
  #setup .lengthControl input[type="number"] { flex: 0 0 72px; width: 72px; }
}

@media (max-width: 720px) and (orientation: landscape) {
  .compactLength { display: none; }
  .bottom { max-height: 46vh; }
  .col-left { bottom: 35%; }
  .col-right { top: 65%; }
}

@media (hover: none) {
  .chip:hover, .nowBar .barBtn:hover { border-color: var(--line); color: var(--ink-soft); }
}
