/* =========================================================
   Kiểm Đếm Ca Đêm – giao diện
   Nguyên tắc tiếng Việt:
   - Font có đủ dấu: Be Vietnam Pro (giao diện), Noto Serif (lời kể), Roboto Mono (số liệu)
   - line-height >= 1.6 cho chữ thường, >= 1.25 cho tiêu đề để dấu chồng (ễ, ậ, ỗ) không bị cắt
   - Không dùng letter-spacing rộng hay text-transform: uppercase với chữ có dấu
   ========================================================= */
:root {
  --bg: #07090c;
  --ink: #ece7dc;
  --muted: #a39f95;
  --amber: #ffcf7a;
  --red: #ff6b5e;
  --blue: #9cc9ff;
  --line: rgba(236, 231, 220, 0.16);
  --panel: rgba(9, 11, 15, 0.86);
  --sans: 'Be Vietnam Pro', 'Segoe UI', 'Noto Sans', Arial, sans-serif;
  --serif: 'Noto Serif', 'Times New Roman', serif;
  --mono: 'Roboto Mono', Consolas, monospace;
}

* { box-sizing: border-box; min-width: 0; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 700px at 50% 0%, #1a2230 0%, var(--bg) 70%);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 17px;
  line-height: 1.75;
  overflow: hidden;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  font-kerning: normal;
  font-variant-ligatures: common-ligatures;
}

