:root {
  --board-max: 432px; /* 8 × 54px — reference max on desktop */

  /* App theme — always dark, independent of the user's Telegram theme (see main.js) */
  --bg:       #1a1a1a;
  --bg-panel: #2a2a2a;
  --bg-timer: #333333;
  --text:     #e0e0e0;
  --text-hi:  #ffffff;
  --text-lo:  #999999;
  --border:   #555555;
}

/* Ensure hidden attribute always wins regardless of other display rules */
[hidden] { display: none !important; }

*, *::before, *::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 12px 12px 32px;
  background: var(--bg);
  color: var(--text);
  font-family: monospace, monospace;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  overflow-x: hidden;
}

h1 {
  margin: 0 0 16px;
  font-size: 1.4rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--text-hi);
}

/* ── Resume dialog ──────────────────────────────────────────────────────────── */

#resume-dialog {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.resume-dialog-content {
  width: min(calc(100vw - 24px), 320px);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.resume-dialog-content h2 {
  margin: 0;
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: var(--text-hi);
}

.resume-dialog-content > p {
  margin: 0;
  font-size: 0.85rem;
  line-height: 1.5;
  color: var(--text);
}

.resume-info {
  font-size: 0.78rem !important;
  color: var(--text-lo) !important;
  font-family: monospace, monospace;
}

.resume-buttons {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}

.resume-btn-primary,
.resume-btn-secondary {
  flex: 1;
  padding: 12px 8px;
  border-radius: 6px;
  font-family: monospace, monospace;
  font-size: 0.85rem;
  font-weight: bold;
  letter-spacing: 0.06em;
  cursor: pointer;
  transition: filter 0.12s;
}

.resume-btn-primary {
  background: var(--text-hi);
  border: 2px solid var(--text-hi);
  color: var(--bg);
}

.resume-btn-primary:hover {
  filter: brightness(0.88);
}

.resume-btn-secondary {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-lo);
}

.resume-btn-secondary:hover {
  border-color: var(--text-hi);
  color: var(--text-hi);
}

/* ── Mode selection ────────────────────────────────────────────────────────── */

#phase-mode {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 48px;
  width: 100%;
}

#phase-mode h1 {
  margin: 0;
}

#mode-buttons {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: min(calc(100vw - 24px), 320px);
}

.mode-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 20px 24px;
  background: var(--bg-panel);
  border: 2px solid var(--border);
  border-radius: 8px;
  color: var(--text-hi);
  font-family: monospace, monospace;
  font-size: 1.1rem;
  font-weight: bold;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

.mode-btn:hover {
  border-color: var(--text-hi);
  background: var(--bg-timer);
}

.mode-btn:active {
  filter: brightness(0.85);
}

.mode-desc {
  font-size: 0.72rem;
  font-weight: normal;
  letter-spacing: 0.04em;
  text-transform: none;
  color: var(--text-lo);
}

/* ── Locked AI difficulty button ───────────────────────────────────────────── */

.mode-btn.locked {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

/* ── Coming-soon mode buttons ──────────────────────────────────────────────── */

.mode-btn.coming-soon {
  opacity: 0.55;
  cursor: pointer;
}

.badge-soon {
  font-size: 0.62rem;
  background: var(--bg-timer);
  color: var(--text-lo);
  padding: 2px 7px;
  border-radius: 4px;
  letter-spacing: 0.12em;
}

/* ── Toasts ────────────────────────────────────────────────────────────────── */

#unlock-toast,
#tinta-toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-hi);
  padding: 10px 18px;
  font-family: monospace, monospace;
  font-size: 0.9rem;
  letter-spacing: 0.04em;
  z-index: 200;
  cursor: pointer;
  white-space: nowrap;
}

/* ── Phase containers ──────────────────────────────────────────────────────── */

#phase-setup,
#phase-game {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  padding-top: 4vh;
}

.btn-audio {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 32px;
  padding: 0;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-lo);
  font-size: 1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.btn-audio:hover {
  background: var(--bg-timer);
}

.btn-resign {
  display: block;
  margin: 16px auto 0;
  padding: 8px 24px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-lo);
  font-family: monospace, monospace;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  cursor: pointer;
}
.btn-resign:hover {
  background: var(--bg-timer);
}

