/* Tanggulan: parchment-and-ink interface. */
@font-face { font-family: "IM Fell English"; src: url("../fonts/IMFeENrm28P.ttf") format("truetype"); font-display: swap; }
@font-face { font-family: "IM Fell English"; font-style: italic; src: url("../fonts/IMFeENit28P.ttf") format("truetype"); font-display: swap; }
@font-face { font-family: "IM Fell English SC"; src: url("../fonts/IMFeENsc28P.ttf") format("truetype"); font-display: swap; }
@font-face { font-family: "IM Fell DW Pica"; font-style: italic; src: url("../fonts/IMFePIit28P.ttf") format("truetype"); font-display: swap; }

:root {
  --ink: #2a1c10;
  --ink-soft: #5a4630;
  --paper: #efe3c3;
  --paper-2: #e4d4ad;
  --paper-3: #d8c496;
  --rust: #8a2a14;
  --olive: #4a5a22;
  --blue: #2d3f8c;
  --gold: #b88a2a;
  --wood: #2b2118;
  --serif: "IM Fell English", Georgia, "Times New Roman", serif;
  --sc: "IM Fell English SC", "IM Fell English", Georgia, serif;
  --script: "IM Fell DW Pica", "IM Fell English", Georgia, serif;
  --hud-h: 58px;
  --pal-h: 96px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--wood); color: var(--ink); font-family: var(--serif); -webkit-font-smoothing: antialiased; }
body { touch-action: manipulation; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent; }
button { font-family: inherit; color: inherit; cursor: pointer; }
kbd { font-family: var(--sc); font-size: 0.72em; border: 1px solid currentColor; border-radius: 3px; padding: 0 4px; opacity: 0.75; }
.hidden { display: none !important; }
.screen, #inspector, #hist-body, .campaign-side, #wc-list { touch-action: pan-y; }
#palette { touch-action: pan-x; }
#hud-top, #wave-card, #tutor, #confirm { touch-action: manipulation; }
#game { position: fixed; inset: 0; display: block; touch-action: none; }

