:root {
  --bg: #14110f;
  --panel: #1b1815;
  --panel-2: #221e1a;
  --line: #322c26;
  --line-soft: #262119;
  --text: #ece4d8;
  --muted: #9b9086;
  --faint: #6d645b;
  --accent: #e0a458;
  --accent-dim: #8a6534;
  --win: #86c06c;
  --danger: #d4735e;
  --mono: "iA Writer Mono", ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

body {
  background-image:
    radial-gradient(900px 500px at 15% -10%, rgba(224, 164, 88, 0.07), transparent 60%),
    radial-gradient(700px 400px at 90% 110%, rgba(134, 192, 108, 0.05), transparent 60%);
}

button, input, textarea { font-family: inherit; font-size: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }

.hidden { display: none !important; }

/* --------------------------------------------------------------- login -- */

.gate {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.gate-card {
  width: min(440px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 32px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
}

.gate-card h1 {
  font-family: var(--mono);
  font-size: 26px;
  margin: 0 0 6px;
  letter-spacing: -0.5px;
}

.gate-card .tag { color: var(--muted); margin: 0 0 24px; line-height: 1.5; }

.gate-card label {
  display: block;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--faint);
  margin-bottom: 8px;
}

.gate-card input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  font-family: var(--mono);
  letter-spacing: 1px;
  outline: none;
}

.gate-card input:focus { border-color: var(--accent-dim); }

.btn {
  background: var(--accent);
  color: #201709;
  border: none;
  border-radius: 8px;
  padding: 11px 18px;
  font-weight: 600;
}

.btn:hover { filter: brightness(1.08); }
.btn:disabled { opacity: 0.5; cursor: default; filter: none; }
.btn-block { width: 100%; margin-top: 16px; }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 13px;
}

.btn-ghost:hover { color: var(--text); border-color: var(--accent-dim); }

.err { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ----------------------------------------------------------------- app -- */

.app {
  height: 100%;
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr) 270px;
}

.sidebar, .facts {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.facts { border-right: none; border-left: 1px solid var(--line); }

.pane-head {
  padding: 16px;
  border-bottom: 1px solid var(--line-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand {
  font-family: var(--mono);
  font-size: 15px;
  letter-spacing: -0.3px;
}

.brand small { display: block; color: var(--faint); font-size: 11px; letter-spacing: 0.06em; }

.pane-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--faint);
}

.pane-body { overflow-y: auto; flex: 1; padding: 8px; min-height: 0; }

.pane-foot {
  border-top: 1px solid var(--line-soft);
  padding: 12px 16px;
  font-size: 12px;
  color: var(--faint);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.new-attempt {
  margin: 12px;
  width: calc(100% - 24px);
  background: var(--panel-2);
  border: 1px dashed var(--line);
  color: var(--text);
  border-radius: 8px;
  padding: 10px;
  font-size: 13px;
}

.new-attempt:hover { border-color: var(--accent-dim); color: var(--accent); }

.game-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  margin-bottom: 2px;
}

.game-item:hover { background: var(--panel-2); }
.game-item.active { background: var(--panel-2); border-color: var(--line); }
.game-item .t { font-size: 13px; line-height: 1.35; margin-bottom: 3px; }
.game-item .m { font-size: 11px; color: var(--faint); display: flex; gap: 8px; }
.game-item .won { color: var(--win); }

/* -------------------------------------------------------------- main -- */

.main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }

.topbar {
  padding: 12px 20px;
  border-bottom: 1px solid var(--line-soft);
  display: flex;
  align-items: center;
  gap: 12px;
}

.topbar h2 {
  font-size: 14px;
  font-weight: 500;
  margin: 0;
  flex: 1;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.transcript {
  flex: 1;
  overflow-y: auto;
  padding: 28px 20px 8px;
  min-height: 0;
}

.thread { max-width: 720px; margin: 0 auto; }

.msg { margin-bottom: 22px; position: relative; }

.msg .bubble {
  border-radius: var(--radius);
  padding: 13px 16px;
  line-height: 1.62;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.msg.assistant .bubble {
  background: transparent;
  font-family: var(--mono);
  font-size: 14.5px;
  border-left: 2px solid var(--line);
  border-radius: 0;
  padding-left: 18px;
  color: #ddd4c7;
}

.msg.opening .bubble { color: var(--muted); border-left-color: var(--accent-dim); }

.msg.user { display: flex; flex-direction: column; align-items: flex-end; }

.msg.user .bubble {
  background: var(--panel-2);
  border: 1px solid var(--line);
  max-width: 85%;
  font-size: 14.5px;
}

.msg.error .bubble {
  border-left-color: var(--danger);
  color: var(--danger);
  font-size: 13.5px;
}

.meta {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 7px;
  min-height: 22px;
  opacity: 0;
  transition: opacity 0.12s;
}

.msg:hover .meta, .meta.sticky { opacity: 1; }
.msg.user .meta { justify-content: flex-end; }
.msg.assistant .meta { padding-left: 18px; }

.chip {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--faint);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 11px;
  line-height: 1.4;
}

.chip:hover { color: var(--accent); border-color: var(--accent-dim); }

.branch {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
}

.branch button {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 3px 7px;
  font-size: 12px;
  line-height: 1.4;
}

.branch button:hover:not(:disabled) { color: var(--accent); background: var(--panel-2); }
.branch button:disabled { color: var(--faint); opacity: 0.4; cursor: default; }
.branch span { font-size: 11px; color: var(--faint); padding: 0 3px; font-variant-numeric: tabular-nums; }

.fact-tick { font-size: 11px; color: var(--accent-dim); }

.edit-box { width: 85%; margin-left: auto; }

.edit-box textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--accent-dim);
  border-radius: var(--radius);
  padding: 12px 14px;
  resize: vertical;
  min-height: 76px;
  outline: none;
  line-height: 1.6;
}

