:root {
  color-scheme: dark;
  --bg: #101827;
  --panel: #172033;
  --panel-2: #202c44;
  --text: #f8fafc;
  --muted: #a8b3c7;
  --accent: #f7c948;
  --accent-2: #7dd3fc;
  --danger: #fb7185;
  --success: #86efac;
  --cell-gap: 4px;
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  min-height: 100%;
  background: radial-gradient(circle at top, #1e2a44 0%, var(--bg) 60%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, select {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  min-height: 100dvh;
  padding: calc(18px + var(--safe-top)) 18px calc(18px + var(--safe-bottom));
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 16px;
  max-width: 980px;
  margin: 0 auto;
}

.topbar, .footer, .card, .game-panel {
  background: rgba(23, 32, 51, 0.88);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 24px;
  box-shadow: 0 18px 50px rgba(0,0,0,0.22);
}

.topbar {
  padding: 18px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
}

h1, h2, h3, p {
  margin: 0;
}

h1 {
  font-size: clamp(28px, 4vw, 44px);
  line-height: 1;
}

.view {
  display: grid;
  align-content: start;
  gap: 16px;
}

.footer {
  padding: 12px 16px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
}

.status-pill {
  display: inline-flex;
  margin-top: 8px;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 14px;
}

.grid {
  display: grid;
  gap: 16px;
}

.home-grid {
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}

.card {
  padding: 18px;
}

.card h2 {
  margin-bottom: 10px;
}

.card p {
  color: var(--muted);
  line-height: 1.45;
}

.control-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 14px;
  align-items: center;
}

.primary-button, .secondary-button, .ghost-button, .danger-button {
  min-height: 44px;
  padding: 10px 16px;
  border-radius: 14px;
  color: var(--text);
}

.primary-button {
  background: var(--accent);
  color: #111827;
  font-weight: 800;
}

.secondary-button {
  background: var(--panel-2);
}

.ghost-button {
  background: rgba(255,255,255,0.08);
}

.danger-button {
  background: rgba(251,113,133,0.2);
  color: #fecdd3;
}

select {
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,0.12);
  background: var(--panel-2);
  color: var(--text);
}

.game-panel {
  padding: 16px;
  display: grid;
  gap: 14px;
}

.game-header {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
  flex-wrap: wrap;
}

.meta {
  color: var(--muted);
  margin-top: 4px;
}

.board-wrap {
  width: min(92vw, 720px);
  margin: 0 auto;
}

.queens-board {
  display: grid;
  gap: var(--cell-gap);
  width: 100%;
  aspect-ratio: 1 / 1;
  background: rgba(255,255,255,0.08);
  padding: var(--cell-gap);
  border-radius: 18px;
  touch-action: manipulation;
}

.cell {
  display: grid;
  place-items: center;
  border-radius: 10px;
  position: relative;
  font-size: clamp(22px, 6vw, 50px);
  font-weight: 900;
  color: #101827;
  user-select: none;
}

