:root {
  --bg: #0d1320;
  --panel: rgba(10, 16, 28, 0.88);
  --panel-border: rgba(255, 255, 255, 0.1);
  --text: #f0ecdf;
  --muted: #9ba5b5;
  --accent: #e1b866;
  --accent-dim: rgba(225, 184, 102, 0.18);
  --red: #d5544f;
  --blue: #4b87d9;
  --green: #5cb87a;
  --plain: #6a8b5b;
  --forest: #335c3c;
  --mountain: #5d626f;
  --chest: #b68a2f;
  --base: #3c748c;
  --fog: rgba(255, 255, 255, 0.06);
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.45);
  --shadow-sm: 0 4px 18px rgba(0, 0, 0, 0.3);
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 12px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Trebuchet MS", "Hiragino Sans", sans-serif;
  color: var(--text);
  background:
    radial-gradient(ellipse at top left, rgba(204, 147, 74, 0.18), transparent 40%),
    radial-gradient(ellipse at bottom right, rgba(75, 135, 217, 0.12), transparent 40%),
    linear-gradient(180deg, #1a2132 0%, #0a0f1a 100%);
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.14); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.24); }

/* ── Base ── */
button {
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  cursor: pointer;
  padding: 0.7rem 1rem;
  font: inherit;
  font-size: 0.88rem;
  transition: transform 120ms ease, background 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}

button:hover {
  transform: translateY(-1px);
  background: rgba(255, 255, 255, 0.11);
  border-color: rgba(255, 255, 255, 0.22);
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  opacity: 0.35;
  cursor: default;
  transform: none;
  box-shadow: none;
}

