:root {
  --light: #eeeed2;
  --dark: #769656;
  --accent: #f59e0b;
  --accent-soft: rgba(251, 191, 36, 0.14);
  --accent-text: #fcd34d;
  --accent-ink: #171717;
  --bg: #171717;
  --panel: #1d1d1d;
  --panel-border: rgba(64, 64, 64, 0.5);
  --text: #f5f5f5;
  --muted: #a3a3a3;
  --good: #4caf50;
  --warn: #e0a030;
  --bad: #e05050;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

#app {
  width: 100%;
  max-width: 520px;
}

/* ---- wide (sidebar) pages: home / play / profile ---- */

body.wide-page {
  display: block;
  align-items: unset;
  justify-content: unset;
  padding: 0;
}

.app-shell {
  display: flex;
  align-items: stretch;
  min-height: 100vh;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 22px 14px;
  border-right: 1px solid var(--panel-border);
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.sidebar-brand {
  font-weight: 700;
  padding: 0 10px;
  white-space: nowrap;
}

.sidebar-nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.sidebar-link {
  display: block;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
}
.sidebar-link:hover { background: rgba(255, 255, 255, 0.05); color: var(--text); }
.sidebar-link.active { background: var(--accent-soft); color: var(--accent-text); }

.sidebar-footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 10px;
}
.sidebar-tokens { font-weight: 600; color: var(--good); font-size: 0.9rem; }
.sidebar-tokens.low { color: var(--bad); }
.sidebar-footer .small-btn { width: 100%; }

.main-content {
  flex: 1;
  min-width: 0;
  padding: 28px;
}

.main-content #app {
  max-width: 1040px;
  margin: 0 auto;
}

@media (max-width: 860px) {
  .app-shell { flex-direction: column; }
  .sidebar {
    width: 100%;
    height: auto;
    flex-direction: row;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--panel-border);
    padding: 12px 16px;
    gap: 14px;
    overflow-x: auto;
  }
  .sidebar-nav { flex-direction: row; }
  .sidebar-link { padding: 8px 10px; }
  .sidebar-footer {
    margin-top: 0;
    margin-left: auto;
    flex-direction: row;
    align-items: center;
    padding: 0;
  }
  .sidebar-footer .small-btn { width: auto; }
  .main-content { padding: 18px; }
}

/* ---- dashboard (home page) ---- */

.hero-cta {
  background: linear-gradient(135deg, #234a2f, #1c3a26);
  border: 1px solid var(--good);
  border-radius: 16px;
  padding: 36px 28px;
  text-align: center;
  margin: 24px 0;
}
.hero-cta h2 { margin: 0 0 8px; font-size: 1.4rem; }
.hero-cta p { color: var(--muted); margin: 0 0 20px; }
.hero-cta .color-btn { display: inline-block; width: auto; padding: 14px 36px; }

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin: 20px 0;
}
.dashboard-card {
  background: var(--panel);
  border-radius: 10px;
  padding: 20px;
  border: 1px solid var(--panel-border);
}
.dashboard-card .card-label {
  color: var(--muted);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.dashboard-card .card-value {
  font-size: 2rem;
  font-weight: 700;
  margin-top: 6px;
}

.section-title {
  font-size: 1.05rem;
  font-weight: 600;
  margin: 28px 0 12px;
}

/* ---- play page two-column layout ---- */

.play-layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  align-items: start;
}
@media (min-width: 900px) {
  .play-layout {
    grid-template-columns: minmax(0, 1fr) 340px;
  }
}
.play-side {
  background: var(--panel);
  border-radius: 10px;
  padding: 18px;
  border: 1px solid var(--panel-border);
}

.idle-board {
  pointer-events: none;
  opacity: 0.55;
}

/* ---- profile page ---- */

.profile-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
}
@media (min-width: 760px) {
  .profile-grid { grid-template-columns: 1fr 1fr; }
}
.profile-card {
  background: var(--panel);
  border-radius: 10px;
  padding: 20px;
  border: 1px solid var(--panel-border);
}
.profile-card h3 { margin-top: 0; }
.history-list { list-style: none; padding: 0; margin: 0; max-height: 320px; overflow-y: auto; }
.history-list li {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--panel-border);
  font-size: 0.86rem;
}
.history-list .amt-pos { color: var(--good); font-weight: 600; }
.history-list .amt-neg { color: var(--bad); font-weight: 600; }

.screen.hidden { display: none; }

h1 {
  text-align: center;
  font-size: 1.6rem;
  margin-bottom: 4px;
}

.sub {
  text-align: center;
  color: var(--muted);
  margin-top: 0;
}

.pick-label {
  text-align: center;
  margin-top: 32px;
}

.color-pick {
  display: flex;
  gap: 16px;
  justify-content: center;
  margin-top: 16px;
}

.color-btn {
  font-size: 1.05rem;
  font-weight: 600;
  padding: 16px 28px;
  border-radius: 8px;
  border: 2px solid var(--accent);
  cursor: pointer;
  background: var(--accent);
  color: var(--accent-ink);
  text-decoration: none;
  display: inline-block;
  text-align: center;
  transition: transform 0.1s, filter 0.15s;
}
.color-btn:hover { filter: brightness(1.08); transform: translateY(-2px); }

.divider {
  display: flex;
  align-items: center;
  text-align: center;
  color: var(--muted);
  margin: 24px 0 12px;
  font-size: 0.8rem;
}
.divider::before, .divider::after {
  content: "";
  flex: 1;
  border-bottom: 1px solid var(--panel-border);
}
.divider span { padding: 0 12px; }

.upload-btn {
  display: block;
  width: fit-content;
  margin: 12px auto 0;
  padding: 12px 20px;
  border-radius: 10px;
  border: 2px dashed #525252;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  font-size: 1rem;
  text-align: center;
}
.upload-btn:hover { border-color: var(--accent); }

