:root {
  --chrome: #2a1a14;
  --bar: #3a241c;
  --text: #f7f1e6;
  --muted: rgba(247, 241, 230, 0.68);
  --accent: #1e4ba8;
  --accent-dark: #153a7a;
  --light: #f4efe4;
  --dark: #2f62d6;
  --last-dark: #e0b14a;
  --last-light: #f0d080;
  --hint: rgba(232, 176, 72, 0.55);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --tap: 44px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--chrome); color: var(--text);
  overflow: hidden;
}
button, h1, h2, p { margin: 0; }
button {
  font: inherit; color: inherit; appearance: none; border: 0; background: none;
  cursor: pointer;
}
#app, .screen {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
}
.screen[hidden] { display: none !important; }
.screen.active { display: block; }

/* Map */
#screen-map { background: #c9844a; }
#map-stage {
  position: absolute; inset: 0;
  overflow: hidden;
}
#map-bg {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  object-position: 50% 42%;
  display: block;
  pointer-events: none;
  user-select: none;
}
#map-layer { position: absolute; left: 0; top: 0; pointer-events: none; }
#map-layer .tile, #map-layer .dad { position: absolute; pointer-events: auto; }
#map-layer .tile { filter: drop-shadow(0 6px 8px rgba(0,0,0,0.35)); }
#map-layer .tile img, #map-layer .dad img {
  width: 100%; height: 100%; display: block; object-fit: contain;
  pointer-events: none;
}
#map-layer .tile { min-width: 44px; min-height: 44px; }
#map-layer .num {
  position: absolute; left: 50%; top: 42%;
  transform: translate(-50%, -50%);
  min-width: 22px; height: 18px; padding: 0 6px;
  border-radius: 9px;
  background: rgba(58, 36, 28, 0.82);
  color: var(--text);
  font-size: 11px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}
#map-layer .dad { z-index: 5; pointer-events: none; filter: drop-shadow(0 4px 6px rgba(0,0,0,0.45)); }

.map-ui {
  position: absolute; inset: 0;
  padding: calc(10px + var(--safe-t)) 12px calc(16px + var(--safe-b));
  pointer-events: none;
  display: flex; flex-direction: column; justify-content: space-between;
  align-items: center;
}
.top-bar, .board-header, .board-bar {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; gap: 8px;
  pointer-events: auto;
}
.badge {
  min-width: 78px; height: var(--tap);
  padding: 0 16px;
  border-radius: 22px;
  background: rgba(58, 36, 28, 0.82);
  display: flex; align-items: center; justify-content: center;
  font-weight: 650; font-size: 15px;
  letter-spacing: 0.02em;
}
.icon-btn, .bar-btn, .menu-btn {
  min-height: var(--tap); min-width: var(--tap);
  padding: 0 14px;
  border-radius: 22px;
  background: rgba(58, 36, 28, 0.86);
  font-weight: 600; font-size: 14px;
}
.icon-btn:active, .bar-btn:active, .menu-btn:active, .play-btn:active {
  transform: translateY(1px);
}
.play-btn {
  pointer-events: auto;
  min-height: 52px; min-width: 168px;
  padding: 0 28px;
  border-radius: 18px;
  background: var(--accent);
  box-shadow: 0 4px 0 var(--accent-dark), 0 8px 18px rgba(0,0,0,0.25);
  font-weight: 750; font-size: 18px;
  color: #f7f1e6;
  align-self: flex-end;
  margin-right: 8px;
}
.play-btn:disabled { opacity: 0.55; }

/* Board */
#screen-board { background: var(--chrome); }
.board-chrome {
  height: 100%;
  display: flex; flex-direction: column;
  padding: calc(8px + var(--safe-t)) 0 calc(8px + var(--safe-b));
}
.board-header { padding: 0 10px 8px; }
.board-titles { flex: 1; text-align: center; min-width: 0; padding: 0 6px; }
.game-line {
  font-size: 12px; color: var(--muted);
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
#board-turn { font-size: 18px; font-weight: 750; line-height: 1.25; }
#board-wrap {
  flex: 0 0 auto;
  display: flex; align-items: flex-start; justify-content: center;
  min-height: 0; width: 100%;
  padding: 0;
}
#board {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  max-height: calc(100dvh - 160px);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  user-select: none;
  touch-action: none;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}
