:root {
  --ink: #2b251c;
  --line: #5a4d39;
  --blue: #4b7ef1;
  --red: #d75a55;
  --panel: rgba(255, 248, 232, 0.92);
  --panel-strong: rgba(252, 241, 214, 0.96);
  --shadow: rgba(43, 37, 28, 0.2);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  color: var(--ink);
  font-family: "Marker Felt", "Comic Sans MS", "Bradley Hand", cursive;
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.5), transparent 42%),
    linear-gradient(180deg, #efe1bb 0%, #ead5a2 100%);
  -webkit-tap-highlight-color: transparent;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: 0.15;
  background-image:
    linear-gradient(0deg, transparent 23px, rgba(103, 85, 58, 0.2) 24px),
    linear-gradient(90deg, transparent 23px, rgba(103, 85, 58, 0.08) 24px);
  background-size: 24px 24px;
}

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

.app-shell {
  position: fixed;
  inset: 0;
}

.title-bar {
  display: none;
}

.hud-card,
.side-panel,
.unit-guide-card,
.deployment-group,
.deploy-button,
.overlay-card {
  border: 2px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 8px 14px var(--shadow);
}

.game-shell {
  position: absolute;
  inset: 0;
  border: none;
  border-radius: 0;
  padding: 0;
  background: transparent;
  box-shadow: none;
}

.map-column,
.battle-stage,
.canvas-frame {
  position: absolute;
  inset: 0;
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
}

button,
canvas {
  touch-action: manipulation;
}

.hud-row {
  position: absolute;
  top: calc(var(--safe-top) + 6px);
  left: 50%;
  transform: translateX(-50%);
  width: min(66vw, 820px);
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 4px;
  z-index: 5;
  pointer-events: none;
}

.hud-card {
  padding: 6px 8px;
  border-radius: 12px;
  background: var(--panel-strong);
}

.hud-card h3 {
  margin-bottom: 2px;
  font-size: 0.76rem;
  line-height: 1;
}

.hud-lines {
  display: grid;
  gap: 1px;
  font-size: 0.62rem;
  line-height: 1.08;
}

.time-display {
  min-width: 66px;
  padding: 5px 8px;
  border: 2px solid var(--line);
  border-radius: 11px;
  background: rgba(255, 251, 241, 0.86);
  text-align: center;
  font-size: 0.8rem;
  box-shadow: 0 6px 12px var(--shadow);
}