.cell.region-0 { background: #fca5a5; }
.cell.region-1 { background: #fdba74; }
.cell.region-2 { background: #fde68a; }
.cell.region-3 { background: #86efac; }
.cell.region-4 { background: #67e8f9; }
.cell.region-5 { background: #93c5fd; }
.cell.region-6 { background: #c4b5fd; }
.cell.region-7 { background: #f0abfc; }
.cell.region-8 { background: #f9a8d4; }

.cell.marked::after {
  content: "×";
  color: rgba(16,24,39,0.58);
}

.cell.queen::after {
  content: "♛";
  color: #101827;
  text-shadow: 0 1px 0 rgba(255,255,255,0.25);
}

.cell.error {
  outline: 4px solid var(--danger);
  outline-offset: -4px;
}

.message {
  min-height: 28px;
  color: var(--muted);
}

.message.success {
  color: var(--success);
  font-weight: 800;
}

.message.danger {
  color: #fecdd3;
  font-weight: 700;
}

@media (orientation: landscape) and (min-width: 900px) {
  .game-layout {
    display: grid;
    grid-template-columns: 1fr 260px;
    gap: 16px;
    align-items: start;
  }

  .board-wrap {
    width: min(62vw, 690px);
  }
}

/* Responsive improvements: laptop, iPad and iPhone */

:root {
  --app-max-width: 1180px;
  --board-max: 720px;
}

.app-shell {
  width: 100%;
  max-width: var(--app-max-width);
}

.home-grid {
  align-items: stretch;
}

.card {
  min-width: 0;
}

.game-layout {
  display: grid;
  gap: 16px;
}

.board-wrap {
  width: min(100%, var(--board-max));
}

.queens-board {
  max-height: min(74dvh, var(--board-max));
}

.cell {
  min-width: 0;
  min-height: 0;
}

@media (min-width: 1024px) {
  .app-shell {
    padding-left: 28px;
    padding-right: 28px;
  }

  .game-layout {
    grid-template-columns: minmax(520px, 1fr) 320px;
    align-items: start;
  }

  .board-wrap {
    width: min(68dvh, 760px);
    max-width: 100%;
  }

  .queens-board {
    max-height: min(76dvh, 760px);
  }

  .game-panel {
    padding: 22px;
  }
}

@media (min-width: 744px) and (max-width: 1023px) {
  :root {
    --board-max: 720px;
  }

  .app-shell {
    padding-left: 22px;
    padding-right: 22px;
  }

  .topbar {
    align-items: center;
  }

  .game-layout {
    grid-template-columns: 1fr;
  }

  .board-wrap {
    width: min(92vw, 700px);
  }

  .queens-board {
    max-height: 68dvh;
  }

  .control-row {
    justify-content: flex-start;
  }
}

@media (max-width: 743px) {
  :root {
    --cell-gap: 3px;
  }

  html, body {
    overflow-x: hidden;
  }

  .app-shell {
    min-height: 100svh;
    padding: calc(10px + var(--safe-top)) 10px calc(10px + var(--safe-bottom));
    gap: 10px;
  }

  .topbar {
    padding: 14px;
    border-radius: 18px;
    flex-direction: column;
    align-items: stretch;
  }

  .topbar h1 {
    font-size: 28px;
  }

  .status-pill {
    font-size: 13px;
    padding: 5px 9px;
  }

  .view {
    gap: 10px;
  }

  .home-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .card,
  .game-panel {
    border-radius: 18px;
    padding: 14px;
  }

  .game-header {
    gap: 8px;
  }

  .game-header h2 {
    font-size: 24px;
  }

  .meta,
  .card p,
  .message {
    font-size: 14px;
  }

  .game-layout {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .board-wrap {
    width: min(100%, 96vw);
  }

  .queens-board {
    border-radius: 14px;
    padding: var(--cell-gap);
    max-height: calc(100svh - 310px);
    min-height: 280px;
  }

  .cell {
    border-radius: 7px;
    font-size: clamp(20px, 9vw, 38px);
  }

  .cell.error {
    outline-width: 3px;
  }

  .control-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-top: 10px;
  }

  .primary-button,
  .secondary-button,
  .ghost-button,
  .danger-button,
  select {
    width: 100%;
    min-height: 42px;
    padding: 9px 10px;
    border-radius: 12px;
    font-size: 14px;
  }

  .footer {
    border-radius: 16px;
    padding: 10px 12px;
    flex-direction: column;
    font-size: 12px;
    gap: 4px;
  }
}

@media (max-width: 380px) {
  .queens-board {
    max-height: calc(100svh - 340px);
    min-height: 250px;
  }

  .control-row {
    grid-template-columns: 1fr;
  }
}

@media (orientation: landscape) and (max-height: 540px) {
  .app-shell {
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .topbar,
  .footer {
    display: none;
  }

  .game-layout {
    grid-template-columns: minmax(280px, 1fr) 250px;
    align-items: start;
  }

  .board-wrap {
    width: min(72vh, 62vw);
  }

  .queens-board {
    max-height: 84vh;
  }

  .card,
  .game-panel {
    padding: 10px;
  }

  .control-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }

  .primary-button,
  .secondary-button,
  .ghost-button,
  .danger-button {
    min-height: 36px;
    padding: 7px 8px;
    font-size: 13px;
  }
}