/* ---------- paper panels ---------- */
.paper, #wave-card, #inspector, #tutor, #toasts .toast, #tip, #confirm, #hud-top, #palette {
  background:
    radial-gradient(ellipse at 20% 10%, rgba(255,250,230,0.55), transparent 60%),
    radial-gradient(ellipse at 90% 90%, rgba(150,110,50,0.18), transparent 55%),
    linear-gradient(var(--paper), var(--paper-2));
  border: 1px solid rgba(42,28,16,0.75);
  box-shadow: 0 0 0 3px rgba(239,227,195,0.9), 0 0 0 4px rgba(42,28,16,0.55), 0 8px 24px rgba(0,0,0,0.45);
}
.stamp {
  font-family: var(--sc); font-size: 17px; letter-spacing: 0.04em;
  background: linear-gradient(#f3e8cc, #dcc79a); border: 1.5px solid var(--ink); border-radius: 3px;
  padding: 8px 16px; min-height: 40px; box-shadow: 2px 2px 0 rgba(42,28,16,0.7);
  transition: transform .08s, box-shadow .08s, background .2s;
}
.stamp:hover { background: linear-gradient(#fbf2da, #e6d3a8); }
.stamp:active { transform: translate(2px, 2px); box-shadow: 0 0 0 rgba(0,0,0,0); }
.stamp.big { font-size: 21px; padding: 10px 26px; background: linear-gradient(#b3342c, #862118); color: #f7ecd2; border-color: #3a0f08; }
.stamp.big:hover { background: linear-gradient(#c43c32, #93261b); }
.stamp.ghost { background: transparent; box-shadow: none; }
.stamp.small { font-size: 14px; padding: 4px 10px; min-height: 30px; }
.stamp:disabled { opacity: 0.45; cursor: not-allowed; }
.linkish { background: none; border: none; text-decoration: underline; font-style: italic; font-size: 14px; color: var(--ink-soft); padding: 6px; }
.kicker { font-family: var(--sc); letter-spacing: 0.12em; color: var(--rust); font-size: 14px; }
.fine { font-size: 13px; color: var(--ink-soft); font-style: italic; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-top: 14px; }

/* ---------- HUD ---------- */
#hud-top {
  position: fixed; top: 6px; left: 8px; right: 8px; height: var(--hud-h); z-index: 10;
  display: flex; align-items: center; gap: 12px; padding: 4px 10px; border-radius: 2px;
}
#mission-chip { min-width: 0; flex: 0 1 260px; }
#m-name { font-family: var(--script); font-style: italic; font-size: 21px; line-height: 1.05; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#m-sub { font-size: 13px; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#resources { display: flex; gap: 6px; flex: 1 1 auto; justify-content: center; }
.res { display: flex; align-items: center; gap: 5px; padding: 3px 9px; border: 1px solid rgba(42,28,16,0.35); border-radius: 3px; background: rgba(255,250,235,0.45); cursor: help; position: relative; }
.res svg { width: 22px; height: 22px; flex: none; }
.res b { font-size: 21px; font-weight: normal; min-width: 1.6em; }
.res .lbl { font-family: var(--sc); font-size: 12px; color: var(--ink-soft); }
.res.warn { animation: warnPulse 1s infinite; }
.res.bump b { animation: bump .4s; }
@keyframes warnPulse { 50% { background: rgba(179,52,44,0.35); border-color: var(--rust); } }
@keyframes bump { 40% { transform: scale(1.25); color: var(--olive); } }
#controls { display: flex; gap: 5px; }
.ctl { width: 42px; height: 42px; border: 1.5px solid var(--ink); border-radius: 3px; background: linear-gradient(#f3e8cc, #dcc79a); display: grid; place-items: center; padding: 0; font-family: var(--sc); font-size: 16px; }
.ctl svg { width: 22px; height: 22px; fill: var(--ink); stroke: var(--ink); }
.ctl.on { background: linear-gradient(#cfbd8e, #bba670); box-shadow: inset 1px 1px 3px rgba(0,0,0,0.35); }

#wave-card { position: fixed; right: 8px; top: calc(var(--hud-h) + 18px); width: 250px; z-index: 9; padding: 8px 10px 10px; border-radius: 2px; }
.wc-head { display: flex; justify-content: space-between; align-items: baseline; font-family: var(--sc); font-size: 15px; border-bottom: 1px solid rgba(42,28,16,0.35); padding-bottom: 4px; margin-bottom: 6px; }
#wc-timer { font-family: var(--serif); color: var(--rust); }
#wc-list { display: flex; flex-direction: column; gap: 3px; max-height: 38vh; overflow: auto; }
.wc-item { display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 6px; font-size: 14px; line-height: 1.1; }
.wc-item canvas { width: 34px; height: 34px; }
.wc-item .n { font-size: 18px; }
.wc-item .route { font-size: 11px; color: var(--ink-soft); font-style: italic; }
.wc-item.hidden-route .route { color: var(--rust); }
#wc-note { font-style: italic; font-size: 13px; color: var(--rust); margin: 4px 0; }
#wave-card.final .wc-head { color: var(--rust); }
#b-call { width: 100%; margin-top: 8px; font-size: 15px; }
#wave-card.collapsed #wc-list, #wave-card.collapsed #wc-note { display: none; }

#palette {
  position: fixed; left: 8px; right: 8px; bottom: calc(6px + var(--safe-b)); height: var(--pal-h); z-index: 10;
  display: flex; gap: 6px; padding: 6px; overflow-x: auto; overflow-y: hidden; border-radius: 2px; scrollbar-width: thin;
}
.pal { position: relative; flex: 0 0 auto; width: 84px; height: 100%; border: 1.5px solid var(--ink); border-radius: 3px; background: linear-gradient(#f5ead0, #e0cda3); display: flex; flex-direction: column; align-items: center; justify-content: flex-start; padding: 2px 2px 3px; touch-action: pan-x; }
.pal canvas { width: 46px; height: 46px; }
.pal .nm { font-size: 12.5px; line-height: 1; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.pal .cost { font-size: 12px; color: var(--ink-soft); margin-top: 2px; white-space: nowrap; }
.pal .cost b { color: var(--ink); font-weight: normal; }
.pal .hk { position: absolute; top: 2px; left: 4px; font-family: var(--sc); font-size: 12px; opacity: 0.7; }
.pal.sel { background: linear-gradient(#e2c98c, #c9ad6c); box-shadow: inset 0 0 0 2px var(--rust); }
.pal.poor { opacity: 0.55; }
.pal.poor .cost { color: var(--rust); }
.pal.reserve { border-style: dashed; background: linear-gradient(#e3e6cf, #cfd3ad); }
.pal.reserve .cost { color: var(--olive); }
.pal.new::after { content: "new"; position: absolute; top: 2px; right: 4px; font-family: var(--sc); font-size: 11px; color: var(--rust); }

#inspector { position: fixed; right: 8px; bottom: calc(var(--pal-h) + 18px + var(--safe-b)); width: 300px; max-height: calc(100vh - var(--hud-h) - var(--pal-h) - 50px); overflow: auto; z-index: 11; padding: 10px 12px 12px; border-radius: 2px; }
.ins-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.ins-head h3 { margin: 0; font-family: var(--script); font-style: italic; font-weight: normal; font-size: 23px; line-height: 1; }
.ins-head .local { font-family: var(--sc); font-size: 13px; color: var(--ink-soft); }
.ins-close { border: none; background: none; font-size: 24px; line-height: 1; padding: 0 4px; min-width: 32px; min-height: 32px; }
.ins-lvl { color: var(--gold); letter-spacing: 2px; font-size: 14px; }
.ins-desc { font-size: 14px; margin: 6px 0; line-height: 1.25; }
.ins-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px 8px; font-size: 13px; margin: 6px 0; }
.ins-stats div { border-bottom: 1px dotted rgba(42,28,16,0.35); }
.ins-stats span { color: var(--ink-soft); font-family: var(--sc); font-size: 11px; display: block; }
.ins-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.ins-actions .stamp { font-size: 14px; padding: 5px 10px; min-height: 36px; }
.spec-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; }
.spec { text-align: left; border: 1.5px solid var(--ink); border-radius: 3px; background: linear-gradient(#f6ecd4, #e3d0a6); padding: 6px; font-size: 12.5px; line-height: 1.2; min-height: 44px; }
.spec b { display: block; font-family: var(--script); font-style: italic; font-weight: normal; font-size: 16px; }
.spec .c { display: block; margin-top: 4px; color: var(--rust); font-family: var(--sc); }
.spec:disabled { opacity: 0.5; }
.ins-note { font-size: 12.5px; font-style: italic; color: var(--ink-soft); margin-top: 6px; }

#toasts { position: fixed; top: calc(var(--hud-h) + 18px); left: 50%; transform: translateX(-50%); z-index: 12; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; width: min(520px, 92vw); }
.toast { padding: 6px 12px; font-size: 15px; border-radius: 2px; animation: toastIn .25s ease-out; text-align: center; }
.toast.warn { color: var(--rust); }
.toast.bad { color: #6a1a10; }
.toast.good { color: var(--olive); }
.toast.out { opacity: 0; transition: opacity .6s; }
@keyframes toastIn { from { transform: translateY(-8px); opacity: 0; } }

#banner { position: fixed; left: 0; right: 0; top: 30%; text-align: center; z-index: 12; pointer-events: none; opacity: 0; transition: opacity .5s; }
#banner.show { opacity: 1; }
#banner-t { font-family: var(--script); font-style: italic; font-size: clamp(34px, 6vw, 64px); color: #f5e8c8; text-shadow: 0 2px 0 #2a1c10, 0 0 24px rgba(0,0,0,0.6); }
#banner-s { font-family: var(--sc); font-size: clamp(14px, 2vw, 20px); color: #efe0bc; text-shadow: 0 1px 0 #2a1c10, 0 0 12px rgba(0,0,0,0.8); letter-spacing: 0.08em; }
#banner.final #banner-t { color: #f0b8a0; }
#edge-flash { position: fixed; inset: 0; pointer-events: none; z-index: 8; box-shadow: inset 0 0 120px 30px rgba(160,20,10,0.0); transition: box-shadow .5s; }
#edge-flash.on { box-shadow: inset 0 0 120px 30px rgba(160,20,10,0.55); transition: box-shadow .05s; }
#edge-flash.leaking { animation: leakPulse 1.6s infinite; }
@keyframes leakPulse { 50% { box-shadow: inset 0 0 90px 16px rgba(160,20,10,0.35); } }

#tip { position: fixed; z-index: 20; max-width: 280px; padding: 8px 10px; font-size: 13.5px; line-height: 1.25; pointer-events: none; border-radius: 2px; }
#tip b { font-family: var(--script); font-style: italic; font-weight: normal; font-size: 17px; }
#tip .meta { font-family: var(--sc); font-size: 12px; color: var(--ink-soft); margin-top: 3px; }
#confirm { position: fixed; z-index: 13; left: 50%; transform: translateX(-50%); bottom: calc(var(--pal-h) + 22px + var(--safe-b)); display: flex; gap: 8px; align-items: center; padding: 6px 10px; border-radius: 2px; font-size: 15px; }

#tutor { position: fixed; z-index: 14; max-width: 320px; padding: 10px 12px; border-radius: 2px; font-size: 15px; line-height: 1.3; left: 50%; bottom: calc(var(--pal-h) + 26px); transform: translateX(-50%); }
#tutor::after { content: ""; position: absolute; left: var(--arrow-x, 50%); bottom: -10px; width: 16px; height: 16px; background: var(--paper-2); border-right: 1px solid var(--ink); border-bottom: 1px solid var(--ink); transform: translateX(-50%) rotate(45deg); }
#tutor.up::after { bottom: auto; top: -10px; transform: translateX(-50%) rotate(225deg); }
.tutor-row { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
.pulse-hl { animation: hl 1.2s infinite; }
@keyframes hl { 50% { box-shadow: 0 0 0 4px var(--rust); } }

/* ---------- screens ---------- */
.screen { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center; padding: 16px; overflow: auto; background: radial-gradient(ellipse at center, #3a2c1e, #1c150e); }
.screen.dim { background: rgba(20,14,8,0.6); }
#scr-title { background: #1c150e; }
#title-map { position: absolute; inset: 0; width: 100%; height: 100%; }
.title-box { position: relative; text-align: center; padding: 28px 34px 22px; max-width: 520px; background: rgba(239,227,195,0.93); border: 1px solid var(--ink); box-shadow: 0 0 0 4px rgba(239,227,195,0.9), 0 0 0 5px var(--ink), 0 20px 60px rgba(0,0,0,0.6); }
.title-box h1 { font-family: var(--script); font-style: italic; font-weight: normal; font-size: clamp(54px, 11vw, 86px); margin: 4px 0 0; line-height: 0.95; }
.title-box .subtitle { font-family: var(--sc); letter-spacing: 0.1em; margin-bottom: 18px; }
.menu-col { display: flex; flex-direction: column; gap: 10px; align-items: stretch; max-width: 320px; margin: 0 auto; }

.results { max-width: 560px; width: 100%; padding: 22px 26px; }
.results.wide, .history.wide { max-width: 820px; }
.results h2, .history h2, .campaign-side h2, .brief-text h2 { font-family: var(--script); font-style: italic; font-weight: normal; font-size: 36px; margin: 2px 0 8px; }
#rs-stars { font-size: 40px; color: var(--gold); letter-spacing: 6px; text-shadow: 0 1px 0 var(--ink); }
#rs-stars .off { color: rgba(42,28,16,0.25); text-shadow: none; }
#rs-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 6px 14px; margin: 12px 0; }
#rs-stats div { border-bottom: 1px dotted rgba(42,28,16,0.35); display: flex; justify-content: space-between; }
#rs-stats dt { font-family: var(--sc); font-size: 13px; color: var(--ink-soft); }
#rs-stats dd { margin: 0; font-size: 17px; }

.campaign-wrap { display: grid; grid-template-columns: minmax(300px, 1.1fr) minmax(300px, 1fr); gap: 18px; width: 100%; max-width: 1180px; height: min(92vh, 800px); }
.campaign-map-box { position: relative; min-height: 300px; border: 1px solid var(--ink); box-shadow: 0 0 0 4px rgba(239,227,195,0.9), 0 0 0 5px var(--ink); background: var(--paper); }
#campaign-map { width: 100%; height: 100%; display: block; cursor: pointer; }
.campaign-side { overflow: auto; padding: 16px 18px; background: linear-gradient(var(--paper), var(--paper-2)); border: 1px solid var(--ink); box-shadow: 0 0 0 4px rgba(239,227,195,0.9), 0 0 0 5px var(--ink); }
.campaign-side h2 small { display: block; font-family: var(--sc); font-style: normal; font-size: 13px; color: var(--ink-soft); }
#timeline { list-style: none; margin: 0 0 12px; padding: 0 0 0 14px; border-left: 2px solid rgba(42,28,16,0.35); font-size: 13.5px; }
#timeline li { margin: 2px 0; color: rgba(42,28,16,0.45); position: relative; }
#timeline li::before { content: ""; position: absolute; left: -19px; top: 6px; width: 8px; height: 8px; border-radius: 50%; background: rgba(42,28,16,0.3); }
#timeline li.reached { color: var(--ink); }
#timeline li.reached::before { background: var(--rust); }
#timeline li b { font-family: var(--sc); font-weight: normal; margin-right: 6px; }
.mcard { display: grid; grid-template-columns: 36px 1fr auto; gap: 10px; align-items: center; width: 100%; text-align: left; padding: 8px 10px; margin: 6px 0; border: 1.5px solid var(--ink); border-radius: 3px; background: linear-gradient(#f6ecd4, #e3d0a6); min-height: 54px; }
.mcard .num { font-family: var(--script); font-style: italic; font-size: 26px; text-align: center; }
.mcard .t { font-family: var(--script); font-style: italic; font-size: 19px; line-height: 1.05; }
.mcard .d { font-size: 12.5px; color: var(--ink-soft); }
.mcard .st { color: var(--gold); font-size: 18px; letter-spacing: 1px; }
.mcard .st .off { color: rgba(42,28,16,0.25); }
.mcard.locked { opacity: 0.45; cursor: not-allowed; }
.mcard.current { box-shadow: inset 0 0 0 2px var(--rust); }

.brief-wrap { display: grid; grid-template-columns: minmax(260px, 1fr) minmax(300px, 1.1fr); gap: 20px; max-width: 1080px; width: 100%; padding: 18px; }
.brief-map { min-height: 280px; border: 1px solid var(--ink); }
#brief-map { width: 100%; height: 100%; display: block; }
.brief-text p { font-size: 16.5px; line-height: 1.42; margin: 0 0 10px; }
.brief-rule { height: 1px; background: rgba(42,28,16,0.4); margin: 10px 0; }
#br-orders { font-size: 15px; }
#br-orders b { font-family: var(--sc); font-weight: normal; color: var(--rust); }
#br-new { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.newchip { display: flex; align-items: center; gap: 4px; font-size: 13px; border: 1px solid rgba(42,28,16,0.4); border-radius: 3px; padding: 2px 8px 2px 2px; background: rgba(255,250,235,0.5); }
.newchip canvas { width: 30px; height: 30px; }
.newchip.enemy { border-color: rgba(45,63,140,0.5); }

.history { width: 100%; padding: 16px 22px 20px; max-height: 92vh; display: flex; flex-direction: column; }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--ink); }
.tab { border: 1px solid var(--ink); border-bottom: none; background: var(--paper-3); padding: 6px 12px; font-family: var(--sc); font-size: 15px; border-radius: 3px 3px 0 0; min-height: 38px; }
.tab.active { background: var(--paper); }
#hist-body { overflow: auto; padding: 12px 4px; font-size: 16px; line-height: 1.42; }
#hist-body dt { font-family: var(--script); font-style: italic; font-size: 20px; margin-top: 10px; }
#hist-body dd { margin: 2px 0 0 0; }
#hist-body table { border-collapse: collapse; width: 100%; font-size: 15px; }
#hist-body td { padding: 4px 6px; border-bottom: 1px dotted rgba(42,28,16,0.35); vertical-align: top; }
#hist-body td:first-child { font-family: var(--sc); white-space: nowrap; }
.setting { display: flex; gap: 10px; align-items: center; margin: 10px 0; font-size: 16px; }
.setting input[type=range] { flex: 1; }
.setting input[type=checkbox] { width: 20px; height: 20px; }
.endless-row { display: grid; grid-template-columns: 1fr auto auto; gap: 10px; align-items: center; padding: 6px 0; border-bottom: 1px dotted rgba(42,28,16,0.35); }
.endless-row .t { font-family: var(--script); font-style: italic; font-size: 19px; }
.endless-row .b { font-size: 13px; color: var(--ink-soft); }

#err-panel { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 100; background: #f3d9c8; color: #4a1208; border: 1px solid #4a1208; padding: 8px 12px; font: 14px Georgia, serif; max-width: 90vw; }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  #mission-chip { flex-basis: 150px; }
  #m-name { font-size: 17px; }
  .res .lbl { display: none; }
  .res b { font-size: 18px; }
  #wave-card { width: 200px; }
}
@media (max-width: 760px) {
  :root { --hud-h: 94px; --pal-h: 84px; }
  #hud-top { flex-wrap: wrap; height: auto; min-height: var(--hud-h); gap: 4px 6px; padding: 4px 6px; }
  #mission-chip { flex: 1 1 50%; order: 1; }
  #controls { order: 2; }
  #resources { order: 3; flex: 1 1 100%; justify-content: space-between; }
  .res { padding: 2px 6px; }
  .res svg { width: 18px; height: 18px; }
  .ctl { width: 38px; height: 38px; }
  #wave-card { top: auto; bottom: calc(var(--pal-h) + 16px + var(--safe-b)); left: 8px; right: auto; width: min(230px, 60vw); padding: 6px 8px; }
  #wave-card #wc-list { max-height: 22vh; }
  #inspector { left: 8px; right: 8px; width: auto; bottom: calc(var(--pal-h) + 16px + var(--safe-b)); max-height: 52vh; }
  .pal { width: 70px; }
  .pal canvas { width: 38px; height: 38px; }
  .pal .nm { font-size: 11px; }
  .campaign-wrap { grid-template-columns: 1fr; height: auto; }
  .campaign-map-box { height: 46vh; }
  .brief-wrap { grid-template-columns: 1fr; }
  .brief-map { height: 34vh; }
  #scr-campaign, #scr-briefing { align-items: flex-start; }
  .spec-cards { grid-template-columns: 1fr 1fr; }
}
@media (max-height: 520px) and (orientation: landscape) {
  :root { --hud-h: 46px; --pal-h: 74px; }
  #m-sub { display: none; }
  .res .lbl { display: none; }
  .pal canvas { width: 34px; height: 34px; }
  #wave-card #wc-list { max-height: 26vh; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, #banner { transition: none; animation: none; }
}