.upload-hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.78rem;
  margin-top: 8px;
}

.setup-title {
  text-align: center;
  font-size: 1.1rem;
  margin-bottom: 4px;
}

.setup-controls {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setup-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 0.9rem;
}

.mini-pick {
  display: flex;
  gap: 6px;
}

.mini-btn {
  background: var(--bg);
  color: var(--text);
  border: 1px solid #525252;
  border-radius: 8px;
  padding: 6px 14px;
  cursor: pointer;
  font-size: 0.85rem;
}
.mini-btn.active { border-color: var(--accent); background: var(--accent); color: var(--accent-ink); }

.setup-error {
  margin-top: 10px;
  padding: 10px;
  border-radius: 8px;
  background: var(--bad);
  text-align: center;
  font-size: 0.85rem;
}
.setup-error.hidden { display: none; }

.setup-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}
.setup-actions .color-btn { flex: 1; padding: 12px; font-size: 1rem; }
.setup-actions .small-btn { flex: 0 0 auto; }

.crop-container {
  position: relative;
  width: 100%;
  user-select: none;
  touch-action: none;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
}

.crop-container img {
  display: block;
  width: 100%;
  height: auto;
  pointer-events: none;
}

.crop-box {
  position: absolute;
  border: 3px solid var(--accent);
  box-shadow: 0 0 0 2000px rgba(0, 0, 0, 0.55);
  cursor: move;
  box-sizing: border-box;
}

.crop-handle {
  position: absolute;
  right: -8px;
  bottom: -8px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid #fff;
  cursor: nwse-resize;
}

.top-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.turn-indicator {
  flex: 1;
  font-weight: 600;
  font-size: 1.05rem;
}

.small-btn {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 0.85rem;
}
.small-btn:hover { border-color: var(--accent); }
.small-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.suggestion-panel {
  background: linear-gradient(135deg, #234a2f, #1c3a26);
  border: 1px solid var(--good);
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 10px;
  text-align: center;
}
.suggestion-panel.hidden { display: none; }

.suggestion-label {
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #9be29b;
}

.suggestion-move {
  font-size: 1.6rem;
  font-weight: 700;
  margin: 4px 0;
}

.suggestion-eval {
  font-size: 0.85rem;
  color: var(--muted);
}

.waiting-panel {
  background: var(--panel);
  border: 1px dashed #525252;
  border-radius: 12px;
  padding: 12px 16px;
  margin-bottom: 10px;
  text-align: center;
  color: var(--muted);
}
.waiting-panel.hidden { display: none; }

.status-banner {
  text-align: center;
  padding: 10px;
  border-radius: 10px;
  margin-bottom: 10px;
  font-weight: 700;
  background: var(--bad);
}
.status-banner.hidden { display: none; }
.status-banner.check { background: var(--warn); color: var(--bg); }
.status-banner.draw { background: #404040; }

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  overflow: hidden;
  border: 3px solid var(--panel);
}

.square {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.2rem;
  cursor: pointer;
  user-select: none;
}
.square.light { background: var(--light); }
.square.dark { background: var(--dark); }

.square.selected { outline: 3px solid var(--accent); outline-offset: -3px; }

.square .dot {
  position: absolute;
  width: 22%;
  height: 22%;
  border-radius: 50%;
  background: rgba(45, 108, 223, 0.75);
}
.square.has-piece .dot {
  width: 92%;
  height: 92%;
  border-radius: 50%;
  background: transparent;
  border: 4px solid rgba(224, 80, 80, 0.75);
}

.square.suggest-from { box-shadow: inset 0 0 0 4px var(--good); }
.square.suggest-to { box-shadow: inset 0 0 0 4px var(--good); }

.piece.white { color: #fff; text-shadow: 0 0 2px #000, 0 0 4px #000; }
.piece.black { color: #111; text-shadow: 0 0 2px #fff; }

.hint {
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  margin-top: 10px;
}

.promotion-dialog {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
}
.promotion-dialog.hidden { display: none; }

.promotion-box {
  background: var(--panel);
  border-radius: 12px;
  padding: 20px;
  text-align: center;
}

.promotion-options {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}
.promotion-options button {
  font-size: 2rem;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid #525252;
  background: var(--bg);
  color: var(--text);
  cursor: pointer;
}
.promotion-options button:hover { border-color: var(--accent); }

.tab-pick {
  display: flex;
  gap: 8px;
  margin: 24px 0 16px;
  background: var(--panel);
  border-radius: 10px;
  padding: 4px;
}
.tab-btn {
  flex: 1;
  background: transparent;
  color: var(--muted);
  border: none;
  border-radius: 8px;
  padding: 10px;
  cursor: pointer;
  font-size: 0.92rem;
}
.tab-btn.active { background: var(--accent); color: var(--accent-ink); }

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.auth-form.hidden { display: none; }
.auth-form input {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 0.95rem;
}
.auth-form input:focus { outline: none; border-color: var(--accent); }
.auth-form .color-btn { width: 100%; }

.link-line {
  text-align: center;
  margin: 0;
  font-size: 0.85rem;
}
.link-line a { color: var(--accent-text); text-decoration: none; }
.link-line a:hover { text-decoration: underline; }

#start-options.hidden { display: none; }

.admin-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 14px;
  font-size: 0.88rem;
}
.admin-table th, .admin-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--panel-border);
}
.admin-table th { color: var(--muted); font-weight: 600; }
.admin-table input {
  width: 70px;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  color: var(--text);
  padding: 6px 8px;
}
.admin-table .row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.admin-table .row-actions button {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 0.8rem;
}
.admin-table .row-actions button.reset-btn { background: #404040; }
