:root {
  color-scheme: dark;
  --bg: #171b21;
  --panel: #242a31;
  --panel-2: #2a3038;
  --line: #3b434d;
  --text: #e7edf3;
  --muted: #9aa5b1;
  --accent: #f4d35e;
  --green: #4ea878;
  --danger: #ef7b72;
  --cell-light: #d7d2c4;
  --cell-dark: #8b8f92;
  --cell-size: min(10.2vw, 58px);
  font-family: Inter, "Yu Gothic UI", "Yu Gothic", system-ui, sans-serif;
  background: var(--bg);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }

body {
  min-height: 100%;
  margin: 0;
  background:
    radial-gradient(circle at 50% -12%, rgba(78, 124, 154, .25), transparent 42%),
    var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

button, select, input, textarea { font: inherit; }
button { touch-action: manipulation; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid rgba(255,255,255,.08);
  background: rgba(23, 27, 33, .9);
  backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 20;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 17px; letter-spacing: .04em; }
h1 span { color: var(--muted); font-size: 14px; font-weight: 600; }
h2 { font-size: 17px; }
h3 { font-size: 14px; line-height: 1.45; }
.eyebrow, .section-kicker { color: var(--accent); font-size: 9px; font-weight: 800; letter-spacing: .18em; }
.eyebrow { margin-bottom: 3px; }

main { width: min(100%, 640px); margin: 0 auto; padding: 14px 12px calc(36px + env(safe-area-inset-bottom)); }

.board-section { display: grid; justify-items: center; }
.board {
  display: grid;
  grid-template-columns: 22px repeat(8, var(--cell-size));
  grid-template-rows: 22px repeat(8, var(--cell-size));
  width: max-content;
  padding: 4px 5px 5px 4px;
  background: #0f1318;
  border: 1px solid #343a40;
  border-radius: 8px;
  box-shadow: 0 16px 36px rgba(0,0,0,.28);
  user-select: none;
}

.coord-label { display: grid; place-items: center; color: #aeb6c1; font-size: 10px; font-weight: 700; }
.corner-label { grid-column: 1; grid-row: 1; }
.board-cell {
  display: grid;
  place-items: center;
  position: relative;
  width: var(--cell-size);
  height: var(--cell-size);
  padding: 0;
  border: 1px solid rgba(31,36,43,.7);
  border-radius: 0;
  background: var(--cell-light);
  overflow: hidden;
}
.board-cell.dark { background: var(--cell-dark); }
.board-cell.selected { outline: 3px solid var(--accent); outline-offset: -3px; z-index: 2; }
.board-cell.move-source { box-shadow: inset 0 0 0 4px #4f88c8; z-index: 2; }
.board-cell img { width: 92%; height: 92%; object-fit: contain; pointer-events: none; }
.weapon-stack { position: absolute; inset: auto 2px 2px 2px; display: flex; justify-content: flex-end; gap: 2px; pointer-events: none; }
.weapon-badge {
  display: grid;
  place-items: center;
  min-width: 17px;
  height: 16px;
  padding: 0 3px;
  border: 1px solid #fff0b8;
  border-radius: 6px;
  background: #20242b;
  color: #fff0b8;
  font-size: 8px;
  font-weight: 900;
  box-shadow: 0 1px 3px #0008;
}

.selection-status { min-height: 32px; padding: 8px 6px 5px; color: #cbd4dd; font-size: 12px; text-align: center; }

.panel {
  margin-top: 10px;
  padding: 14px;
  border: 1px solid rgba(255,255,255,.09);
  border-radius: 12px;
  background: linear-gradient(145deg, rgba(42,48,56,.98), rgba(34,40,47,.98));
  box-shadow: 0 8px 26px rgba(0,0,0,.14);
}
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.section-kicker { margin-bottom: 3px; }

.owner-switch, .turn-switch, .mode-tabs { display: flex; align-items: center; gap: 4px; }
.owner-button, .current-button, .mode-button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #343b44;
  color: #dce4eb;
  font-weight: 700;
}
.owner-button { min-width: 48px; }
.owner-button.active[data-owner="black"] { background: #15191e; color: #fffdf6; border-color: #69737e; }
.owner-button.active[data-owner="white"] { background: #efe9dc; color: #20242b; border-color: #fff; }
.mode-tabs { margin-bottom: 12px; }
.mode-button { flex: 1; }
.mode-button.active { background: #4e7c9a; border-color: #78a7c5; color: white; }
.mode-button.active[data-mode="erase"] { background: #814640; border-color: #c16c63; }

.field-label { display: block; margin: 0 0 6px; color: var(--muted); font-size: 12px; font-weight: 700; }
.piece-picker { display: grid; grid-template-columns: 54px 1fr; gap: 10px; align-items: center; }
.piece-picker img { width: 54px; height: 54px; border-radius: 10px; background: #171b21; }
select, input, textarea {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #171b21;
  color: var(--text);
}
select, input { min-height: 44px; padding: 8px 10px; }
select { width: 100%; }

.inspector { margin-top: 13px; padding: 11px; border: 1px solid #3c4651; border-radius: 9px; background: #20262d; }
.inspector.empty { opacity: .62; }
.weapon-fields { display: grid; gap: 8px; margin-top: 10px; }
.weapon-fields label { display: grid; grid-template-columns: 58px 1fr; gap: 8px; align-items: center; color: var(--muted); font-size: 12px; font-weight: 700; }
.text-button { display: block; margin: 13px 0 0 auto; padding: 8px; border: 0; background: transparent; }
.danger-text { color: var(--danger); }

.turn-switch { justify-content: flex-start; margin-bottom: 12px; }
.turn-switch span { margin-right: 5px; color: var(--muted); font-size: 12px; font-weight: 700; }
.current-button { min-width: 64px; }
.current-button.active { color: #171b21; background: var(--accent); border-color: #ffe783; }
.state-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.state-grid label { color: var(--muted); font-size: 12px; font-weight: 700; }
.state-grid input { width: 100%; margin-top: 5px; font-size: 16px; }
.saved-indicator { color: #8bd3ad; font-size: 11px; }
.saved-indicator.saving { color: var(--accent); }

textarea {
  width: 100%;
  padding: 10px;
  resize: vertical;
  font: 12px/1.55 ui-monospace, SFMono-Regular, Consolas, monospace;
  white-space: pre;
}
.action-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.primary, .secondary {
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid transparent;
  border-radius: 8px;
  color: white;
  font-weight: 800;
}
.primary { background: #3f7d5b; border-color: #66a57f; }
.secondary { background: #454e59; border-color: #5d6875; }
.compact { min-height: 38px; padding: 6px 10px; }
button:disabled { opacity: .4; }
.help { margin-top: 9px; color: var(--muted); font-size: 11px; line-height: 1.55; }
.warning-badge { color: #ffd47d; font-size: 11px; text-align: right; }

.install-note { display: grid; gap: 4px; margin: 14px 4px 0; color: var(--muted); font-size: 11px; line-height: 1.5; }
.install-note strong { color: #dbe3ea; }
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  z-index: 40;
  max-width: calc(100vw - 32px);
  transform: translate(-50%, 16px);
  padding: 10px 15px;
  border-radius: 999px;
  background: #0e1216;
  color: white;
  font-size: 12px;
  box-shadow: 0 8px 30px #0009;
  opacity: 0;
  pointer-events: none;
  transition: .18s ease;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 560px) {
  :root { --cell-size: 58px; }
  main { padding-top: 20px; }
}

@media (max-width: 350px) {
  :root { --cell-size: 9.9vw; }
  main { padding-inline: 7px; }
  .board { grid-template-columns: 18px repeat(8, var(--cell-size)); }
  .owner-button { min-width: 43px; }
  .mode-button { font-size: 12px; }
}

@media (display-mode: standalone) {
  .install-note { display: none; }
}