/* ── Board (shared by setup and game) ─────────────────────────────────────── */

#board,
#setup-board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  width: min(calc(100vw - 24px), var(--board-max));
  border: 2px solid var(--border);
  flex-shrink: 0;
}

#board.finished .cell {
  cursor: default;
}

#board.finished .cell:hover {
  filter: none;
}

.cell {
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  position: relative;
}

.cell:hover {
  filter: brightness(1.15);
}

/* Setup board cells: display only, no hover, no pointer */
.setup-cell {
  cursor: default;
  pointer-events: none;
}

/* Override hover for setup cells */
#setup-board .cell:hover {
  filter: none;
}

/* Small circle on the 8 tiles of the first chosen color */
.setup-chosen::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width:  42%;
  height: 42%;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.85);
  background: rgba(0, 0, 0, 0.25);
  pointer-events: none;
}

.cell.selected {
  outline: 4px solid #ffd700;
  outline-offset: -4px;
  z-index: 2;
  position: relative;
}

/* AI move highlight: where the piece came from (dashed) and where it went (solid) */
.cell.last-move-from {
  outline: 3px dashed #ffd700;
  outline-offset: -3px;
  z-index: 1;
  position: relative;
}

.cell.last-move-to {
  outline: 4px solid #ffd700;
  outline-offset: -4px;
  z-index: 2;
  position: relative;
}

.cell.valid-dest {
  outline: 3px dashed rgba(255, 255, 255, 0.85);
  outline-offset: -3px;
  z-index: 1;
  position: relative;
}

.cell.valid-dest::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.10);
  pointer-events: none;
}

.tile-just-tinted {
  animation: tile-appear 250ms ease-out both;
}

@keyframes tile-appear {
  from {
    opacity: 0.3;
    transform: scale(1.2);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* ── Crystals ──────────────────────────────────────────────────────────────── */
/*
 * Background color and border color are set inline by JS.
 * Size is percentage of the cell so it scales with the grid.
 */

.crystal {
  width:  60%;
  height: 60%;
  border-radius: 50%;
  pointer-events: none;
  position: relative;
  z-index: 3;
  flex-shrink: 0;
}

/* ── Player panels (desktop: left and right of board) ─────────────────────── */

#app {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  overflow-x: visible;
  max-width: 100%;
}

.panel {
  width: 110px;
  padding: 14px 10px;
  background: var(--bg-panel);
  border: 2px solid transparent;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.panel.active {
  border-color: #ffd700;
}

/* Background and border set inline by JS */
.panel-crystal {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  flex-shrink: 0;
}

.panel-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-lo);
}

.panel-score {
  font-size: 2.4rem;
  font-weight: bold;
  color: var(--text-hi);
  line-height: 1;
}

.panel-pts {
  font-size: 0.65rem;
  letter-spacing: 0.12em;
  color: var(--text-lo);
}

/* ── Timer ─────────────────────────────────────────────────────────────────── */

.timer-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  width: min(calc(100vw - 24px), var(--board-max));
}

#phase-setup .timer-wrap {
  min-height: 32px;
}

.timer-bar {
  flex: 1;
  height: 8px;
  background: var(--bg-timer);
  border-radius: 4px;
  overflow: hidden;
}

.timer-fill {
  height: 100%;
  width: 100%;
  background: #4caf50;
  border-radius: 4px;
  transition: width 0.1s linear;
}

.timer-fill.urgent {
  background: #f44336;
}

.timer-num {
  font-size: 1.1rem;
  font-weight: bold;
  min-width: 1.4em;
  text-align: right;
  color: var(--text);
}

.timer-num.urgent {
  color: #f44336;
}

/* ── Setup prompt ──────────────────────────────────────────────────────────── */

#setup-prompt {
  margin: 0 0 12px;
  font-size: 1.1rem;
  font-weight: bold;
  color: var(--text-hi);
  text-align: center;
  letter-spacing: 0.04em;
}

/* ── Color swatches ────────────────────────────────────────────────────────── */