.message-banner {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 8px;
  z-index: 7;
  padding: 6px 10px;
  border: 2px solid rgba(90, 77, 57, 0.38);
  border-radius: 11px;
  background: rgba(255, 251, 241, 0.74);
  font-size: 0.68rem;
  line-height: 1.15;
  text-align: center;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.message-banner.is-visible {
  opacity: 1;
  transform: translateY(0);
}

.deployment-hud {
  position: absolute;
  inset: 0;
  z-index: 6;
  pointer-events: none;
}

.deployment-group {
  position: absolute;
  right: calc(var(--safe-right) + 6px);
  display: inline-flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  width: max-content;
  background: rgba(255, 251, 241, 0.38);
  border-radius: 12px;
  pointer-events: auto;
  transform: translateY(-50%);
}

.deployment-group[data-lane="top"] {
  top: 30%;
}

.deployment-group[data-lane="bottom"] {
  top: 75%;
}

.deployment-group[data-state="pushing"] {
  background: rgba(235, 247, 255, 0.38);
}

.deployment-group[data-state="threatened"] {
  background: rgba(255, 241, 237, 0.38);
}

.deploy-buttons {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 2px;
}

.deploy-button {
  width: 32px;
  min-width: 32px;
  height: 30px;
  padding: 2px 1px;
  border-width: 2px;
  border-radius: 8px;
  color: var(--ink);
  background: linear-gradient(180deg, #fff8dc 0%, #f7e4ae 68%, #edcb78 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.deploy-button.is-disabled {
  opacity: 0.64;
  cursor: not-allowed;
}

.deploy-badge {
  width: 12px;
  height: 12px;
  margin: 0 auto;
}

.deploy-note {
  font-size: 0.4rem;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  max-width: 100%;
}

.deploy-note:empty {
  display: none;
}

.deploy-badge svg,
.unit-icon svg,
.summary-unit svg,
.result-badge svg {
  display: block;
  width: 100%;
  height: 100%;
  stroke: currentColor;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.unit-guide-toggle,
.side-panel-close {
  border: 2px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  font: inherit;
  background: linear-gradient(180deg, #fff7e3, #f3d987);
}

.unit-guide-toggle {
  position: fixed;
  bottom: calc(var(--safe-bottom) + 8px);
  left: calc(var(--safe-left) + 8px);
  z-index: 25;
  padding: 7px 9px;
  font-size: 0.8rem;
}

.side-panel {
  position: fixed;
  top: calc(var(--safe-top) + 6px);
  left: calc(var(--safe-left) + 6px);
  bottom: calc(var(--safe-bottom) + 6px);
  width: min(300px, calc(100vw - var(--safe-left) - var(--safe-right) - 16px));
  padding: 10px;
  background: var(--panel-strong);
  z-index: 26;
  overflow: auto;
  transform: translateX(calc(-100% - 18px));
  opacity: 0;
  pointer-events: none;
  transition: transform 180ms ease, opacity 180ms ease;
}

.side-panel-header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  background: var(--panel-strong);
}

.eyebrow {
  font-size: 0.84rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.side-panel-close {
  padding: 6px 10px;
  font-size: 0.8rem;
}

.side-panel-backdrop {
  position: fixed;
  inset: 0;
  z-index: 24;
  border: 0;
  padding: 0;
  background: rgba(36, 30, 23, 0.32);
  opacity: 0;
  pointer-events: none;
}

body.unit-guide-open .side-panel {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}

body.unit-guide-open .side-panel-backdrop {
  opacity: 1;
  pointer-events: auto;
}

body.unit-guide-open .unit-guide-toggle {
  opacity: 0;
  pointer-events: none;
}

.unit-guide {
  display: grid;
  gap: 6px;
}

.unit-guide-card {
  display: grid;
  gap: 2px;
  padding: 7px 9px 8px;
  background: linear-gradient(180deg, rgba(255, 247, 223, 0.96), rgba(245, 228, 179, 0.98));
}

.unit-name,
.unit-meta,
.unit-stats,
.unit-role,
.unit-matchup {
  font-size: 0.78rem;
}

.unit-name,
.unit-meta {
  display: flex;
  justify-content: space-between;
  gap: 6px;
}

.unit-name strong {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.unit-icon {
  width: 20px;
  height: 20px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  border: 1px solid rgba(90, 77, 57, 0.22);
  background: rgba(75, 126, 241, 0.12);
}

.unit-stats {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.unit-chip {
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 0.72rem;
  background: rgba(90, 77, 57, 0.08);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding:
    calc(var(--safe-top) + 8px)
    max(10px, var(--safe-right))
    calc(var(--safe-bottom) + 8px)
    max(10px, var(--safe-left));
  background: rgba(36, 30, 23, 0.45);
  backdrop-filter: blur(5px);
}

.hidden {
  display: none;
}

.overlay-card {
  width: min(90vw, 760px);
  max-height: calc(100dvh - var(--safe-top) - var(--safe-bottom) - 16px);
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(254, 246, 225, 0.98);
  overflow: auto;
}

#overlayTitle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1.3rem;
}

.overlay-title-score {
  display: flex;
  align-items: baseline;
  gap: 5px;
  margin-left: auto;
}

.overlay-title-score-label {
  color: rgba(42, 33, 21, 0.6);
  font-size: 0.7rem;
  font-weight: normal;
}

.overlay-title-score-value {
  font-size: 1.3rem;
}

.result-badge {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 2px solid rgba(90, 77, 57, 0.28);
  border-radius: 10px;
  background: rgba(255, 251, 241, 0.9);
}

.result-badge svg {
  width: 20px;
  height: 20px;
}

.overlay[data-result="win"] .result-badge {
  color: #9f6a12;
}

.overlay[data-result="lose"] .result-badge {
  color: #8c3b37;
}

.result-score {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 6px;
  padding: 6px 10px;
  border: 2px solid rgba(90, 77, 57, 0.34);
  border-radius: 12px;
  background: rgba(255, 251, 241, 0.72);
  font-size: 0.86rem;
}

.result-score-team {
  min-width: 7ch;
}

.result-score-red {
  color: var(--red);
  text-align: right;
}

.result-score-blue {
  color: var(--blue);
}

.overlay-subtitle {
  margin-top: 4px;
  font-size: 0.5rem;
  line-height: 1.4;
}

.summary-grid {
  display: grid;
  gap: 8px;
  margin: 8px 0 10px;
}

.summary-card {
  padding: 8px;
  border: 1px solid rgba(90, 77, 57, 0.22);
  border-radius: 12px;
  background: rgba(255, 251, 241, 0.88);
}

.summary-card h3 {
  margin-bottom: 4px;
  font-size: 0.88rem;
}

.summary-scoreboard,
.summary-kpis {
  display: grid;
  gap: 6px;
}

.summary-kpis {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.summary-score,
.summary-kpi {
  display: grid;
  gap: 2px;
  padding: 7px 8px;
  border: 1px solid rgba(90, 77, 57, 0.14);
  border-radius: 10px;
  background: rgba(255, 248, 232, 0.82);
}

.summary-score-label,
.summary-kpi-label,
.summary-score-formula {
  color: rgba(42, 33, 21, 0.72);
  font-size: 0.68rem;
}

.summary-score-value {
  font-size: 1.2rem;
}

.summary-kpi-value {
  font-size: 0.86rem;
}

.summary-line {
  padding: 2px 0;
  border-bottom: 1px dashed rgba(90, 77, 57, 0.28);
  font-size: 0.66rem;
  line-height: 1.1;
}

.summary-line:last-child {
  border-bottom: 0;
}

.summary-line-columns,
.summary-line-unit {
  display: grid;
  grid-template-columns: 22px repeat(4, minmax(34px, 1fr));
  align-items: center;
  gap: 5px;
}

.summary-column-title,
.summary-team-value {
  text-align: center;
}

.summary-column-title {
  font-size: 0.62rem;
}

.summary-unit {
  width: 21px;
  height: 21px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(90, 77, 57, 0.16);
  border-radius: 999px;
  background: rgba(90, 77, 57, 0.08);
}

.summary-unit svg {
  width: 13px;
  height: 13px;
}

.summary-team-label-red,
.summary-team-value-red {
  color: var(--red);
}

.summary-team-label-blue,
.summary-team-value-blue {
  color: var(--blue);
}

.restart-button {
  width: 100%;
  padding: 8px 12px;
  border: 2px solid var(--line);
  border-radius: 12px;
  color: var(--ink);
  font: inherit;
  font-size: 0.8rem;
  background: linear-gradient(180deg, #fff7e3, #f3d987);
}

.rotate-hint {
  position: fixed;
  left: 50%;
  bottom: calc(var(--safe-bottom) + 12px);
  transform: translateX(-50%);
  max-width: 320px;
  padding: 10px 14px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 248, 232, 0.96);
  box-shadow: 0 8px 14px var(--shadow);
  font-size: 0.9rem;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  z-index: 45;
}

.rotate-hint.is-visible {
  opacity: 1;
}

html.ios-app,
html.ios-app body {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

@media (max-width: 900px) {
  .game-shell {
    padding: 6px;
  }

  .hud-row {
    width: min(82vw, 680px);
  }

  .deploy-button {
    width: 30px;
    min-width: 30px;
    height: 28px;
  }

  .overlay-card {
    width: min(96vw, 740px);
    padding: 10px;
  }

  .summary-kpis {
    grid-template-columns: 1fr;
  }
}

@media (orientation: landscape) and (min-width: 901px) and
  (max-aspect-ratio: 3 / 2) and (pointer: coarse) {
  .app-shell {
    background: linear-gradient(
      180deg,
      #f7efd3 0%,
      #f7efd3 50%,
      #c4a269 50%,
      #c4a269 100%
    );
  }

  .battle-stage {
    inset: auto;
    top: 50%;
    left: 50%;
    width: 100vw;
    height: 56.25vw;
    transform: translate(-50%, -50%);
  }

  .hud-row {
    top: calc(var(--safe-top) + 10px);
    width: min(92vw, 1080px);
    gap: 8px;
  }

  .hud-card {
    padding: 10px 14px;
  }

  .hud-card h3 {
    margin-bottom: 4px;
    font-size: 1rem;
  }

  .hud-lines {
    gap: 2px;
    font-size: 0.82rem;
    line-height: 1.15;
  }

  .time-display {
    min-width: 82px;
    padding: 9px 12px;
    font-size: 1rem;
  }

  .deployment-group {
    right: calc(var(--safe-right) + 12px);
    gap: 4px;
    padding: 5px;
  }

  .deploy-buttons {
    gap: 4px;
  }

  .deploy-button {
    width: 44px;
    min-width: 44px;
    height: 42px;
    padding: 3px 2px;
    border-radius: 10px;
  }

  .deploy-badge {
    width: 18px;
    height: 18px;
  }

  .deploy-note {
    font-size: 0.55rem;
  }

  .unit-guide-toggle {
    bottom: calc(var(--safe-bottom) + 12px);
    left: calc(var(--safe-left) + 12px);
    padding: 10px 14px;
    font-size: 1rem;
  }

  .side-panel {
    width: min(
      360px,
      calc(100vw - var(--safe-left) - var(--safe-right) - 24px)
    );
    padding: 14px;
  }
}

@media (orientation: portrait) {
  .app-shell {
    opacity: 0.8;
  }
}
