:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #243447;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #f59e0b;
  --danger: #f87171;
  --ok: #4ade80;
  --board: #c4a574;
  --board-line: #5b3a1a;
  --font: "IBM Plex Sans JP", "Segoe UI", sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1000px 500px at 10% -10%, rgba(56, 189, 248, 0.12), transparent 60%),
    radial-gradient(800px 400px at 100% 0%, rgba(245, 158, 11, 0.08), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
}

body {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(180px, 260px) 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(15, 23, 42, 0.92);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  gap: 10px;
  align-items: center;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: 1px solid var(--accent);
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.brand-title {
  font-weight: 700;
  font-size: 15px;
}

.brand-sub {
  color: var(--muted);
  font-size: 12px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.tool-btn,
.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
  cursor: pointer;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
}

.tool-btn:hover,
.btn:hover {
  border-color: var(--accent);
}

.tool-btn.accent {
  background: #0ea5e9;
  border-color: #0284c7;
  color: #082f49;
}

.tool-btn:disabled,
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.tool-link,
.inline-link {
  color: var(--accent);
  text-decoration: none;
  font-size: 13px;
  font-weight: 600;
  padding: 8px 10px;
}

.topbar-status {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.pill {
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
}

.pill.active {
  color: #052e1a;
  background: rgba(74, 222, 128, 0.85);
  border-color: transparent;
}

.pill.warn {
  color: #451a03;
  background: rgba(245, 158, 11, 0.9);
  border-color: transparent;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: 280px minmax(420px, 1fr) 320px;
  gap: 14px;
  padding: 14px;
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
  align-items: start;
  min-height: 0;
}

.col {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
}

.card.compact { padding: 12px; }
.card.grow { flex: 1; min-height: 220px; display: flex; flex-direction: column; }

.card h2 {
  margin: 0 0 12px;
  font-size: 14px;
  color: var(--accent);
  font-weight: 700;
}

.field {
  display: grid;
  gap: 4px;
  margin-bottom: 10px;
  font-size: 12px;
  color: var(--muted);
}

.field input,
.field select {
  width: 100%;
  border: 1px solid var(--line);
  background: #0b1220;
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font: inherit;
}

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.btn.primary {
  background: #0369a1;
  border-color: #0284c7;
}

.board-shell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.board-meta {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  font-weight: 700;
}

.check-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  border-radius: 999px;
  background: rgba(185, 28, 28, 0.15);
  color: #b91c1c;
  font-size: 12px;
  letter-spacing: 0.04em;
}

.check-badge[hidden] {
  display: none !important;
}

.ai-clock {
  font-family: "JetBrains Mono", ui-monospace, monospace;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
}

.ai-clock.active {
  color: #b45309;
}

.shogi-board {
  display: grid;
  grid-template-columns: 24px 432px;
  grid-template-rows: 432px 24px;
  gap: 4px;
}

.rank-labels,
.file-labels {
  display: grid;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.rank-labels {
  grid-template-rows: repeat(9, 1fr);
  align-items: center;
  justify-items: center;
}

.file-labels {
  grid-column: 2;
  grid-template-columns: repeat(9, 1fr);
  justify-items: center;
}

.board-grid {
  grid-column: 2;
  grid-row: 1;
  width: 432px;
  height: 432px;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--board);
  border: 3px solid var(--board-line);
}

.board-cell {
  border: 1px solid rgba(91, 58, 26, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  overflow: hidden;
  padding: 2px;
  box-sizing: border-box;
}

.board-cell.selected {
  background: rgba(56, 189, 248, 0.35);
}

.board-cell.possible-move {
  background: rgba(74, 222, 128, 0.28);
}

.board-overlay {
  grid-column: 2;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 16px;
  text-align: center;
  background: rgba(8, 12, 20, 0.78);
  backdrop-filter: blur(2px);
  border: 3px solid var(--board-line);
  opacity: 0;
  pointer-events: none;
  transform: scale(0.96);
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 5;
}

.board-overlay.board-overlay-visible {
  opacity: 1;
  transform: scale(1);
}

.board-overlay.board-overlay-dismissible.board-overlay-visible {
  pointer-events: auto;
  cursor: pointer;
}

.board-overlay-title {
  font-size: clamp(1.8rem, 5vw, 2.6rem);
  font-weight: 800;
  color: var(--text);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6);
}

.board-overlay-win .board-overlay-title { color: var(--ok); }
.board-overlay-lose .board-overlay-title { color: var(--danger); }
.board-overlay-draw .board-overlay-title { color: var(--accent); }

.board-overlay-sub {
  font-size: 1.05em;
  color: var(--text);
  opacity: 0.9;
}

.board-overlay-close {
  margin-top: 4px;
  padding: 6px 22px;
  border: none;
  border-radius: 999px;
  background: var(--accent-2);
  color: #201400;
  font-weight: 700;
  font-size: 0.95em;
  cursor: pointer;
}

.board-overlay-close:hover {
  filter: brightness(1.08);
}

.board-overlay-start {
  font-size: clamp(1.5rem, 4.5vw, 2.2rem);
  font-weight: 800;
  color: var(--text);
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.6);
}