.primary {
  background: linear-gradient(135deg, #d48c33, #9e4a1c);
  border-color: rgba(255, 220, 160, 0.35);
  font-weight: 600;
}

.primary:hover {
  background: linear-gradient(135deg, #e09a40, #ae5424);
  border-color: rgba(255, 220, 160, 0.5);
}

h1, h2, h3, p { margin: 0; }

h1 { font-size: clamp(1.8rem, 2.4vw, 2.8rem); }

h2 {
  font-size: 0.9rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text);
}

.hidden { display: none !important; }

/* ── Lobby ── */
.app-shell {
  padding: 20px;
  min-height: 100vh;
  position: relative;
  overflow: hidden;
}

.lobby-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.lobby-card {
  width: min(520px, 100%);
  padding: 32px;
  border-radius: 28px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
}

.lobby-copy {
  margin-top: 14px;
  color: var(--muted);
  line-height: 1.6;
}

.lobby-actions { margin-top: 22px; }
.lobby-status-card { margin-top: 18px; }

/* ── Topbar ── */
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: stretch;
  margin-bottom: 20px;
  padding: 16px 20px;
  border-radius: var(--radius-lg);
  background:
    linear-gradient(135deg, rgba(255,255,255,0.05), rgba(255,255,255,0.02)),
    radial-gradient(circle at top left, rgba(225, 184, 102, 0.14), transparent 38%),
    rgba(8, 13, 22, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
  position: relative;
  z-index: 40;
}

.app-shell.topbar-collapsed .topbar {
  margin-bottom: 10px;
  align-items: center;
}

.app-shell.topbar-collapsed .topbar-stats { display: none; }

.topbar-brand {
  display: grid;
  align-content: space-between;
  gap: 10px;
  min-width: 0;
  flex: 1 1 380px;
}

.eyebrow {
  margin: 0 0 4px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-size: 0.68rem;
  color: var(--accent);
  opacity: 0.8;
}

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

.topbar-summary {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.topbar-chip {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  max-width: 100%;
  padding: 5px 12px;
  border-radius: 999px;
  border: 1px solid rgba(225, 184, 102, 0.2);
  background: rgba(225, 184, 102, 0.07);
  color: var(--text);
  font-size: 0.82rem;
  line-height: 1.25;
}

.topbar-chip.muted {
  border-color: rgba(255, 255, 255, 0.07);
  background: rgba(255, 255, 255, 0.05);
  color: var(--muted);
}

/* ── Stat Cards ── */
.topbar-stats {
  display: grid;
  grid-template-columns: repeat(5, minmax(120px, 1fr));
  gap: 10px;
  min-width: min(100%, 700px);
  flex: 1.3 1 700px;
}

.stat-card,
.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(18px);
}

.stat-card {
  border-radius: var(--radius-md);
  padding: 12px 14px;
  min-height: 82px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background:
    linear-gradient(180deg, rgba(255,255,255,0.05), rgba(255,255,255,0.01)),
    rgba(8, 14, 24, 0.78);
  position: relative;
  overflow: hidden;
}

.stat-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: rgba(255,255,255,0.08);
  border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.stat-label {
  display: block;
  color: var(--muted);
  font-size: 0.68rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: 6px;
}

.stat-card strong {
  display: block;
  font-size: 1.2rem;
  font-weight: 700;
  line-height: 1.2;
}

.stat-card-turn::before { background: linear-gradient(90deg, var(--accent), transparent); }
.stat-card-turn { border-color: rgba(225, 184, 102, 0.22); }
.stat-card-turn strong { color: var(--accent); }

.stat-card-timer::before { background: linear-gradient(90deg, #f5c842, transparent); }
.stat-card-timer strong {
  color: #f5d47a;
  font-variant-numeric: tabular-nums;
  font-size: 1.35rem;
}

.stat-card-team::before { background: linear-gradient(90deg, var(--blue), transparent); }
.stat-card-team {
  background:
    radial-gradient(circle at top right, rgba(75, 135, 217, 0.12), transparent 50%),
    linear-gradient(180deg, rgba(255,255,255,0.05), rgba(255,255,255,0.01)),
    rgba(8, 14, 24, 0.78);
}
.stat-card-team strong { color: #7ab2f0; }

.stat-card-score::before { background: linear-gradient(90deg, rgba(255,255,255,0.2), transparent); }

.stat-card-funds::before { background: linear-gradient(90deg, var(--green), transparent); }
.stat-card-funds strong { color: #7ed09e; font-size: 1.2rem; }

/* ── Layout ── */
.layout {
  position: relative;
  min-height: calc(100vh - 192px);
}

.app-shell.topbar-collapsed .layout { min-height: calc(100vh - 110px); }

.sidebar {
  display: grid;
  gap: 14px;
  position: fixed;
  top: 192px;
  bottom: 20px;
  width: 310px;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: 35;
  padding-right: 4px;
}

.app-shell.topbar-collapsed .sidebar { top: 110px; }

.left-panel { left: 20px; }
.right-panel { right: 20px; }

/* ── Panels ── */
.panel {
  border-radius: var(--radius-lg);
  padding: 14px 16px;
}

.panel-heading,
.board-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}

.panel-heading {
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.panel-copy {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
  margin-bottom: 12px;
}

/* ── End Turn Button ── */
.end-turn-btn {
  display: block;
  width: 100%;
  margin-top: 14px;
  padding: 0.9rem 1rem;
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, #c97f28, #8c3e18);
  border-color: rgba(255, 210, 140, 0.3);
  box-shadow: 0 4px 16px rgba(200, 100, 20, 0.3);
}

.end-turn-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #de8e32, #9e4820);
  box-shadow: 0 6px 20px rgba(200, 100, 20, 0.45);
}

.end-turn-btn:disabled {
  background: rgba(255,255,255,0.08);
  border-color: rgba(255,255,255,0.1);
  box-shadow: none;
}

/* ── Unit List ── */
.unit-list,
.log-list,
.info-block,
.item-box-list {
  display: grid;
  gap: 8px;
}

.log-list {
  max-height: 260px;
  overflow-y: auto;
  padding-right: 2px;
}

/* ── Cards ── */
.unit-card,
.log-entry,
.info-card {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.03);
  padding: 12px;
}

.unit-card {
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease, transform 100ms ease;
}

.unit-card:hover {
  background: rgba(255, 255, 255, 0.06);
  transform: translateX(2px);
}

.unit-card.planned {
  border-color: rgba(225, 184, 102, 0.38);
  background: rgba(225, 184, 102, 0.05);
}

.unit-card.unplanned { border-color: rgba(255, 255, 255, 0.07); }

.unit-card.selected {
  border-color: rgba(225, 184, 102, 0.75);
  background: rgba(225, 184, 102, 0.08);
  box-shadow: 0 0 0 1px rgba(225, 184, 102, 0.25);
}

.unit-card.red { border-left: 3px solid var(--red); }
.unit-card.blue { border-left: 3px solid var(--blue); }

.unit-card-header {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  margin-bottom: 6px;
}

.unit-card h3,
.info-card h3 {
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 0;
}

.unit-card-header h3 { margin-bottom: 0; }

.unit-plan-badge {
  flex-shrink: 0;
  font-size: 0.7rem;
  font-weight: 600;
  line-height: 1;
  border-radius: 999px;
  padding: 4px 8px;
}

.unit-plan-badge.planned {
  background: rgba(225, 184, 102, 0.16);
  color: #f0cc7a;
  border: 1px solid rgba(225, 184, 102, 0.28);
}

.unit-plan-badge.unplanned {
  background: rgba(255, 255, 255, 0.06);
  color: var(--muted);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

/* ── Strength Bar ── */
.unit-strength-bar {
  height: 3px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  margin-top: 8px;
  overflow: hidden;
}

.unit-strength-fill {
  height: 100%;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--blue), #7ab0e8);
  transition: width 300ms ease;
}

.unit-card.red .unit-strength-fill {
  background: linear-gradient(90deg, var(--red), #e87575);
}

/* ── Meta / Tags ── */
.meta,
.unit-tags,
.legend,
.info-list,
.zoom-controls,
.board-header-side {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.board-header-side {
  justify-content: flex-end;
  align-items: center;
}

.zoom-controls { align-items: center; }

.zoom-controls button {
  min-width: 36px;
  padding: 0.38rem 0.6rem;
  font-size: 1rem;
}

#zoomLabel {
  min-width: 56px;
  text-align: center;
  font-size: 0.82rem;
  color: var(--text);
  border-radius: 999px;
  padding: 4px 8px;
  background: rgba(255, 255, 255, 0.08);
}

.meta span,
.unit-tags span,
.legend span,
.pill {
  font-size: 0.76rem;
  border-radius: 999px;
  padding: 3px 8px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--muted);
}

/* ── Log ── */
.log-entry {
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--muted);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  border-left: 2px solid rgba(255,255,255,0.08);
  border-top: none;
  border-right: none;
  border-bottom: none;
  background: rgba(255,255,255,0.025);
}

/* ── Action Grid ── */
.action-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

#cancelButton {
  grid-column: 1 / -1;
}

.action-hint {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid rgba(255, 255, 255, 0.07);
  color: var(--muted);
  line-height: 1.5;
  font-size: 0.84rem;
}

.action-tips { display: grid; gap: 6px; margin-top: 8px; }

.action-tip {
  font-size: 0.8rem;
  color: var(--muted);
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.04);
}

