:root {
  --bg: #f4ecdc; --bg-2: #ece1c9; --paper: #fbf6ea; --ink: #2a2118; --muted: #6f6454;
  --line: #d6c8aa; --accent: #9a4a1c; --accent-ink: #fff8ec; --warn: #a3321f; --ok: #3d6b3a;
  --shadow: 0 1px 2px rgba(60, 40, 10, .12), 0 6px 18px rgba(60, 40, 10, .08);
  --base-size: 18px; --radius: 10px;
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, "Noto Serif", serif;
  --font-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --font-body: var(--font-serif);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #17140f; --bg-2: #201c15; --paper: #211d16; --ink: #ece3cf; --muted: #a39881;
    --line: #3a3326; --accent: #d98a4e; --accent-ink: #1a1208; --warn: #ee7b63; --ok: #86b97f;
    --shadow: 0 1px 2px rgba(0,0,0,.5), 0 6px 18px rgba(0,0,0,.35);
  }
}
:root[data-theme="dark"] {
  --bg: #17140f; --bg-2: #201c15; --paper: #211d16; --ink: #ece3cf; --muted: #a39881;
  --line: #3a3326; --accent: #d98a4e; --accent-ink: #1a1208; --warn: #ee7b63; --ok: #86b97f;
  --shadow: 0 1px 2px rgba(0,0,0,.5), 0 6px 18px rgba(0,0,0,.35);
}
:root[data-font="sans"] { --font-body: var(--font-sans); }
:root[data-font="mono"] { --font-body: var(--font-mono); }

* { box-sizing: border-box; }
html { font-size: var(--base-size); }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--font-body); line-height: 1.6;
  min-height: 100vh; display: flex; flex-direction: column; }
main { flex: 1; width: 100%; max-width: 1400px; margin: 0 auto; padding: 1rem 16px 2rem; }
h1, h2, h3 { line-height: 1.25; margin: .6em 0 .4em; font-weight: 600; }
h1 { font-size: 1.6rem; } h2 { font-size: 1.2rem; } h3 { font-size: 1rem; }
a { color: var(--accent); }
.muted { color: var(--muted); } .small { font-size: .82rem; } .center { text-align: center; }
.prewrap, .narr, .act, .epilogue { white-space: pre-wrap; }
.row { display: flex; gap: .6rem; align-items: center; } .row.wrap { flex-wrap: wrap; } .row.between { justify-content: space-between; }
hr { border: 0; border-top: 1px solid var(--line); margin: 1rem 0; }

/* controls */
button, .btn, input, select, textarea { font: inherit; color: inherit; }
button, .btn { cursor: pointer; border: 1px solid var(--line); background: var(--paper); border-radius: var(--radius);
  padding: .45rem .9rem; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
button:hover, .btn:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary.big { font-size: 1.3rem; padding: .8rem 2.5rem; min-height: 56px; }
button.danger { color: var(--warn); border-color: var(--warn); }
button.link { border: 0; background: none; color: var(--accent); padding: .25rem .5rem; min-height: 44px; }
.icon-btn { min-height: 32px; min-width: 32px; padding: 0 .4rem; border: 0; background: none; color: var(--muted); font-size: 1.1rem; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
input[type=text], input[type=email], input[type=password], input[type=search], textarea, select {
  width: 100%; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: .5rem .7rem; min-height: 44px; }
textarea { resize: vertical; line-height: 1.6; }
label { display: block; margin: .6rem 0; font-size: .9rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: .25rem; color: var(--ink); }
label.check { display: flex; gap: .7rem; align-items: flex-start; color: var(--ink); }
label.check input { width: 1.2rem; min-height: 1.2rem; margin-top: .3rem; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); margin: .8rem 0; padding: .6rem .8rem; }
fieldset input { margin: .2rem 0; }
.error { color: var(--warn); } 
.chip { display: inline-flex; align-items: center; gap: .25rem; font-size: .75rem; padding: .05rem .5rem; border: 1px solid var(--line); border-radius: 99px; margin-right: .3rem; }
.chip.warn { color: var(--warn); border-color: var(--warn); }
.chip.dom svg { opacity: .8; }
.row-form { display: flex; gap: .6rem; align-items: flex-end; } .row-form label { flex: 1; }
.addrow { display: flex; gap: .4rem; } .addrow button { flex: none; }

/* shell */
.topbar { display: flex; justify-content: space-between; align-items: center; padding: .3rem 16px; border-bottom: 1px solid var(--line); background: var(--bg-2); }
.brand { font-size: 1.3rem; font-weight: 700; letter-spacing: .08em; text-decoration: none; color: var(--ink); text-transform: uppercase; }
.topbar nav { display: flex; align-items: center; gap: .2rem; flex-wrap: wrap; }
.credit { text-align: center; color: var(--muted); font-size: .75rem; padding: 1rem 16px 1.5rem; }

/* auth */
.auth { max-width: 26rem; margin: 2rem auto; }
.auth .lead { font-size: 1.15rem; }
.tabs { display: flex; gap: .4rem; margin: 1rem 0; } .tabs button { flex: 1; } .tabs button.active { border-color: var(--accent); background: var(--bg-2); font-weight: 600; }
.auth form button { width: 100%; margin-top: .6rem; }

/* stories */
.cards { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr)); gap: 1rem; }
.card { display: block; background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; text-decoration: none; color: var(--ink); box-shadow: var(--shadow); transition: transform .15s; }
.card:hover { transform: translateY(-2px); border-color: var(--accent); }
.card h2 { margin: 0; } .card p { margin: .2rem 0; }
.empty { padding: 3rem 0; text-align: center; }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(20, 12, 4, .55); display: flex; align-items: flex-start; justify-content: center; padding: 3vh 12px; overflow-y: auto; z-index: 50; }
.modal { background: var(--paper); border-radius: var(--radius); width: 100%; max-width: 40rem; box-shadow: var(--shadow); border: 1px solid var(--line); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: .4rem 1rem; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: .3rem 0; }
.modal-body { padding: .5rem 1rem 1.2rem; }
.reflist { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr)); gap: .6rem; }
.reflist li { display: flex; gap: .7rem; align-items: center; }
.helplist li { margin: .5rem 0; }

