:root {
  --ink: #120d09;
  --leather: #231a13;
  --leather-2: #2e2219;
  --wood: #3a2a1d;
  --gold: #c9a45c;
  --gold-hi: #e8cf8f;
  --gold-dim: #8a6f3e;
  --parch: #e9dcbc;
  --parch-dim: #b9aa86;
  --blood: #8e2a22;
  --good: #8fb36a;
  --warn: #d79a4a;
  --ally: #7fb0c9;
  --serif: "EB Garamond", Georgia, "Times New Roman", serif;
  --display: "Cinzel", "Trajan Pro", Georgia, serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--ink); color: var(--parch); font-family: var(--serif); font-size: 17px; overflow: hidden; }
button, input, select { font-family: inherit; font-size: inherit; }
.hidden { display: none !important; }
.muted { color: var(--parch-dim); }
.small { font-size: 0.85em; }
.error { color: #e08070; min-height: 1.2em; margin: 8px 0 0; }

.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: block; }

/* ---------- frames & buttons ---------- */
.frame {
  background:
    linear-gradient(180deg, rgba(255, 230, 180, 0.04), rgba(0, 0, 0, 0.25)),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.012) 0 2px, transparent 2px 5px),
    var(--leather);
  border: 1px solid var(--gold-dim);
  box-shadow: 0 0 0 3px var(--ink), 0 0 0 4px var(--gold-dim), 0 18px 60px rgba(0, 0, 0, 0.7), inset 0 0 40px rgba(0, 0, 0, 0.6);
  border-radius: 3px;
}
.panel {
  background: linear-gradient(180deg, rgba(46, 34, 25, 0.94), rgba(28, 20, 14, 0.94));
  border: 1px solid var(--gold-dim);
  box-shadow: 0 0 0 2px rgba(10, 7, 5, 0.85), 0 6px 22px rgba(0, 0, 0, 0.55);
  border-radius: 3px;
}
.btn {
  font-family: var(--display); font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; font-size: 0.82rem;
  color: var(--gold-hi); background: linear-gradient(180deg, #4a3624, #2a1e14);
  border: 1px solid var(--gold-dim); padding: 10px 16px; border-radius: 2px; cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 220, 160, 0.15), 0 2px 0 #0a0705;
  transition: filter 0.12s, transform 0.05s;
}
.btn:hover { filter: brightness(1.2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; filter: none; }
.btn.primary { background: linear-gradient(180deg, #8e2a22, #5a1712); color: #f6e2b0; border-color: var(--gold); }
.btn.ghost { background: transparent; }
.btn.wide { width: 100%; margin-top: 14px; }
input, select {
  background: #17110c; color: var(--parch); border: 1px solid var(--gold-dim); border-radius: 2px; padding: 9px 10px; outline: none;
}
input:focus, select:focus { border-color: var(--gold-hi); }

/* ---------- menu ---------- */
.menu-bg {
  position: absolute; inset: 0;
  background:
    radial-gradient(ellipse at 50% 35%, rgba(201, 164, 92, 0.16), transparent 60%),
    radial-gradient(ellipse at 50% 120%, rgba(142, 42, 34, 0.35), transparent 60%),
    linear-gradient(180deg, #1a130e, #0c0806);
}
.menu-card, .room-card {
  position: relative; margin: 6vh auto 0; width: min(560px, calc(100% - 32px)); padding: 30px 32px 24px; max-height: 88vh; overflow: auto;
}
.room-card { width: min(860px, calc(100% - 32px)); }
.crest {
  width: 64px; height: 76px; margin: 0 auto 6px;
  background: linear-gradient(180deg, #8e2a22, #4a120e);
  clip-path: path("M0 0 H64 V40 C64 62 40 72 32 76 C24 72 0 62 0 40 Z");
  position: relative;
}
.crest::after { content: ""; position: absolute; inset: 14px 18px 22px; background: var(--gold); clip-path: polygon(50% 0, 100% 100%, 50% 72%, 0 100%); }
h1 { font-family: var(--display); font-weight: 900; text-align: center; margin: 0; font-size: 2.6rem; color: var(--gold-hi); letter-spacing: 0.08em; text-shadow: 0 2px 0 #000, 0 0 24px rgba(201, 164, 92, 0.25); }
h1 span.t { display: inline; font-size: inherit; letter-spacing: inherit; color: inherit; font-weight: inherit; margin: 0; }
h1 span:not(.t) { display: block; font-size: 0.9rem; letter-spacing: 0.42em; color: var(--parch-dim); font-weight: 500; margin-top: 2px; }
h2 { font-family: var(--display); color: var(--gold-hi); margin: 0 0 4px; letter-spacing: 0.05em; }
h3.sub { font-family: var(--display); font-size: 0.82rem; letter-spacing: 0.2em; text-transform: uppercase; color: var(--gold); border-bottom: 1px solid rgba(201, 164, 92, 0.3); padding-bottom: 6px; margin: 22px 0 10px; }
.tagline { text-align: center; color: var(--parch-dim); font-style: italic; margin: 10px 0 22px; }
.field { display: grid; gap: 6px; margin-bottom: 12px; }
.field span { font-family: var(--display); font-size: 0.75rem; letter-spacing: 0.15em; text-transform: uppercase; color: var(--gold); }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row .btn.primary { flex: 1 1 auto; }
.code-input { width: 90px; text-transform: uppercase; text-align: center; letter-spacing: 0.25em; font-family: var(--display); }
.room-list, .plist { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.room-list li:not(.muted) {
  display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; background: rgba(0, 0, 0, 0.25); border: 1px solid rgba(201, 164, 92, 0.25); cursor: pointer;
}
.room-list li:not(.muted):hover { border-color: var(--gold); }
.code { font-family: var(--display); color: var(--gold-hi); letter-spacing: 0.2em; font-size: 1.2em; }
.room-head { display: flex; justify-content: space-between; align-items: start; gap: 12px; }
.room-body { display: grid; grid-template-columns: 1fr 1.3fr; gap: 28px; }
.plist li { padding: 8px 12px; background: rgba(0, 0, 0, 0.25); border-left: 3px solid var(--gold-dim); }
.plist li.host::after { content: " ♛"; color: var(--gold); }
.settings { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.settings label { display: grid; gap: 4px; font-size: 0.9rem; color: var(--parch-dim); }
@media (max-width: 720px) {
  .room-body { grid-template-columns: 1fr; }
  .settings { grid-template-columns: 1fr; }
}

/* ---------- game ---------- */
#map { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: crosshair; background: #1b2f3a; touch-action: none; }
#map.dragging { cursor: grabbing; }

#hud-top { position: absolute; top: 12px; left: 12px; display: flex; gap: 12px; padding: 10px 14px 10px 10px; align-items: center; min-width: 300px; }
#hud-top .title { font-family: var(--display); color: var(--gold-hi); font-weight: 700; letter-spacing: 0.04em; }
.stats { display: flex; gap: 16px; margin-top: 2px; }
.stats span { display: inline-flex; align-items: baseline; gap: 5px; }
.stats b { font-size: 1.1rem; color: var(--parch); }
.stats small { color: var(--parch-dim); font-size: 0.8rem; }
.troopbar { height: 4px; background: rgba(0, 0, 0, 0.5); margin-top: 6px; border: 1px solid rgba(201, 164, 92, 0.3); }
#troop-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--blood), var(--gold)); transition: width 0.2s; }
.ico { width: 13px; height: 13px; display: inline-block; align-self: center; background: var(--gold); }
.ico.sword { clip-path: polygon(45% 0, 55% 0, 58% 62%, 85% 62%, 85% 70%, 58% 70%, 58% 88%, 66% 100%, 34% 100%, 42% 88%, 42% 70%, 15% 70%, 15% 62%, 42% 62%); }
.ico.coin { border-radius: 50%; background: radial-gradient(circle at 35% 35%, #f3dc95, #b2862f 70%); }
.ico.flag { clip-path: polygon(10% 0, 20% 0, 20% 8%, 95% 8%, 75% 30%, 95% 52%, 20% 52%, 20% 100%, 10% 100%); }

#phase-banner { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); padding: 10px 22px; text-align: center; max-width: min(640px, calc(100% - 24px)); font-family: var(--display); color: var(--gold-hi); letter-spacing: 0.03em; }
#phase-banner small { display: block; font-family: var(--serif); color: var(--parch); letter-spacing: 0; font-size: 0.95rem; margin-top: 2px; }
#my-attacks { position: absolute; top: 96px; left: 12px; list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
#my-attacks li { font-size: 0.85rem; padding: 3px 10px; background: rgba(20, 14, 10, 0.85); border-left: 3px solid var(--blood); }
#my-attacks li.incoming { border-left-color: var(--warn); }

#leaderboard { position: absolute; top: 12px; right: 12px; width: 250px; padding: 8px 10px; max-height: 46vh; overflow: auto; }
#leaderboard h4, #help h4 { font-family: var(--display); margin: 0 0 6px; color: var(--gold); letter-spacing: 0.2em; font-size: 0.75rem; text-transform: uppercase; }
#lb { list-style: none; margin: 0; padding: 0; display: grid; gap: 3px; }
#lb li { display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; align-items: center; font-size: 0.88rem; padding: 2px 4px; cursor: pointer; }
#lb li:hover { background: rgba(201, 164, 92, 0.1); }
#lb li.me { background: rgba(201, 164, 92, 0.16); }
#lb li .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lb li .nm small { color: var(--parch-dim); }
#lb li .pct { color: var(--gold-hi); font-variant-numeric: tabular-nums; }
#lb canvas { width: 18px; height: 22px; }

#hud-bottom { position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); display: flex; gap: 14px; padding: 10px 12px; align-items: center; max-width: calc(100% - 24px); }
.ratio { display: grid; gap: 2px; min-width: 150px; font-size: 0.85rem; }
.ratio input { width: 100%; accent-color: var(--blood); padding: 0; }
.build-bar { display: flex; gap: 6px; }
.bbtn {
  position: relative; width: 78px; padding: 6px 4px 5px; text-align: center; cursor: pointer; background: linear-gradient(180deg, #3d2c1e, #22180f);
  border: 1px solid var(--gold-dim); color: var(--parch); border-radius: 2px;
}
.bbtn:hover { border-color: var(--gold-hi); }
.bbtn.active { border-color: var(--gold-hi); box-shadow: 0 0 0 1px var(--gold-hi), 0 0 14px rgba(232, 207, 143, 0.35); }
.bbtn.poor { opacity: 0.5; }
.bbtn canvas { display: block; margin: 0 auto 2px; }
.bbtn .bn { font-family: var(--display); font-size: 0.68rem; letter-spacing: 0.06em; }
.bbtn .bc { font-size: 0.8rem; color: var(--gold-hi); }
.bbtn kbd { position: absolute; top: 2px; left: 4px; font-size: 0.65rem; color: var(--parch-dim); font-family: var(--serif); }

#log-wrap { position: absolute; left: 12px; bottom: 12px; width: 330px; padding: 6px 8px; }
#log { list-style: none; margin: 0 0 6px; padding: 0; max-height: 170px; overflow: auto; font-size: 0.86rem; display: flex; flex-direction: column; gap: 2px; }
#log li { line-height: 1.25; }
#log li.warn { color: var(--warn); }
#log li.good { color: var(--good); }
#log li.big { color: var(--gold-hi); font-weight: 600; }
#log li.ally { color: var(--ally); }
#log li.chat b { font-weight: 600; }
#chat { width: 100%; padding: 5px 8px; font-size: 0.86rem; }

#toasts { position: absolute; top: 120px; right: 12px; display: grid; gap: 8px; width: 250px; }
.toast { padding: 10px 12px; font-size: 0.9rem; }
.toast .acts { display: flex; gap: 6px; margin-top: 8px; }
.toast .btn { padding: 6px 10px; font-size: 0.7rem; }

#ctx { position: absolute; min-width: 210px; padding: 10px; z-index: 5; }
#ctx .hd { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
#ctx .hd .t { font-family: var(--display); color: var(--gold-hi); font-weight: 700; }
#ctx .hd small { display: block; color: var(--parch-dim); }
#ctx button { display: block; width: 100%; margin-top: 5px; text-align: left; }

#tooltip { position: absolute; pointer-events: none; padding: 4px 8px; font-size: 0.82rem; background: rgba(15, 10, 7, 0.88); border: 1px solid rgba(201, 164, 92, 0.4); white-space: nowrap; z-index: 4; }
#help { position: absolute; right: 12px; bottom: 64px; width: 330px; padding: 10px 12px; font-size: 0.88rem; }
#help ul { margin: 0; padding-left: 18px; display: grid; gap: 4px; }
.small-btn { position: absolute; right: 12px; bottom: 12px; width: 40px; height: 40px; padding: 0; font-size: 1rem; }

.overlay { position: absolute; inset: 0; background: rgba(5, 3, 2, 0.6); display: grid; place-items: center; z-index: 10; }
.end-card { padding: 30px 36px; text-align: center; width: min(460px, calc(100% - 32px)); }
.end-card .btn { margin: 14px 4px 0; }

@media (max-width: 900px) {
  #leaderboard { width: 190px; max-height: 30vh; }
  #log-wrap { width: 240px; bottom: 120px; }
  #log { max-height: 90px; }
  #hud-bottom { flex-wrap: wrap; justify-content: center; }
  .bbtn { width: 64px; }
  #help { bottom: 120px; }
}

/* offline (static hosting): no joining/rooms */
body.offline #in-code, body.offline #btn-join, body.offline .room-list, body.offline .menu-card h3.sub, body.offline #room-url { display: none; }

.credit { font-size: 0.72rem; color: var(--parch-dim); text-align: center; margin: 14px 0 0; line-height: 1.4; }
.credit a { color: var(--gold); }
#ctx .pacts { color: var(--ally); }

/* lobby: player banner */
.profile { display: flex; gap: 14px; align-items: flex-start; padding: 10px; background: rgba(0,0,0,0.22); border: 1px solid rgba(201,164,92,0.25); }
.profile canvas#pf-preview { flex: none; width: 72px; height: 88px; }
.pf-body { display: grid; gap: 6px; flex: 1; min-width: 0; }
.pf-label { font-size: 0.85rem; color: var(--parch-dim); display: grid; gap: 4px; }
.swatches, .charges { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch { width: 26px; height: 26px; border-radius: 50%; cursor: pointer; border: 2px solid rgba(0,0,0,0.6); box-shadow: 0 0 0 1px var(--gold-dim); }
.swatch.on { box-shadow: 0 0 0 2px var(--gold-hi), 0 0 10px rgba(232,207,143,0.5); }
.charges canvas { width: 30px; height: 36px; cursor: pointer; border-radius: 3px; padding: 2px; background: rgba(0,0,0,0.25); border: 1px solid transparent; }
.charges canvas.on { border-color: var(--gold-hi); background: rgba(201,164,92,0.18); }
.plist li { display: flex; align-items: center; gap: 8px; }
.plist li canvas { width: 20px; height: 24px; flex: none; }
.plist .team { margin-left: auto; font-size: 0.75rem; font-family: var(--display); color: var(--gold); letter-spacing: 0.08em; }
.settings label.wide { grid-column: 1 / -1; }

#my-attacks li { display: flex; align-items: center; gap: 8px; }
#my-attacks li.boat { border-left-color: var(--ally); }
#my-attacks button.x { margin-left: auto; padding: 0 7px; line-height: 1.5; font-size: 0.8rem; cursor: pointer; color: var(--gold-hi);
  background: #3a2618; border: 1px solid var(--gold-dim); border-radius: 2px; }
#my-attacks button.x:hover { filter: brightness(1.3); }

.bbtn .bo { font-size: 0.66rem; color: var(--parch-dim); }
#speed-ctl { position: absolute; top: 70px; left: 50%; transform: translateX(-50%); display: flex; gap: 4px; align-items: center; padding: 4px 8px; font-family: var(--display); font-size: 0.72rem; color: var(--gold); }
#speed-ctl button { font-family: var(--display); font-size: 0.72rem; color: var(--parch); background: #2a1d13; border: 1px solid var(--gold-dim); padding: 2px 8px; cursor: pointer; }
#speed-ctl button.on { color: #1a120c; background: var(--gold-hi); }
#pf-realm { padding: 6px 8px; font-size: 0.9rem; }

/* minimap, stats, sound, tutorial, alerts, siege */
#minimap { position: absolute; right: 12px; bottom: 60px; width: 220px; height: 147px; border: 1px solid var(--gold-dim);
  box-shadow: 0 0 0 2px rgba(10,7,5,0.85), 0 6px 22px rgba(0,0,0,0.55); background: #1b2f3a; cursor: pointer; border-radius: 2px; }
#btn-stats { right: 58px; }
#btn-sound { right: 104px; }
#btn-sound.off { opacity: 0.5; }
#help { bottom: 216px; }
.small-tut { padding: 6px 10px; font-size: 0.7rem; margin-bottom: 8px; }
.stats-card { padding: 22px 26px; width: min(900px, calc(100% - 24px)); max-height: 90vh; overflow: auto; text-align: left; }
#stats-chart { width: 100%; height: auto; background: rgba(0,0,0,0.25); border: 1px solid rgba(201,164,92,0.25); }
.stats-table-wrap { overflow-x: auto; margin: 12px 0; }
#stats-table { border-collapse: collapse; width: 100%; font-size: 0.86rem; }
#stats-table th { font-family: var(--display); font-size: 0.68rem; letter-spacing: 0.08em; color: var(--gold); text-align: right; padding: 6px 8px; border-bottom: 1px solid rgba(201,164,92,0.35); white-space: nowrap; }
#stats-table th:first-child, #stats-table td:first-child { text-align: left; }
#stats-table td { padding: 5px 8px; text-align: right; border-bottom: 1px solid rgba(201,164,92,0.12); white-space: nowrap; font-variant-numeric: tabular-nums; }
#stats-table tr.me td { background: rgba(201,164,92,0.14); }
#stats-table tr.dead td { opacity: 0.55; }
#stats-table .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
#log li.goto { cursor: pointer; text-decoration: underline dotted rgba(232,207,143,0.5); }
#log li.goto:hover { color: var(--gold-hi); }
#my-attacks li.incoming { cursor: pointer; }
#tutorial { position: absolute; left: 50%; top: 92px; transform: translateX(-50%); width: min(460px, calc(100% - 24px)); padding: 12px 16px; z-index: 6; }
#tutorial .tut-head { display: flex; justify-content: space-between; align-items: center; font-size: 0.75rem; color: var(--parch-dim); font-family: var(--display); letter-spacing: 0.1em; }
#tutorial .tut-head .btn { padding: 4px 8px; font-size: 0.65rem; }
#tutorial h4 { font-family: var(--display); color: var(--gold-hi); margin: 6px 0; font-size: 1rem; }
#tutorial p { margin: 0 0 10px; line-height: 1.45; }
#tutorial .acts { display: flex; justify-content: flex-end; gap: 6px; }
#tutorial .acts .btn { padding: 6px 12px; font-size: 0.7rem; }
@media (max-width: 900px) { #minimap { width: 150px; height: 100px; bottom: 60px; } #help { bottom: 170px; } }
#stats { z-index: 12; }

/* stats window: always fits the screen; the body scrolls, close button stays visible */
.stats-card { position: relative; display: flex; flex-direction: column; max-height: calc(100vh - 32px); overflow: hidden; }
.stats-card h2 { flex: none; padding-right: 40px; }
.stats-body { flex: 1 1 auto; min-height: 0; overflow: auto; }
.stats-card > #btn-stats-close { flex: none; align-self: flex-start; margin-top: 10px; }
.stats-x { position: absolute; top: 10px; right: 12px; width: 34px; height: 34px; border-radius: 50%; cursor: pointer; font-size: 1rem; line-height: 1;
  color: var(--gold-hi); background: #2a1d13; border: 1px solid var(--gold-dim); z-index: 2; }
.stats-x:hover { filter: brightness(1.35); border-color: var(--gold-hi); }