#swatches {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
  padding: 10px;
  border: 2px solid var(--text-lo);
  border-radius: 8px;
  width: min(calc(100vw - 24px), var(--board-max));
}

.swatch {
  aspect-ratio: 1 / 1;
  border-radius: 4px;
  cursor: pointer;
  border: 3px solid var(--border);
  transition: filter 0.12s, border-color 0.12s, transform 0.12s;
  position: relative;
}

.swatch:hover:not(.swatch-taken) {
  filter: brightness(1.25);
  border-color: var(--text-hi);
  transform: scale(1.10);
}

.swatch-taken {
  opacity: 0.35;
  cursor: default;
  filter: grayscale(0.7);
}

.swatch-taken::after {
  content: '✕';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  font-size: 1.4rem;
  font-weight: bold;
  line-height: 1;
  text-shadow: 0 0 3px #000, 0 0 6px #000;
  pointer-events: none;
}

/* ── Message area ──────────────────────────────────────────────────────────── */

#message {
  margin-top: 18px;
  min-height: 1.8em;
  font-size: 0.9rem;
  color: var(--text);
  text-align: center;
  max-width: min(calc(100vw - 24px), var(--board-max));
}

#message.game-over {
  color: #ffd700;
  font-size: 1.1rem;
  font-weight: bold;
}

#stall {
  margin-top: 6px;
  min-height: 1.2em;
  font-size: 0.8rem;
  color: var(--text-lo);
  text-align: center;
  letter-spacing: 0.05em;
}

/* ── End-game buttons ──────────────────────────────────────────────────────── */

#end-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 18px;
  width: min(calc(100vw - 24px), var(--board-max));
}


#end-buttons button {
  flex: 1;
  padding: 14px 8px;
  background: var(--bg-panel);
  border: 2px solid var(--border);
  border-radius: 8px;
  color: var(--text-hi);
  font-family: monospace, monospace;
  font-size: 0.9rem;
  font-weight: bold;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  cursor: pointer;
  transition: border-color 0.12s, background 0.12s;
}

#end-buttons button:hover {
  border-color: var(--text-hi);
  background: var(--bg-timer);
}

#end-buttons button:active {
  filter: brightness(0.85);
}

/* ── Setup hint ────────────────────────────────────────────────────────────── */

#setup-hint {
  margin: 8px 0 0;
  font-size: 0.75rem;
  color: var(--text-lo);
  text-align: center;
  letter-spacing: 0.03em;
}

/* ── Game hint ─────────────────────────────────────────────────────────────── */

#game-hint {
  margin: 8px 0 0;
  font-size: 0.75rem;
  color: var(--text-lo);
  text-align: center;
  max-width: min(calc(100vw - 24px), var(--board-max));
  letter-spacing: 0.03em;
}

/* ── Mode footer (tagline + rules button) ──────────────────────────────────── */

.mode-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#mode-tagline {
  margin: 0;
  font-size: 0.75rem;
  color: var(--text-lo);
  text-align: center;
  letter-spacing: 0.04em;
}

.mode-footer-links {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(calc(100vw - 24px), 320px);
}

#btn-rules,
#btn-stats,
#btn-achievements {
  padding: 7px 20px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-lo);
  font-family: monospace, monospace;
  font-size: 0.75rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
}

#btn-rules:hover,
#btn-stats:hover,
#btn-achievements:hover {
  border-color: var(--text-hi);
  color: var(--text-hi);
}

/* ── Stats screen ──────────────────────────────────────────────────────────── */

#phase-stats {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  padding: 16px 0 32px;
  gap: 12px;
}

.stats-back {
  align-self: flex-start;
  padding: 6px 14px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-lo);
  font-family: monospace, monospace;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
  cursor: pointer;
}

.stats-back:hover {
  border-color: var(--text-hi);
  color: var(--text-hi);
}

.stats-title {
  margin: 16px 0 0 0;
  font-size: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.2em;
  color: var(--text-hi);
}

.stats-block {
  width: min(calc(100vw - 24px), 320px);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
}

.stats-block-title {
  margin: 0 0 10px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-lo);
  font-family: monospace, monospace;
  font-weight: normal;
}