/* play layout */
.playhead { flex-wrap: wrap; display: flex; align-items: center; gap: .8rem; justify-content: space-between; }
.playhead h1 { margin: 0; flex: 1; font-size: 1.3rem; }
.paneltabs { display: none; }
.play { display: grid; grid-template-columns: minmax(16rem, 22rem) 1fr minmax(14rem, 18rem); gap: 1.2rem; align-items: start; }
.panel { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; box-shadow: var(--shadow); }
.panel.journal { max-height: calc(100vh - 8rem); overflow-y: auto; position: sticky; top: .5rem; }
.panel.stage { padding: 1rem 1.4rem 1.4rem; min-height: 28rem; }
@media (max-width: 1023px) {
  .paneltabs { display: flex; gap: .3rem; margin: .5rem 0; position: sticky; top: 0; background: var(--bg); z-index: 5; padding: .3rem 0; }
  .paneltabs button { flex: 1; } .paneltabs button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
  .play { display: block; } .panel { display: none; } .panel.is-active { display: block; } .panel.journal { max-height: none; position: static; }
}

/* stage */
.steps { display: flex; gap: .4rem; list-style: none; padding: 0; margin: 0 0 .8rem; font-size: .78rem; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.steps li { flex: 1; text-align: center; padding: .25rem; border-bottom: 2px solid var(--line); }
.steps li.on { color: var(--accent); border-color: var(--accent); font-weight: 700; }
.balance { display: flex; align-items: center; gap: .6rem; margin-bottom: .6rem; }
.candles { display: flex; gap: .45rem; }
.candle { width: 10px; height: 22px; border-radius: 3px 3px 2px 2px; background: var(--line); position: relative; border: 1px solid var(--muted); }
.candle.lit { background: var(--paper); border-color: var(--accent); }
.candle.lit::before { content: ""; position: absolute; left: 50%; top: -9px; width: 7px; height: 9px; transform: translateX(-50%); border-radius: 50% 50% 50% 50% / 65% 65% 35% 35%; background: var(--accent); }
.banner { border: 1px solid var(--line); background: var(--bg-2); border-radius: var(--radius); padding: .6rem .9rem; margin: .5rem 0; display: flex; gap: .8rem; align-items: center; flex-wrap: wrap; }
.banner.crisis { border-color: var(--warn); color: var(--ink); flex-direction: column; align-items: stretch; }
.banner.crisis strong { color: var(--warn); }
.cards3 { display: flex; gap: .6rem; justify-content: center; align-items: flex-end; margin: 1rem 0 .3rem; flex-wrap: nowrap; }
.cards3.ghost { opacity: .45; }
.dcard { flex: 1 1 0; min-width: 0; max-width: 8.2rem; flex-direction: column; gap: .15rem; padding: .8rem .4rem .6rem; background: var(--bg); border: 1px solid var(--accent); height: auto; position: relative; }
.dcard.big { flex-grow: 1.3; max-width: 10.5rem; padding-top: 1.2rem; box-shadow: var(--shadow); }
.dcard .dname { font-weight: 600; font-size: .95rem; } .dcard .dnum { position: absolute; top: .25rem; right: .5rem; font-size: .7rem; color: var(--muted); }
.dnum { font-size: .72rem; color: var(--muted); margin-left: .3rem; }
.dcard.ghost { border-color: var(--line); }
.dicon { color: var(--accent); }
@keyframes flip { 0% { transform: rotateY(90deg) translateY(10px); opacity: 0; } 60% { opacity: 1; } 100% { transform: none; opacity: 1; } }
.dcard { animation: flip .55s cubic-bezier(.2,.8,.2,1) both; animation-delay: calc(var(--i) * .28s); }
.dcard.ghost { animation: none; }
:root[data-motion="off"] .dcard { animation: none; }
@media (prefers-reduced-motion: reduce) { :root:not([data-motion="on"]) .dcard { animation: none; } }
.rollmeta { text-align: center; min-height: 1.6rem; margin: 0 0 .5rem; }
.hint { text-align: center; color: var(--muted); font-size: .9rem; margin: .2rem 0 .8rem; }
.ready { text-align: center; padding: 1.5rem 0; }
.write textarea { margin-bottom: .5rem; }
.write .narration { font-size: 1.05rem; min-height: 11rem; }
.narr-quote { margin: .2rem 0 .8rem; padding: .3rem 1rem; border-left: 3px solid var(--accent); color: var(--muted); white-space: pre-wrap; }

/* journal */
.filters { display: grid; gap: .4rem; margin-bottom: .6rem; } .filters select { flex: 1; min-width: 0; }
.entry { padding: .6rem 0 .8rem; border-bottom: 1px solid var(--line); }
.entry.crisis { border-left: 4px solid var(--warn); padding-left: .7rem; }
.entry header { display: flex; flex-wrap: wrap; align-items: center; gap: .2rem .5rem; }
.tnum { font-size: .75rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.entry .narr { margin: .3rem 0; } .entry .act { margin: .2rem 0; font-style: italic; color: var(--muted); }
.chapter { margin: 1rem 0 .2rem; border-bottom: 2px solid var(--accent); color: var(--accent); }

/* avatar */
.avname { font-size: 1.2rem; font-weight: 600; margin: 0; }
.items { list-style: none; padding: 0; margin: 0 0 .5rem; }
.items li { display: flex; align-items: center; gap: .4rem; padding: .15rem 0; border-bottom: 1px dashed var(--line); }
.items li > span { flex: 1; } .items li.off .toolname { text-decoration: line-through; color: var(--muted); }
.items select { width: auto; min-height: 32px; padding: .1rem .3rem; font-size: .8rem; }

@media print {
  .topbar, .credit, .paneltabs, .modal-head button, .playhead .row, button { display: none !important; }
  body { background: #fff; color: #000; } .modal-backdrop { position: static; background: none; display: block; padding: 0; }
  .modal { box-shadow: none; border: 0; max-width: none; } .panel { display: none; }
  .entry { break-inside: avoid; }
}

.dcard .dname { overflow-wrap: anywhere; text-align: center; }
@media (max-width: 520px) { .dcard { padding: .6rem .2rem .5rem; } .dcard .dname { font-size: .8rem; } .dicon { width: 40px; height: 40px; } .panel.stage { padding: .8rem; } }

.dot { width: .6rem; height: .6rem; border-radius: 50%; background: var(--line); flex: none; }
.dot.on { background: var(--ok); }
button.xcard { color: var(--warn); border-color: var(--warn); font-weight: 600; }
.members li > span:nth-child(2) { flex: 1; }
.lobby label { margin: .8rem 0; }
@media (max-width: 520px) { .topbar { flex-wrap: wrap; } .topbar nav { gap: 0; } }
.dcard .dname { overflow-wrap: break-word; hyphens: auto; }
@media (max-width: 520px) { .dcard .dname { font-size: .72rem; } }
.items li > .dot { flex: none; width: .6rem; height: .6rem; }