/* ── Room Status ── */
#roomStatus { line-height: 1.5; font-size: 0.88rem; }

/* ── Squad List ── */
.squad-list { display: grid; gap: 6px; margin-top: 8px; }

.squad-row {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) auto auto;
  gap: 8px;
  align-items: center;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.04);
  color: var(--muted);
  font-size: 0.83rem;
}

.squad-row-clickable { cursor: pointer; transition: background 120ms ease; }
.squad-row-clickable:hover { background: rgba(255, 255, 255, 0.09); }
.squad-row strong { color: var(--text); }

/* ── Board ── */
.board-panel {
  position: fixed;
  top: 172px;
  right: 0;
  bottom: 0;
  left: 0;
  z-index: 5;
  min-width: 0;
  pointer-events: none;
}

.app-shell.topbar-collapsed .board-panel { top: 90px; }

.board-header {
  position: fixed;
  top: 172px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 25;
  width: min(640px, calc(100vw - 720px));
  min-width: 300px;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: rgba(10, 16, 28, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.09);
  box-shadow: var(--shadow-sm);
  backdrop-filter: blur(18px);
  pointer-events: auto;
}

.app-shell.topbar-collapsed .board-header { top: 90px; }

.board-header h2 {
  font-size: 0.88rem;
  margin-bottom: 2px;
}

.board-header p {
  color: var(--muted);
  margin-top: 3px;
  font-size: 0.78rem;
}

.board-frame {
  position: absolute;
  inset: 0;
  border-radius: var(--radius-lg);
  overflow: hidden;
  background: #0a1118;
  border: 1px solid rgba(255, 255, 255, 0.07);
  box-shadow: var(--shadow), inset 0 0 80px rgba(0, 0, 0, 0.6);
  cursor: grab;
  user-select: none;
  pointer-events: auto;
}

.board-frame.panning { cursor: grabbing; }

#hexBoard {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ── Hint Popup ── */
.board-hint-popup {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 8500;
  width: min(740px, calc(100vw - 32px));
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  border-radius: var(--radius-md);
  background: rgba(8, 13, 22, 0.94);
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
}

.board-hint-popup-text { color: var(--text); line-height: 1.5; font-size: 0.9rem; }

/* ── Guide Popup ── */
.guide-popup {
  position: fixed;
  top: 16px;
  left: 16px;
  z-index: 8600;
  width: min(400px, calc(100vw - 32px));
  padding: 16px;
  border-radius: var(--radius-lg);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
}

.guide-card { display: grid; gap: 10px; }

.guide-step {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
}

.guide-step-number {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(225, 184, 102, 0.16);
  color: var(--accent);
  font-weight: 700;
  font-size: 0.85rem;
}

.guide-step strong { display: block; color: var(--text); margin-bottom: 2px; font-size: 0.9rem; }
.guide-step p { color: var(--muted); line-height: 1.45; font-size: 0.83rem; }