.edit-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }

.cursor {
  display: inline-block;
  width: 8px;
  height: 15px;
  background: var(--accent);
  vertical-align: text-bottom;
  animation: blink 1.05s steps(2, start) infinite;
}

@keyframes blink { to { visibility: hidden; } }

/* ----------------------------------------------------------- composer -- */

.composer { padding: 12px 20px 20px; }

.composer-inner {
  max-width: 720px;
  margin: 0 auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 8px 8px 8px 14px;
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.composer-inner:focus-within { border-color: var(--accent-dim); }

.composer textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  padding: 8px 0;
  max-height: 180px;
  line-height: 1.55;
}

.composer .send {
  background: var(--accent);
  color: #201709;
  border: none;
  border-radius: 8px;
  width: 34px;
  height: 34px;
  font-size: 16px;
  display: grid;
  place-items: center;
  flex: none;
}

.composer .send:disabled { opacity: 0.35; cursor: default; }

.hint {
  max-width: 720px;
  margin: 8px auto 0;
  font-size: 11px;
  color: var(--faint);
  display: flex;
  justify-content: space-between;
}

/* -------------------------------------------------------------- facts -- */

.fact {
  background: var(--panel-2);
  border: 1px solid var(--line-soft);
  border-left: 2px solid var(--accent-dim);
  border-radius: 6px;
  padding: 9px 11px;
  font-size: 12.5px;
  line-height: 1.5;
  margin-bottom: 6px;
  color: #d8cfc2;
  animation: slidein 0.3s ease;
}

@keyframes slidein {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: none; }
}

.empty { color: var(--faint); font-size: 12.5px; padding: 14px; line-height: 1.6; }

/* ------------------------------------------------------------ victory -- */

.victory {
  border: 1px solid var(--win);
  background: rgba(134, 192, 108, 0.08);
  border-radius: var(--radius);
  padding: 16px 18px;
  margin-bottom: 22px;
  text-align: center;
}

.victory h3 { margin: 0 0 4px; color: var(--win); font-family: var(--mono); font-size: 17px; }
.victory p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.5; }

#confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9999;
}

/* ------------------------------------------------------------- modal -- */

.modal-back {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: grid;
  place-items: center;
  z-index: 500;
  padding: 20px;
}

.modal {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 24px;
  width: min(460px, 100%);
}

.modal h3 { margin: 0 0 6px; font-size: 16px; }
.modal p { color: var(--muted); font-size: 13px; line-height: 1.55; margin: 0 0 16px; }

.copyrow { display: flex; gap: 8px; }

.copyrow input {
  flex: 1;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: var(--mono);
  font-size: 12.5px;
  outline: none;
}

/* -------------------------------------------------------------- share -- */

.share-page { max-width: 760px; margin: 0 auto; padding: 40px 20px 80px; }

.share-head {
  border-bottom: 1px solid var(--line);
  padding-bottom: 20px;
  margin-bottom: 32px;
}

.share-head h1 { font-family: var(--mono); font-size: 22px; margin: 0 0 8px; letter-spacing: -0.4px; }
.share-head .by { color: var(--faint); font-size: 13px; }

.share-facts {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin: 32px 0;
  background: var(--panel);
}

.share-facts h4 {
  margin: 0 0 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--faint);
}

.cta {
  text-align: center;
  margin-top: 48px;
  padding-top: 28px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.7;
}

/* -------------------------------------------------------------- admin -- */

.admin-page { max-width: 1000px; margin: 0 auto; padding: 36px 20px 80px; }
.admin-page h1 { font-family: var(--mono); font-size: 22px; margin: 0 0 4px; }
.admin-page .sub { color: var(--faint); font-size: 13px; margin-bottom: 28px; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }

th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--faint);
  font-weight: 500;
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
}

td { padding: 10px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
td.num { font-variant-numeric: tabular-nums; color: var(--muted); }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 24px;
}

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

.field label {
  display: block;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--faint);
  margin-bottom: 6px;
}

.field input {
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px 12px;
  outline: none;
}

.field input:focus { border-color: var(--accent-dim); }

.stat-grid { display: flex; gap: 28px; flex-wrap: wrap; }
.stat .v { font-size: 20px; font-family: var(--mono); }
.stat .k { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: 0.07em; }

/* --------------------------------------------------------- responsive -- */

.pane-toggle { display: none; }

@media (max-width: 1080px) {
  .app { grid-template-columns: 1fr; }
  .sidebar, .facts {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 270px;
    z-index: 100;
    transition: transform 0.2s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
  }
  .sidebar { left: 0; transform: translateX(-100%); }
  .facts { right: 0; transform: translateX(100%); }
  .sidebar.open, .facts.open { transform: none; }
  .pane-toggle { display: inline-flex; }
}
