:root {
  --board-wood: #dcb35c;
  --board-wood-dark: #c69a4a;
  --board-line: #5a3d1e;
  --panel-bg: #2b2320;
  --panel-bg-2: #3a2f29;
  --text-light: #f0e6d6;
  --accent: #b3401f;
  --piece-bg: #f0d9a8;
  --piece-border: #3a2410;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: #1a1512;
  color: var(--text-light);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", serif;
  overflow-x: hidden;
}

h1, h2, h3 { margin: 0.2em 0; }

button {
  font-family: inherit;
  cursor: pointer;
}

/* ---------- Overlay / Start screen ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: radial-gradient(circle at 50% 30%, #2a231d, #120e0b);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.start-panel {
  background: var(--panel-bg-2);
  border: 1px solid #55453a;
  border-radius: 12px;
  padding: 2rem 2.5rem;
  text-align: center;
  max-width: 90vw;
  box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

.start-group { margin: 1.2rem 0; }

.option-row {
  display: flex;
  gap: 0.6rem;
  justify-content: center;
  margin-top: 0.5rem;
}

.option-btn {
  background: var(--panel-bg);
  border: 1px solid #665344;
  color: var(--text-light);
  padding: 0.6rem 1.2rem;
  border-radius: 8px;
  font-size: 1rem;
}

.option-btn.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.primary-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  padding: 0.7rem 1.6rem;
  border-radius: 8px;
  font-size: 1.05rem;
  margin-top: 1rem;
}

.secondary-btn {
  background: transparent;
  border: 1px solid #665344;
  color: var(--text-light);
  padding: 0.7rem 1.6rem;
  border-radius: 8px;
  font-size: 1.05rem;
}

.danger-btn {
  background: #6b1c1c;
  border: 1px solid #8a2828;
  color: var(--text-light);
  padding: 0.5rem 1rem;
  border-radius: 8px;
  width: 100%;
}

/* ---------- App layout ---------- */
.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.8rem;
}

.app-header {
  display: flex;
  align-items: baseline;
  gap: 1.2rem;
  margin-bottom: 0.6rem;
}

.turn-indicator {
  font-size: 1.05rem;
  color: #e0c898;
}

.main-layout {
  display: flex;
  gap: 1.2rem;
  width: 100%;
  max-width: 1100px;
  justify-content: center;
  flex-wrap: wrap;
}

.board-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

/* ---------- Hands ---------- */
.hand {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
  min-height: 3.2rem;
  padding: 0.2rem 0.4rem;
}

.hand-owner-label {
  font-size: 0.8rem;
  color: #cbb894;
  margin-right: 0.4rem;
  white-space: nowrap;
}

.hand-tile {
  position: relative;
  background: linear-gradient(160deg, var(--piece-bg), #d9bd85);
  border: 1px solid var(--piece-border);
  border-radius: 6px;
  padding: 0.3rem 0.6rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  color: #2a1a0a;
  font-weight: bold;
}

.hand-tile.selected {
  outline: 3px solid #4fc3f7;
}

.hand-kanji { font-size: 1.1rem; }
.hand-count { font-size: 0.85rem; opacity: 0.8; }

/* ---------- Board grid with labels ---------- */
.board-grid {
  display: grid;
  grid-template-columns: 1.4rem repeat(9, minmax(0, 1fr));
  grid-template-rows: 1.4rem repeat(9, minmax(0, 1fr));
  width: min(92vw, 62vh, 620px);
  aspect-ratio: 1 / 1;
  min-height: 0;
  overflow: hidden;
}

.corner { grid-column: 1; grid-row: 1; }

.file-label {
  grid-column: 2 / span 9;
  grid-row: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
}

.rank-label {
  grid-column: 1;
  grid-row: 2 / span 9;
  display: grid;
  grid-template-rows: repeat(9, 1fr);
}

.label-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.6rem, 1.6vw, 0.9rem);
  color: #c9b48a;
}

.board {
  grid-column: 2 / span 9;
  grid-row: 2 / span 9;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background:
    repeating-linear-gradient(90deg, rgba(0,0,0,0.02) 0 2px, transparent 2px 100%),
    linear-gradient(160deg, var(--board-wood), var(--board-wood-dark) 60%, var(--board-wood));
  border: 3px solid var(--board-line);
  box-shadow: 0 6px 24px rgba(0,0,0,0.5), inset 0 0 30px rgba(80,50,10,0.3);
}

.cell {
  position: relative;
  border: 1px solid var(--board-line);
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.cell.selected::before {
  content: '';
  position: absolute;
  inset: 2px;
  border: 2px solid #4fc3f7;
  border-radius: 4px;
}

.cell.highlight::after {
  content: '';
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: rgba(30, 144, 60, 0.55);
}

.cell.highlight-capture::after {
  content: '';
  position: absolute;
  inset: 8%;
  border: 3px solid rgba(180, 30, 30, 0.75);
  border-radius: 6px;
}

.cell.in-check {
  background: rgba(200, 30, 30, 0.35);
}

.cell.last-move {
  box-shadow: inset 0 0 0 3px rgba(220, 40, 40, 0.9);
}

/* ---------- Pieces ---------- */
.piece {
  width: 82%;
  height: 88%;
  background: linear-gradient(160deg, var(--piece-bg), #d9bd85);
  border: 1px solid var(--piece-border);
  color: #2a1a0a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: clamp(0.85rem, 2.6vw, 1.5rem);
  clip-path: polygon(50% 0%, 92% 22%, 100% 100%, 0% 100%, 8% 22%);
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
  user-select: none;
  writing-mode: horizontal-tb;
  line-height: 1;
}

.piece.two-char {
  font-size: clamp(0.6rem, 1.9vw, 1.05rem);
  writing-mode: vertical-rl;
}

.piece.opponent {
  transform: rotate(180deg);
}

/* ---------- Side panel ---------- */
.side-panel {
  width: 260px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  background: var(--panel-bg-2);
  border: 1px solid #55453a;
  border-radius: 10px;
  padding: 0.8rem;
}

.status-box {
  min-height: 1.6rem;
}

.check-indicator {
  color: #ff5252;
  font-weight: bold;
  font-size: 1.1rem;
  text-align: center;
  animation: pulse 1s infinite alternate;
}

#eval-indicator {
  color: #cbb894;
  font-size: 0.85rem;
  text-align: center;
  margin-top: 0.3rem;
}

@keyframes pulse {
  from { opacity: 0.6; }
  to { opacity: 1; }
}

.kifu-box {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.kifu-list {
  flex: 1;
  overflow-y: auto;
  max-height: 40vh;
  margin: 0;
  padding-left: 1.4rem;
  font-size: 0.9rem;
  background: rgba(0,0,0,0.2);
  border-radius: 6px;
  padding: 0.5rem 0.5rem 0.5rem 1.6rem;
}

.kifu-list li { margin-bottom: 0.15rem; }

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal-box {
  background: var(--panel-bg-2);
  border: 1px solid #665344;
  border-radius: 10px;
  padding: 1.5rem 2rem;
  text-align: center;
}

.modal-actions {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-top: 1rem;
}

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .main-layout {
    flex-direction: column;
    align-items: center;
  }
  .side-panel {
    width: 100%;
    max-width: 620px;
  }
  .kifu-list { max-height: 22vh; }
}