/* ---------- Lớp 3D ---------- */
#world { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; z-index: 0; }
.no3d #world { display: none; }
.labels { position: fixed; inset: 0; pointer-events: none; z-index: 3; }
.label3d {
  position: absolute; left: 0; top: 0; white-space: nowrap;
  font: 600 13px/1.4 var(--sans); color: #fff;
  background: rgba(0, 0, 0, 0.55); border: 1px solid rgba(255, 207, 122, 0.55);
  padding: 2px 8px; border-radius: 10px; margin-top: -6px;
}
.vignette {
  position: fixed; inset: 0; pointer-events: none; z-index: 2;
  background: radial-gradient(ellipse at 50% 40%, transparent 45%, rgba(0, 0, 0, 0.65) 100%);
}
.grain {
  position: fixed; inset: -50%; pointer-events: none; z-index: 4; opacity: 0.06;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='160' height='160'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
  animation: grain 1.2s steps(6) infinite;
}
@keyframes grain { 0%{transform:translate(0,0)} 50%{transform:translate(-4%,3%)} 100%{transform:translate(3%,-2%)} }
.fader { position: fixed; inset: 0; background: #000; opacity: 0; pointer-events: none; z-index: 5; transition: opacity 0.38s; }
.fader.on { opacity: 1; }

/* ---------- HUD ---------- */
.hud {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: auto 1fr auto; gap: 16px; align-items: center;
  padding: 10px 18px; background: linear-gradient(rgba(5,7,10,0.92), rgba(5,7,10,0.7));
  border-bottom: 1px solid var(--line); font-family: var(--sans); font-size: 13px; line-height: 1.4;
}
.brand strong { display: block; font-size: 15px; font-weight: 700; }
.brand span { color: var(--muted); font-size: 12px; }
.stats { display: flex; gap: 16px; flex-wrap: wrap; justify-content: center; }
.stat { display: flex; align-items: center; gap: 7px; }
.stat .lbl { color: var(--muted); }
.track { width: 86px; height: 7px; background: #1b1f25; border-radius: 4px; overflow: hidden; }
.fill { display: block; height: 100%; transition: width 0.6s ease; }
.fill.tt { background: #a8d8bd; } .fill.tc { background: var(--amber); } .fill.dn { background: var(--red); }
.num { min-width: 24px; text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.stat.warn .lbl, .stat.warn .num { color: var(--red); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
.pins { display: flex; gap: 3px; }
.pin { width: 7px; height: 13px; border: 1px solid #555; border-radius: 1px; }
.pin.on { background: var(--amber); border-color: var(--amber); box-shadow: 0 0 6px rgba(255,207,122,0.5); }
.tools { display: flex; gap: 8px; align-items: center; }
.clock { color: var(--amber); font: 500 16px/1 var(--mono); margin-right: 4px; }
.tool {
  font: 500 13px/1.3 var(--sans); color: var(--ink); background: rgba(0,0,0,0.35);
  border: 1px solid var(--line); padding: 7px 11px; border-radius: 6px; cursor: pointer; min-height: 34px;
}
.tool:hover, .tool:focus-visible { border-color: var(--amber); outline: none; }
.tool[aria-pressed="true"] { border-color: var(--amber); color: var(--amber); }
.badge { background: var(--amber); color: #111; border-radius: 8px; padding: 0 6px; margin-left: 3px; font-weight: 700; }

.location {
  position: fixed; top: 72px; left: 18px; z-index: 9;
  font: 600 14px/1.4 var(--sans); color: #fff;
  background: rgba(0,0,0,0.5); border-left: 3px solid var(--amber);
  padding: 6px 12px; border-radius: 0 6px 6px 0;
}
.location:empty { display: none; }

/* ---------- Khung lời kể (kiểu game nhập vai) ---------- */
.stage {
  position: fixed; z-index: 8; left: 50%; bottom: 16px; transform: translateX(-50%);
  width: min(920px, calc(100% - 24px)); max-height: 46vh; overflow-y: auto;
  padding: 18px 26px 20px; background: var(--panel);
  border: 1px solid var(--line); border-top: 2px solid var(--amber); border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.6);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
  scrollbar-width: thin; transition: max-height 0.35s ease, opacity 0.3s;
}
.stage.danger { border-top-color: var(--red); }
.stage.t-puzzle { max-height: 78vh; }
.stage.t-actend, .stage.t-ending { max-height: 80vh; }
.summary p, .summary .endname { opacity: 1 !important; transform: none !important; }
.stage.t-title { max-height: 62vh; bottom: 50%; transform: translate(-50%, 50%); }
.no3d .stage { max-height: 82vh; }
body.view-only .stage { opacity: 0; pointer-events: none; }

.scene-head { margin-bottom: 10px; }
.scene-time { font: 500 13px/1.4 var(--mono); color: var(--amber); }
.stage.danger .scene-time { color: var(--red); }
.scene-head h2 { font: 700 23px/1.3 var(--sans); margin: 2px 0 0; text-wrap: balance; }
.t-ending .scene-head h2 { color: var(--red); font-size: 26px; }

.text p {
  margin: 0 0 12px; opacity: 0; transform: translateY(6px);
  transition: opacity 0.55s ease, transform 0.55s ease;
}
.text p.in { opacity: 1; transform: none; }
.t-ending .text p { color: #e6d2cc; font-style: italic; }

/* ---------- Lựa chọn ---------- */
.choices { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.choice {
  display: flex; align-items: baseline; gap: 12px; width: 100%; text-align: left;
  font: 500 16px/1.5 var(--sans); color: var(--ink); background: rgba(28,32,40,0.9);
  border: 1px solid var(--line); border-left: 3px solid var(--amber);
  padding: 11px 15px; border-radius: 6px; cursor: pointer; min-height: 46px;
  transition: transform 0.15s, border-color 0.15s, background 0.15s;
}
.choice:hover, .choice:focus-visible { transform: translateX(4px); border-color: var(--amber); background: rgba(48,40,24,0.95); outline: none; }
.choice .key { font: 700 15px/1.5 var(--mono); color: var(--amber); }
.choice .hint { margin-left: auto; font: 400 12px/1.4 var(--sans); color: var(--muted); white-space: nowrap; }
.stage.danger .choice { border-left-color: var(--red); }
.choice.ghost { border-left-color: var(--muted); }

/* ---------- Bản vẽ ---------- */
.blueprint { border: 1px solid #2c5a86; border-radius: 6px 6px 0 0; overflow: hidden; background: #0b2a4a; }
.blueprint svg { display: block; width: 100%; height: auto; max-height: 42vh; }
.blueprint .hit { cursor: pointer; transition: filter 0.15s; }
.blueprint .hit:hover, .blueprint .hit:focus { filter: brightness(1.6) drop-shadow(0 0 4px #ffd27a); outline: none; }
.blueprint .hit.sel { filter: brightness(1.8) drop-shadow(0 0 6px #ffd27a); }
.inspect {
  font: 400 14px/1.6 var(--sans); color: var(--blue);
  background: #0a1f36; border: 1px solid #2c5a86; border-top: 0; border-radius: 0 0 6px 6px;
  padding: 9px 12px; min-height: 42px;
}

/* ---------- Thông báo nổi ---------- */
.toasts { position: fixed; right: 16px; top: 76px; z-index: 20; display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.toast {
  font: 500 13px/1.4 var(--sans); padding: 6px 11px; border-radius: 6px;
  background: rgba(15,18,23,0.92); border: 1px solid var(--line); animation: tin 0.3s ease;
}
.toast.up { color: #b8e8c8; } .toast.down { color: var(--red); }
.toast.rule { color: #fff; border-color: #fff; } .toast.clue { color: var(--amber); }
.toast.out { opacity: 0; transform: translateX(20px); transition: all 0.5s; }
@keyframes tin { from { opacity: 0; transform: translateX(20px); } }

/* ---------- Sổ tay ---------- */
dialog.paper {
  max-width: 660px; width: calc(100% - 32px); max-height: 84vh; overflow-y: auto;
  color: #2a251c; border: 0; border-radius: 4px; padding: 24px 28px;
  font: 400 16px/1.7 var(--serif); box-shadow: 0 20px 60px rgba(0,0,0,0.7);
  background: repeating-linear-gradient(#ece5d2 0 28px, #d6cdb5 28px 29px);
}
.paper::backdrop { background: rgba(0,0,0,0.72); }
.paper h2 { margin: 0 0 6px; font: 700 22px/1.3 var(--sans); }
.paper h3 { margin: 18px 0 4px; font: 700 14px/1.4 var(--sans); color: #6b4a1e; }
.paper ul { padding-left: 18px; margin: 6px 0; }
.paper li { margin-bottom: 10px; }
.paper .src { display: block; font: 400 12px/1.5 var(--sans); color: #6b665a; }
.paper .conf-thap { color: #a1301f; font-weight: 700; }
.paper .choice { margin-top: 16px; color: var(--ink); }
.muted { color: var(--muted); } .small { font-size: 13px; }
.paper .muted { color: #6b665a; }

/* ---------- Tổng kết màn ---------- */
.summary table { border-collapse: collapse; width: 100%; font: 400 15px/1.5 var(--sans); }
.summary th, .summary td { border-bottom: 1px solid var(--line); padding: 7px 6px; text-align: left; }
.summary td { font-family: var(--mono); text-align: right; }
.summary th { color: var(--muted); font-weight: 400; }
.summary h3 { font: 700 14px/1.4 var(--sans); color: var(--amber); margin-top: 18px; }
.summary .next { color: var(--amber); font-style: italic; margin-top: 16px; }

/* ---------- Trạng thái ---------- */
body.low-sanity .text p.in { animation: drift 7s ease-in-out infinite; }
@keyframes drift { 50% { transform: translateX(1.5px); filter: blur(0.25px); } }
body.dead .vignette { background: radial-gradient(ellipse at 50% 40%, rgba(120,0,0,0.2) 20%, rgba(60,0,0,0.85) 100%); }

@media (max-width: 760px) {
  body { font-size: 16px; line-height: 1.7; }
  .hud { grid-template-columns: 1fr; gap: 6px; padding: 8px 10px; }
  .brand, .clock { display: none; }
  .stats { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 14px; }
  .stat .lbl { white-space: nowrap; width: 58px; font-size: 12px; }
  .track { flex: 1; width: auto; }
  .tools { justify-content: flex-end; }
  .tool { padding: 5px 9px; font-size: 12px; min-height: 30px; }
  .location { top: 100px; left: 8px; font-size: 13px; }
  .stage { max-height: 52vh; bottom: 8px; padding: 14px 16px 16px; width: calc(100% - 16px); }
  .stage.t-puzzle { max-height: 84vh; }
  .stage.t-title { max-height: 70vh; }
  .scene-head h2 { font-size: 20px; }
  .choice { font-size: 15px; }
  .toasts { top: 140px; right: 8px; }
  .toast { font-size: 12px; padding: 4px 9px; }
}
@media (prefers-reduced-motion: reduce) {
  .grain { animation: none; }
  .text p { transition: none; }
  body.low-sanity .text p.in { animation: none; }
}

/* ---------- Màn hình chính ---------- */
.menu {
  position: fixed; inset: 0; z-index: 30; display: none; overflow-y: auto;
  background: linear-gradient(90deg, rgba(5,7,10,0.92) 0%, rgba(5,7,10,0.75) 45%, rgba(5,7,10,0.1) 75%);
}
body.in-menu .menu { display: block; }
body.in-menu .hud, body.in-menu .stage, body.in-menu .location, body.in-menu .labels { display: none; }
.menu-inner { max-width: 680px; padding: clamp(28px, 7vh, 80px) clamp(20px, 5vw, 64px) 40px; }
.kicker { font: 600 13px/1.4 var(--sans); color: var(--amber); margin: 0 0 10px; }
.menu h1 { font: 700 clamp(38px, 5vw, 62px)/1.15 var(--sans); margin: 0; color: #fff; text-shadow: 0 4px 30px rgba(0,0,0,0.8); }
.menu .sub { font: 500 18px/1.5 var(--sans); color: var(--muted); margin: 6px 0 22px; }
.hook p { margin: 0 0 12px; font-size: 18px; line-height: 1.75; color: #e9e3d6; }
.hook .q { color: #fff; font-style: italic; }
.facts { list-style: none; padding: 0; margin: 18px 0 26px; display: flex; flex-wrap: wrap; gap: 8px; }
.facts li { font: 500 13px/1.4 var(--sans); padding: 5px 11px; border: 1px solid var(--line); border-radius: 20px; background: rgba(0,0,0,0.35); }
.menu-actions { display: flex; gap: 12px; flex-wrap: wrap; }
.cta {
  font: 700 18px/1.3 var(--sans); color: #1a1408; background: var(--amber); border: 0;
  padding: 15px 28px; border-radius: 8px; cursor: pointer; min-height: 52px;
  box-shadow: 0 8px 30px rgba(255,207,122,0.25); transition: transform 0.15s, box-shadow 0.15s;
}
.cta:hover, .cta:focus-visible { transform: translateY(-2px); box-shadow: 0 12px 36px rgba(255,207,122,0.4); outline: 2px solid #fff; outline-offset: 2px; }
.cta.ghost { background: transparent; color: var(--ink); border: 1px solid var(--amber); box-shadow: none; }
.menu-links { display: flex; flex-wrap: wrap; gap: 8px 18px; margin-top: 26px; }
.menu-links button { font: 500 15px/1.5 var(--sans); color: var(--ink); background: none; border: 0; padding: 6px 0; cursor: pointer; border-bottom: 1px solid transparent; min-height: 36px; }
.menu-links button:hover, .menu-links button:focus-visible { color: var(--amber); border-bottom-color: var(--amber); outline: none; }
.pill { font: 600 12px/1 var(--mono); background: rgba(255,207,122,0.18); color: var(--amber); padding: 3px 6px; border-radius: 8px; margin-left: 4px; }
.foot { margin-top: 28px; font: 400 13px/1.5 var(--sans); color: var(--muted); }

/* ---------- Đồng hồ đếm ngược ---------- */
.timer { display: flex; align-items: center; gap: 10px; margin-top: 12px; font: 600 13px/1.4 var(--sans); color: var(--amber); }
.timer-track { flex: 1; height: 6px; background: #22262d; border-radius: 3px; overflow: hidden; }
.timer-bar { height: 100%; width: 100%; background: var(--amber); transition: width 0.1s linear; }
.timer.urgent { color: var(--red); animation: pulse 0.6s infinite; }
.timer.urgent .timer-bar { background: var(--red); }

/* ---------- Hộp thoại giấy ---------- */
.paper .close { margin-top: 18px; }
.paper i { font-style: italic; }
.paper .changelog li { margin-bottom: 6px; }
.paper .clues li.rare { color: #7a3d0a; font-weight: 600; }
.endings { list-style: none; padding: 0 !important; display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.endings li { background: rgba(255,255,255,0.55); border: 1px solid #b8ad92; border-radius: 6px; padding: 10px 12px; margin: 0 !important; font-family: var(--sans); }
.endings li b { display: block; font-size: 15px; line-height: 1.35; }
.endings li span { display: block; font-size: 11px; color: #6b665a; }
.endings li small { display: block; font-size: 12.5px; line-height: 1.45; margin-top: 4px; color: #4a4436; }
.endings li.locked { background: rgba(60,55,45,0.12); }
.endings li.locked b { color: #8a826f; letter-spacing: 0.1em; }
.endings li.got.act { border-color: #b8862a; }
.endings li.got.fail b { color: #8e2a1c; }
.paper .bank { display: grid; gap: 6px; background: rgba(255,255,255,0.55); padding: 12px; border-radius: 6px; font-family: var(--sans); font-size: 15px; }
.paper .bank span { display: inline-block; width: 120px; color: #6b665a; font-size: 13px; }
.paper .bank .tool { color: #2a251c; border-color: #b8ad92; background: #fff; min-height: 28px; padding: 3px 8px; }
.paper .links a { display: inline-block; margin: 4px 10px 4px 0; padding: 8px 14px; background: #2a251c; color: #ffe2a8; border-radius: 6px; text-decoration: none; font: 600 15px/1.3 var(--sans); }
.paper .qr { display: block; width: 200px; max-width: 70%; margin: 10px 0; border-radius: 6px; }
.paper .tool { color: #2a251c; border-color: #b8ad92; background: rgba(255,255,255,0.7); }

/* ---------- Kết màn ---------- */
.summary .endname { font: 700 20px/1.35 var(--sans); color: var(--amber); margin: 0 0 10px; }
.summary .new { font: 700 11px/1 var(--sans); background: var(--red); color: #fff; padding: 3px 6px; border-radius: 8px; vertical-align: middle; }
.summary .teaser { margin-top: 14px; color: #e9e3d6; }
.thanks { margin-top: 16px; padding: 14px 16px; border: 1px dashed rgba(255,207,122,0.5); border-radius: 8px; }
.thanks h3 { margin: 0 0 6px !important; }
.thanks p { margin: 0 0 8px; }
.collect { font: 600 14px/1.5 var(--sans); color: var(--amber); font-style: normal !important; }

@media (max-width: 760px) {
  .menu { background: linear-gradient(180deg, rgba(5,7,10,0.15) 0%, rgba(5,7,10,0.92) 38%); }
  .menu-inner { padding-top: 34vh; }
  .hook p { font-size: 16.5px; }
  .cta { width: 100%; }
}

.choice.primary { background: var(--amber); color: #1a1408; border-color: var(--amber); font-weight: 700; }
.choice.primary:hover, .choice.primary:focus-visible { background: #ffe0a3; }
[hidden] { display: none !important; }

.qr-thumb { display: inline-flex; align-items: center; gap: 12px; margin-top: 6px; padding: 8px; background: #fff; border: 0; border-radius: 8px; cursor: pointer; color: #1a1408; font: 600 14px/1.4 var(--sans); text-align: left; }
.qr-thumb img { width: 92px; height: auto; border-radius: 4px; }
.qr-thumb:hover, .qr-thumb:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }
.paper .qr { width: 260px; max-width: 80%; background: #fff; padding: 6px; }

/* ---------- Góp ý, đăng ký, chọn màn ---------- */
.fb { display: grid; gap: 12px; font-family: var(--sans); }
.fb label { display: grid; gap: 4px; font: 600 14px/1.4 var(--sans); color: #3a3226; }
.fb input[type=text], .fb select, .fb textarea { font: 400 16px/1.5 var(--sans); padding: 9px 10px; border: 1px solid #b8ad92; border-radius: 6px; background: #fffdf6; color: #2a251c; min-height: 44px; }
.fb textarea { resize: vertical; }
.fb input:focus-visible, .fb select:focus-visible, .fb textarea:focus-visible { outline: 2px solid #b8862a; }
.fb .check { display: flex; align-items: center; gap: 8px; font-weight: 500; }
.fb .check input { width: 20px; height: 20px; }
.stars { border: 0; padding: 0; margin: 0; }
.stars legend { font: 600 14px/1.4 var(--sans); color: #3a3226; margin-bottom: 4px; }
.stars button { font-size: 28px; line-height: 1; background: none; border: 0; cursor: pointer; color: #c9bd9f; padding: 4px; min-width: 44px; min-height: 44px; }
.stars button.on { color: #d18b14; }
.stars button:focus-visible { outline: 2px solid #b8862a; border-radius: 6px; }
.warn { color: #8e2a1c; margin: 0; }
.fb-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.fb-actions a, .fb-actions button, .sub-list a, .sub-list button {
  display: inline-flex; align-items: center; gap: 6px; padding: 10px 14px; min-height: 44px; border-radius: 6px;
  background: #2a251c; color: #ffe2a8; text-decoration: none; border: 0; cursor: pointer; font: 600 15px/1.3 var(--sans);
}
.fb-actions .ghostbtn { background: transparent; color: #2a251c; border: 1px solid #b8ad92; }
.sub-list li { margin-bottom: 14px; font-family: var(--sans); font-size: 15px; }
.sub-list li p { margin: 4px 0 8px; color: #4a4436; }
.chapters { list-style: none; padding: 0 !important; display: grid; gap: 8px; }
.chapters li { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: rgba(255,255,255,0.55); border: 1px solid #b8ad92; border-radius: 6px; padding: 10px 12px; margin: 0 !important; font-family: var(--sans); }
.chapters li b { font-size: 15px; }
.chapters li small { display: block; color: #6b665a; font-size: 12.5px; }
.chapters button { padding: 8px 12px; min-height: 40px; border-radius: 6px; border: 0; background: #2a251c; color: #ffe2a8; font: 600 14px/1.3 var(--sans); cursor: pointer; }
.chapters li.locked { opacity: 0.55; }
.final-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 8px; margin-top: 12px; }