.piece {
  width: 86%;
  height: 90%;
  max-width: 40px;
  max-height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(165deg, #f3e2c4 0%, #e4c896 42%, #c9a56a 100%);
  border: 1px solid rgba(90, 55, 20, 0.55);
  clip-path: polygon(50% 1%, 90% 16%, 96% 94%, 4% 94%, 10% 16%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.45);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "MS Mincho", serif;
  font-size: clamp(14px, 2.2vw, 22px);
  font-weight: 700;
  line-height: 1;
  color: #1a1208;
  pointer-events: none;
}

.piece.gote {
  transform: rotate(180deg);
  color: #1a1208;
}

.piece.promoted {
  color: #c41e3a;
}

.captured-pieces {
  width: 100%;
  min-height: 40px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 13px;
  display: grid;
  gap: 8px;
}

.hand-hint {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.hand-side {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.hand-label {
  min-width: 7.5em;
  color: var(--text);
  font-size: 12px;
}

.hand-empty {
  color: var(--muted);
  font-size: 12px;
}

.hand-pieces {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.hand-piece {
  appearance: none;
  border: 1px solid var(--line);
  background: linear-gradient(165deg, #f3e2c4 0%, #e4c896 55%, #c9a56a 100%);
  color: #1a1208;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "MS Mincho", serif;
  font-size: 16px;
  font-weight: 700;
  width: 34px;
  height: 38px;
  border-radius: 4px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  position: relative;
  line-height: 1;
  padding: 0;
}

.hand-piece:hover {
  border-color: var(--accent);
  filter: brightness(1.05);
}

.hand-piece.selected {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.25);
}

.hand-piece-readonly {
  cursor: default;
  opacity: 0.85;
}

.hand-piece-readonly:hover {
  border-color: var(--line);
  filter: none;
}

.hand-piece sup {
  position: absolute;
  top: 1px;
  right: 3px;
  font-size: 10px;
  font-family: var(--mono);
  color: #7c2d12;
}

.promote-dialog {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(2, 6, 23, 0.62);
  padding: 16px;
}

.promote-dialog[hidden] {
  display: none;
}

.promote-dialog-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 18px 20px;
  min-width: min(280px, 100%);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
}

.promote-dialog-card p {
  margin: 0 0 14px;
  font-size: 16px;
  text-align: center;
}

.promote-dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}

.metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.metric {
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  display: grid;
  gap: 4px;
}

.metric span {
  color: var(--muted);
  font-size: 11px;
}

.metric strong {
  font-size: 14px;
  font-family: var(--mono);
}

.log {
  margin-top: 10px;
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  min-height: 88px;
  max-height: 140px;
  overflow: auto;
  font-size: 12px;
  color: var(--muted);
  white-space: pre-wrap;
}

.kifu-display {
  flex: 1;
  overflow: auto;
  background: #0b1220;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  font-family: var(--mono);
  font-size: 12px;
  min-height: 180px;
  max-height: calc(100vh - 420px);
}

.kifu-move { padding: 4px 0; border-bottom: 1px solid rgba(51, 65, 85, 0.6); }
.kifu-move.gote { color: #fca5a5; }

.emotion-row {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 10px;
}

.emotion-face {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  border: 1px solid var(--line);
  display: grid;
  place-items: center;
  background: #0b1220;
  font-weight: 700;
}

.mono { font-family: var(--mono); }
.muted { color: var(--muted); font-size: 12px; }

.toast-root {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: grid;
  gap: 8px;
  z-index: 50;
}

.toast {
  min-width: 220px;
  max-width: 360px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  font-size: 13px;
}

.toast.success { border-color: var(--ok); }
.toast.error { border-color: var(--danger); }
.toast.info { border-color: var(--accent); }

@media (max-width: 1200px) {
  .workspace {
    grid-template-columns: 240px 1fr 280px;
  }
  .board-grid,
  .shogi-board {
    width: auto;
  }
  .shogi-board {
    grid-template-columns: 20px 360px;
    grid-template-rows: 360px 20px;
  }
  .board-grid {
    width: 360px;
    height: 360px;
  }
}

@media (max-width: 980px) {
  .topbar {
    grid-template-columns: 1fr;
  }
  .workspace {
    grid-template-columns: 1fr;
  }
  .kifu-display {
    max-height: 240px;
  }
}