#board.shake { animation: shake 0.42s ease; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-9px); }
  40% { transform: translateX(8px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(3px); }
}
.sq {
  position: relative;
  width: 100%; height: 100%;
}
.sq.light { background: var(--light); }
.sq.dark { background: var(--dark); }
.sq.last.light { background: var(--last-light); }
.sq.last.dark { background: var(--last-dark); }
.sq.sel { outline: 3px solid #f7f1e6; outline-offset: -3px; }
.sq.hint-from { box-shadow: inset 0 0 0 4px #e8b048; }
.sq.hint-to { background: #e8b048; }
.coord {
  position: absolute; font-size: 9px; font-weight: 600; line-height: 1;
  pointer-events: none; opacity: 0.55;
}
.coord.file { right: 2px; bottom: 2px; }
.coord.rank { left: 2px; top: 2px; }
.sq.light .coord { color: #3a241c; }
.sq.dark .coord { color: #f7f1e6; }
.piece {
  position: absolute; left: 4%; width: 92%;
  bottom: 1%; height: 96%;
  background: center bottom / contain no-repeat;
  pointer-events: none;
  z-index: 2;
}
.piece.tall { height: 108%; }
.dot {
  position: absolute; left: 50%; top: 50%;
  width: 22%; height: 22%; min-width: 10px; min-height: 10px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: rgba(42, 26, 20, 0.28);
  z-index: 1; pointer-events: none;
}
.sq.dark .dot { background: rgba(247, 241, 230, 0.38); }
.dot.cap {
  width: 72%; height: 72%;
  background: transparent;
  border: 4px solid rgba(42, 26, 20, 0.32);
  border-radius: 50%;
}
.sq.dark .dot.cap { border-color: rgba(247, 241, 230, 0.45); }
.board-bar { padding: 10px 12px 4px; gap: 10px; }
.bar-btn { flex: 1; background: rgba(255,255,255,0.08); }

/* Card */
#screen-card {
  background: var(--chrome);
  display: flex; align-items: center; justify-content: center;
  padding: calc(20px + var(--safe-t)) 20px calc(20px + var(--safe-b));
}
.card-sheet {
  width: min(420px, 100%);
  max-height: 100%;
  overflow: auto;
  background: #3a241c;
  border-radius: 20px;
  padding: 28px 22px 22px;
  display: flex; flex-direction: column; gap: 14px;
}
.card-kicker {
  font-size: 12px; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--muted); font-weight: 700;
}
#card-title {
  font-size: 22px; font-weight: 750; line-height: 1.25;
  overflow-wrap: anywhere;
}
.card-game, .card-lesson {
  font-size: 16px; line-height: 1.45;
  overflow-wrap: break-word;
}
.card-lesson { color: var(--text); }
.card-game { color: var(--muted); }
.card-sheet .play-btn { align-self: stretch; min-width: 0; margin-top: 8px; }

/* Menu */
.menu {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(10, 6, 4, 0.55);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.menu[hidden] { display: none !important; }
.menu-sheet {
  width: min(360px, 100%);
  background: #3a241c;
  border-radius: 20px;
  padding: 24px 18px 18px;
  display: flex; flex-direction: column; gap: 10px;
}
#menu-title { font-size: 22px; font-weight: 750; }
.menu-sub { color: var(--muted); margin-bottom: 8px; }
.menu-btn { width: 100%; background: rgba(255,255,255,0.08); text-align: center; }
.menu-btn.danger { background: rgba(140, 40, 28, 0.55); }

/* Landscape */
@media (orientation: landscape) and (max-height: 520px) {
  #map-bg { object-fit: contain; object-position: center; background: #c9844a; }
  .map-ui {
    flex-direction: row;
    align-items: stretch;
    justify-content: space-between;
    padding: 10px calc(12px + var(--safe-r)) 10px calc(12px + var(--safe-l));
  }
  .top-bar {
    flex-direction: column;
    width: auto; align-items: flex-start;
  }
  .play-btn { align-self: flex-end; }

  .board-chrome {
    display: grid;
    grid-template-columns: minmax(0, auto) minmax(150px, 1fr);
    grid-template-rows: auto minmax(0, 1fr);
    height: 100%;
    align-items: stretch;
    padding: 6px calc(8px + var(--safe-r)) 6px calc(8px + var(--safe-l));
    gap: 0 8px;
  }
  .board-header {
    grid-column: 1 / -1;
    width: 100%;
    padding: 0 4px 6px;
  }
  #board-wrap {
    grid-column: 1;
    grid-row: 2;
    width: min(calc(100vh - 64px), 58vw);
    height: auto;
    max-height: 100%;
    aspect-ratio: 1 / 1;
  }
  #board { width: 100%; height: 100%; max-height: none; }
  .board-bar {
    grid-column: 2;
    grid-row: 2;
    flex-direction: column;
    justify-content: center;
    padding: 8px 12px;
    min-width: 120px;
    width: auto;
  }
  .bar-btn { width: 100%; flex: 0 0 auto; height: 48px; max-height: 48px; }
  #board-turn { font-size: 16px; }
  .play-btn { align-self: flex-end; }
  .card-sheet { max-height: calc(100dvh - 24px); padding: 16px 18px; gap: 10px; }
  #card-title { font-size: 18px; }
  .card-game, .card-lesson { font-size: 15px; }
}