/* ── Tile ── */
.tile { cursor: pointer; transition: filter 120ms ease; }
.tile:hover { filter: brightness(1.15); }

.tile.available-spawn { animation: spawn-pulse 1.4s ease-in-out infinite alternate; }

@keyframes spawn-pulse {
  from { filter: brightness(1.1); }
  to   { filter: brightness(1.45); }
}

.tile-label {
  font-size: 0.8rem;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.tile-sub {
  font-size: 0.64rem;
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
  -webkit-user-select: none;
}

.tile-building {
  font-size: 0.7rem;
  font-weight: 800;
  fill: rgba(255, 244, 205, 0.96);
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
}

.unit-counter,
.reservation-mark { pointer-events: none; user-select: none; -webkit-user-select: none; }

.unit-counter {
  font-size: 0.84rem;
  font-weight: 700;
  fill: #ffffff;
  text-anchor: middle;
  pointer-events: none;
  paint-order: stroke fill;
  stroke: rgba(8, 12, 20, 0.92);
  stroke-width: 3px;
  stroke-linejoin: round;
}

.reservation-mark {
  font-size: 0.76rem;
  font-weight: 800;
  fill: #0d1320;
  text-anchor: middle;
  pointer-events: none;
}

/* ── Tile Popup ── */
.tile-popup {
  position: absolute;
  z-index: 300;
  background: rgba(8, 14, 24, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  backdrop-filter: blur(20px);
  box-shadow: var(--shadow);
  min-width: 170px;
  max-width: 250px;
  pointer-events: auto;
}

.tile-popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.tile-popup-header strong { font-size: 0.95rem; }

.tile-popup-close {
  padding: 2px 6px;
  border-radius: 8px;
  font-size: 0.85rem;
  line-height: 1.4;
}

.tile-popup-body {
  display: grid;
  gap: 3px;
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.45;
}

/* ── Digest Overlay ── */
.digest-overlay {
  position: absolute;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  z-index: 280;
  min-width: min(540px, calc(100% - 32px));
  max-width: calc(100% - 32px);
  padding: 14px 18px;
  border-radius: var(--radius-md);
  border: 1px solid rgba(255, 255, 255, 0.13);
  background: rgba(8, 13, 22, 0.84);
  box-shadow: var(--shadow);
  text-align: center;
  backdrop-filter: blur(18px);
}

.digest-overlay strong { display: block; margin-bottom: 6px; color: var(--accent); }

/* ── Item/Split/Merge/Recruit/Build Box ── */
.item-box-panel {
  position: fixed;
  inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  width: min(520px, calc(100vw - 32px));
  max-height: min(72vh, 700px);
  overflow: auto;
  z-index: 8000;
  border-color: rgba(225, 184, 102, 0.25);
}

.item-card {
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.03);
  padding: 12px;
  display: grid;
  gap: 8px;
}

.item-card-header {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.item-card p { color: var(--muted); line-height: 1.45; font-size: 0.85rem; }
.item-card button { width: 100%; }

.item-card .danger-soft {
  border-color: rgba(255, 215, 140, 0.24);
  background: rgba(225, 184, 102, 0.1);
}

/* ── Legend ── */
.legend i {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  display: inline-block;
  vertical-align: middle;
  margin-right: 3px;
}

.legend-plain    { background: var(--plain); }
.legend-sea      { background: #214e72; }
.legend-lake     { background: #2f6a8e; }
.legend-forest   { background: var(--forest); }
.legend-mountain { background: var(--mountain); }
.legend-town     { background: #926f52; }
.legend-chest    { background: var(--chest); }
.legend-base     { background: var(--base); }

.empty { color: var(--muted); padding: 6px 0; font-size: 0.88rem; }

/* ── Responsive ── */
@media (max-width: 1180px) {
  .app-shell { overflow: auto; }
  .layout { min-height: auto; }

  .topbar {
    flex-direction: column;
    padding: 14px 16px;
  }

  .topbar-brand,
  .topbar-stats { width: 100%; }

  .topbar-stats {
    min-width: 0;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  }

  .sidebar {
    position: relative;
    inset: auto;
    width: auto;
    overflow: visible;
    padding-right: 0;
  }

  .board-panel {
    position: relative;
    inset: auto;
    pointer-events: auto;
    min-height: 60vh;
    margin-bottom: 16px;
  }

  .board-header {
    position: relative;
    top: auto;
    left: auto;
    transform: none;
    width: auto;
    min-width: 0;
    margin-bottom: 10px;
  }

  .board-frame {
    position: relative;
    min-height: 60vh;
  }

  #hexBoard { height: 60vh; }
}