.stats-row {
  display: flex;
  justify-content: space-between;
  padding: 5px 0;
  font-size: 0.85rem;
  border-bottom: 1px solid var(--bg-timer);
}

.stats-row:last-child {
  border-bottom: none;
}

.stats-label {
  color: var(--text-lo);
}

.stats-val {
  color: var(--text-hi);
  font-weight: bold;
}

.stats-advanced {
  width: min(calc(100vw - 24px), 320px);
  padding: 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-lo);
  font-family: monospace, monospace;
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  cursor: not-allowed;
  opacity: 0.45;
  margin-top: 4px;
}

/* ── Achievements screen ───────────────────────────────────────────────────── */

#phase-achievements {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  padding: 16px 0 32px;
  gap: 8px;
}

.achievement-item {
  width: min(calc(100vw - 24px), 320px);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  opacity: 0.4;
}

.achievement-item.unlocked {
  opacity: 1;
}

.achievement-title {
  font-size: 0.88rem;
  font-weight: bold;
  color: var(--text-hi);
  letter-spacing: 0.04em;
}

.achievement-desc {
  font-size: 0.76rem;
  color: var(--text-lo);
  letter-spacing: 0.02em;
}

.achievement-date {
  font-size: 0.7rem;
  color: var(--text-lo);
  margin-top: 3px;
}

.achievement-progress {
  font-size: 0.72rem;
  color: var(--text-lo);
  margin-top: 3px;
}

.achievements-divider {
  width: min(calc(100vw - 24px), 320px);
  border: none;
  border-top: 1px solid var(--border);
  margin: 6px 0 2px;
}

.achievements-coming-title {
  margin: 0;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-lo);
  font-family: monospace, monospace;
  font-weight: normal;
}

.achievements-coming {
  width: min(calc(100vw - 24px), 320px);
  font-size: 1.4rem;
  letter-spacing: 0.15em;
  color: var(--text-lo);
  text-align: center;
  line-height: 2;
}

/* ── Rules screen ───────────────────────────────────────────────────────────── */

#rules-content h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--text-lo);
  margin: 18px 0 6px;
  font-family: monospace, monospace;
}

#rules-content h2:first-child {
  margin-top: 0;
}

#rules-content p {
  font-size: 0.85rem;
  line-height: 1.55;
  color: var(--text);
  margin: 0 0 8px;
}

#rules-content ul {
  margin: 0 0 8px;
  padding-left: 18px;
}

#rules-content li {
  font-size: 0.85rem;
  line-height: 1.55;
  color: var(--text);
  margin: 0 0 4px;
}

/* ── Rotate overlay ────────────────────────────────────────────────────────── */

#rotate-message {
  display: none;
}

@media (orientation: landscape) and (max-width: 1024px) {
  #rotate-message {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: fixed;
    inset: 0;
    background: var(--bg);
    color: var(--text);
    font-size: 1.2rem;
    font-family: monospace, monospace;
    text-align: center;
    padding: 20px;
    z-index: 100;
  }

  #rotate-message p {
    margin: 12px 0 0;
    letter-spacing: 0.04em;
  }

  h1,
  #phase-setup,
  #phase-game {
    display: none;
  }
}

/* ── Mobile: panels stacked above and below the board ─────────────────────── */

@media (max-width: 767px) {
  body {
    padding: 10px 10px 20px;
  }

  #phase-setup,
  #phase-game {
    padding-top: 3vh;
  }

  .btn-audio {
    top: 4px;
    right: 4px;
    bottom: auto;
    left: auto;
    transform: none;
  }

  h1 {
    font-size: 1.2rem;
    margin-bottom: 10px;
  }

  /* Stack: panel-0 → board → panel-1 (HTML order matches column order) */
  #app {
    flex-direction: column;
    align-items: center;
    overflow-x: visible;
    gap: 8px;
    width: 100%;
  }

  /* Panels become full-width horizontal bars */
  .panel {
    width: min(calc(100vw - 85px), var(--board-max));
    flex-direction: row;
    padding: 8px 16px;
    gap: 14px;
    justify-content: center;
    align-items: center;
  }

  .panel-score {
    font-size: 1.8rem;
  }

  .panel-crystal {
    width: 28px;
    height: 28px;
  }
}
