/* Эрудит — styles. Light and dark themes via CSS variables, mobile-first layout. */

:root {
  --bg: #f5efe4;
  --surface: #fffdf8;
  --surface-2: #f0e7d8;
  --border: #e2d6c2;
  --text: #2c2418;
  --muted: #7d705e;
  --accent: #3d2f1e;
  --accent-text: #fbf3e4;
  --focus: #c98a2b;
  --danger: #b4372f;
  --ok: #2f8a5b;
  --warn-bg: #fff4dc;
  --warn-border: #ecc97a;

  --cell: #ebe2d2;
  --cell-border: #dcd0bc;
  --tile: #3d2f1e;
  --tile-text: #f8ecd6;
  --tile-shadow: #1d140b;
  --tile-draft: #c98a2b;
  --tile-draft-text: #fffaf0;
  --tile-last: #6b4a22;
  --tile-pending: #7a6448;

  --dl: #bfe3de;
  --dl-text: #2c6e66;
  --tl: #b9d6ef;
  --tl-text: #2b5c86;
  --dw: #f0c8d6;
  --dw-text: #8e3b5a;
  --tw: #efc6a8;
  --tw-text: #8a4a1f;

  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 16px rgb(60 40 10 / 6%);
  --font:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-tile: Georgia, 'Times New Roman', serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1814;
    --surface: #26211b;
    --surface-2: #2f2922;
    --border: #3d352b;
    --text: #efe6d6;
    --muted: #a89a86;
    --accent: #e6d5b8;
    --accent-text: #2c2418;
    --warn-bg: #3a2f1c;
    --warn-border: #7a5d26;

    --cell: #2c2620;
    --cell-border: #3a3229;
    --tile: #e6d5b8;
    --tile-text: #2c2418;
    --tile-shadow: #7a6448;
    --tile-draft: #e0a548;
    --tile-draft-text: #2c2418;
    --tile-last: #f2c88a;
    --tile-pending: #b39d7c;

    --dl: #2a4b47;
    --dl-text: #9fd6cd;
    --tl: #29425a;
    --tl-text: #a6cdf0;
    --dw: #57303f;
    --dw-text: #f0b4ca;
    --tw: #5a3a24;
    --tw-text: #f2c29d;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%);
  }
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 var(--font);
  -webkit-tap-highlight-color: transparent;
}

a {
  color: inherit;
}

button,
input {
  font: inherit;
  color: inherit;
}

[hidden] {
  display: none !important;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 13px;
}

.center {
  text-align: center;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 1200px;
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 10px
    max(16px, env(safe-area-inset-left));
}

.logo {
  font: 700 22px var(--font-tile);
  text-decoration: none;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.game-code {
  font:
    600 14px ui-monospace,
    SFMono-Regular,
    Menlo,
    monospace;
  color: var(--muted);
  letter-spacing: 0.05em;
}

/* ---------- generic layout ---------- */

.page {
  max-width: 1200px;
  margin: 0 auto;
  padding: 8px 16px 32px;
}

.page.narrow {
  max-width: 520px;
}

.hero {
  text-align: center;
  margin: 12px 0 20px;
}

.hero h1 {
  font: 700 clamp(36px, 9vw, 52px) / 1.1 var(--font-tile);
  margin: 0 0 8px;
}

.hero p {
  margin: 0;
  color: var(--muted);
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.card + .card {
  margin-top: 12px;
}

.stack > * + * {
  margin-top: 14px;
}

.card-section {
  border-top: 1px solid var(--border);
  padding-top: 14px;
}

.section-title {
  font-size: 15px;
  font-weight: 650;
  margin: 0 0 10px;
}

/* ---------- forms and buttons ---------- */

.field {
  display: block;
}

.field-label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}

.input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 16px; /* 16px+ prevents iOS zoom on focus */
}

.input:focus {
  outline: 2px solid var(--focus);
  outline-offset: -1px;
}

.form-error {
  color: var(--danger);
  font-size: 14px;
  margin: 8px 0 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
  text-decoration: none;
  font-weight: 550;
  white-space: nowrap;
  transition:
    background 0.15s,
    transform 0.05s;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.btn-ghost {
  background: transparent;
}

.btn-danger {
  color: var(--danger);
}

.btn.btn-danger:not(.btn-ghost) {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.btn-small {
  min-height: 34px;
  padding: 6px 12px;
  font-size: 14px;
}

.btn-wide {
  width: 100%;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  flex: none;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  cursor: pointer;
}

.checkbox input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

/* ---------- settings ---------- */

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

.setting-title {
  font-size: 14px;
  font-weight: 600;
  margin-bottom: 8px;
}

.setting-value {
  font-weight: 500;
  color: var(--muted);
  margin-left: 6px;
}

.segmented {
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  gap: 6px;
}

.segment {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
  text-align: left;
  min-height: 44px;
}

.segment.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  background: var(--surface-2);
}

.segment:disabled {
  cursor: default;
}

.segment:disabled:not(.active) {
  opacity: 0.55;
}

.segment-label {
  font-weight: 600;
  font-size: 15px;
}

.segment-hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.25;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  min-height: 38px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  cursor: pointer;
  font-size: 14px;
}

.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.chip:disabled {
  cursor: default;
}

.chip:disabled:not(.active) {
  opacity: 0.55;
}

.timer-custom {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
}

.timer-range {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
  height: 32px;
}

.timer-output {
  min-width: 56px;
  text-align: right;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

/* ---------- lobby ---------- */

.invite {
  display: flex;
  gap: 8px;
}

.invite-url {
  flex: 1;
  min-width: 0;
  font-size: 14px;
}

/* ---------- game layout ---------- */

.game {
  --board-size: min(calc(100vw - 16px), 640px);
  display: grid;
  grid-template-areas: 'play' 'controls' 'sidebar';
  gap: 12px;
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 8px 24px;
  justify-content: center;
}

.play {
  grid-area: play;
  width: var(--board-size);
  display: grid;
  gap: 10px;
  justify-self: center;
}

.controls {
  grid-area: controls;
  width: var(--board-size);
  display: grid;
  gap: 10px;
  justify-self: center;
}

.sidebar {
  grid-area: sidebar;
  width: 100%;
  max-width: var(--board-size);
  justify-self: center;
}

/* Tablet portrait: sidebar cards side by side under the board */
@media (min-width: 700px) and (max-width: 979px) {
  .game {
    --board-size: min(calc(100vw - 32px), 720px);
  }

  .sidebar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    align-items: start;
  }

  .sidebar .card + .card {
    margin-top: 0;
  }
}

/* Desktop and tablet landscape: the board takes the full height, rack and buttons on the right */
@media (min-width: 980px) {
  .game {
    --board-size: clamp(460px, calc(100vh - 170px), 760px);
    --side: 340px;
    grid-template-columns: var(--board-size) var(--side);
    grid-template-rows: auto 1fr;
    grid-template-areas:
      'play controls'
      'play sidebar';
    align-items: start;
    padding: 0 24px 24px;
  }

  .controls,
  .sidebar {
    width: var(--side);
  }

  .controls .rack {
    --tile-size: calc((var(--side) - 62px) / 7);
  }

  .controls .actions-row {
    flex-wrap: wrap;
  }
}

/* ---------- status and timer ---------- */

.status {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.status.mine {
  border-color: var(--focus);
  box-shadow: 0 0 0 1px var(--focus);
}

.timer {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
}

.timer-text {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.timer-track {
  width: 80px;
  height: 6px;
  border-radius: 3px;
  background: var(--surface-2);
  overflow: hidden;
}

.timer-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  transition: width 0.25s linear;
}

.timer.low .timer-text {
  color: var(--danger);
}

.timer.low .timer-fill {
  background: var(--danger);
}

.timer.paused {
  opacity: 0.6;
}

/* ---------- board ---------- */

.board-wrap {
  width: var(--board-size);
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.board {
  container-type: inline-size;
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  gap: 2px;
}

.cell {
  position: relative;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--cell);
  border-radius: 3px;
  min-width: 0;
  user-select: none;
  -webkit-user-select: none;
}

.premium-dl {
  background: var(--dl);
  color: var(--dl-text);
}

.premium-tl {
  background: var(--tl);
  color: var(--tl-text);
}

.premium-dw {
  background: var(--dw);
  color: var(--dw-text);
}

.premium-tw {
  background: var(--tw);
  color: var(--tw-text);
}

.premium-label {
  font-size: 9px;
  font-size: 2.1cqi;
  font-weight: 700;
  letter-spacing: -0.02em;
  opacity: 0.9;
}

.center-star {
  color: var(--muted);
  font-size: 14px;
  font-size: 3.6cqi;
}

.cell.drop-hover {
  outline: 2px solid var(--focus);
  outline-offset: -2px;
}

/* ---------- tiles ---------- */

.tile {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 3px;
  background: var(--tile);
  color: var(--tile-text);
  box-shadow: 0 2px 0 var(--tile-shadow);
  font-family: var(--font-tile);
  font-weight: 700;
  user-select: none;
  -webkit-user-select: none;
}

.board .tile {
  font-size: 13px;
  font-size: 3.5cqi;
  box-shadow: 0 1px 0 var(--tile-shadow);
}

.tile-letter {
  line-height: 1;
}

.tile-value {
  position: absolute;
  right: 8%;
  bottom: 4%;
  font: 600 0.42em var(--font);
  opacity: 0.85;
}

.tile.joker .tile-letter {
  font-style: italic;
  opacity: 0.85;
}

.tile.joker {
  background: color-mix(in srgb, var(--tile) 75%, var(--focus));
}

.tile.last {
  background: var(--tile-last);
}

.tile.draft {
  background: var(--tile-draft);
  color: var(--tile-draft-text);
  box-shadow: 0 1px 0 color-mix(in srgb, var(--tile-draft) 60%, black);
  cursor: grab;
  touch-action: none;
}

.tile.pending {
  background: var(--tile-pending);
  outline: 2px dashed var(--focus);
  outline-offset: -2px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 14px;
  font-size: 12px;
  color: var(--muted);
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

/* ---------- rack and actions ---------- */

.rack {
  --tile-size: min(calc((var(--board-size) - 60px) / 7), 56px);
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 10px;
  min-height: calc(var(--tile-size) + 22px);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.rack .tile,
.tile-slot {
  width: var(--tile-size);
  height: var(--tile-size);
  flex: none;
  font-size: calc(var(--tile-size) * 0.5);
  border-radius: 6px;
}

.rack .tile {
  cursor: grab;
  touch-action: none;
  transition:
    transform 0.1s,
    box-shadow 0.1s;
}

.tile-slot {
  border: 1px dashed var(--border);
}

.rack .tile.selected {
  transform: translateY(-6px);
  box-shadow:
    0 0 0 3px var(--focus),
    0 4px 0 var(--tile-shadow);
}

.rack.exchange .tile {
  cursor: pointer;
}

.rack .tile.marked {
  transform: translateY(-6px);
  box-shadow:
    0 0 0 3px var(--danger),
    0 4px 0 var(--tile-shadow);
}

.tile.dragging {
  opacity: 0.3;
}

.drag-clone {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 100;
  pointer-events: none;
  font-size: 24px;
  border-radius: 6px;
  box-shadow: 0 10px 24px rgb(0 0 0 / 30%);
}

.actions {
  display: grid;
  gap: 8px;
}

.actions-row {
  display: flex;
  gap: 8px;
}

.actions-row > .btn {
  flex: 1;
}

.btn-submit b {
  font-weight: 800;
}

.hint {
  margin: 0;
  font-size: 14px;
  color: var(--muted);
  text-align: center;
}

.notice {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: var(--radius);
}

.notice p {
  margin: 0;
}

.notice-warn {
  background: var(--warn-bg);
  border: 1px solid var(--warn-border);
}

/* ---------- sidebar ---------- */

.players {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.player {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.player.current {
  border-color: var(--focus);
  background: var(--surface-2);
}

.player.out {
  opacity: 0.55;
}

.player-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.player-tiles {
  font-size: 13px;
}

.player-score {
  font-weight: 800;
  font-size: 18px;
  min-width: 2ch;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--border);
  flex: none;
}

.dot.online {
  background: var(--ok);
}

.bag-info {
  margin: 12px 0 4px;
}

.sidebar .card > p {
  margin: 6px 0;
}

.sidebar .btn-danger {
  margin-top: 8px;
}

.log .moves {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 340px;
  overflow-y: auto;
  display: grid;
  gap: 4px;
  font-size: 14px;
}

.moves li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}

.moves li:last-child {
  border-bottom: 0;
}

.move-score {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.badge {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--muted);
}

/* ---------- vote, dialogs, toast ---------- */

.vote-panel {
  display: grid;
  gap: 8px;
  padding: 12px;
  border: 1px solid var(--warn-border);
  background: var(--warn-bg);
  border-radius: var(--radius);
}

.vote-panel p {
  margin: 0;
}

.vote-words {
  font: 700 18px var(--font-tile);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgb(20 14 8 / 45%);
  backdrop-filter: blur(2px);
}

.dialog {
  width: min(420px, 100%);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  background: var(--surface);
  border-radius: 16px;
  padding: 20px;
  box-shadow: 0 20px 50px rgb(0 0 0 / 30%);
  display: grid;
  gap: 14px;
}

.dialog h3 {
  margin: 0;
  font-size: 20px;
  text-align: center;
}

.dialog p {
  margin: 0;
  text-align: center;
}

.letter-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
}

.letter-btn {
  aspect-ratio: 1;
  border: 0;
  border-radius: 6px;
  background: var(--tile);
  color: var(--tile-text);
  font: 700 18px var(--font-tile);
  cursor: pointer;
  box-shadow: 0 2px 0 var(--tile-shadow);
}

.result-title {
  font: 700 28px var(--font-tile) !important;
}

.result-scores {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

.result-scores li {
  display: flex;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: max(20px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 200;
  max-width: calc(100vw - 32px);
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--accent-text);
  box-shadow: 0 8px 24px rgb(0 0 0 / 25%);
  font-weight: 550;
  animation: toast-in 0.2s ease-out;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 8px);
  }
}

/* ---------- computer thinking ---------- */

.status.bot-thinking {
  justify-content: flex-start;
  background: var(--warn-bg);
  border-color: var(--warn-border);
}

.bot-phrase {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  font-style: italic;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  animation: phrase-in 0.3s ease-out;
}

.thinking-dots {
  display: inline-flex;
  gap: 3px;
  flex: none;
}

.thinking-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--focus);
  animation: thinking 1.2s infinite ease-in-out;
}

.thinking-dots i:nth-child(2) {
  animation-delay: 0.15s;
}

.thinking-dots i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes thinking {
  0%,
  80%,
  100% {
    opacity: 0.25;
    transform: translateY(0);
  }
  40% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

@keyframes phrase-in {
  from {
    opacity: 0;
  }
}
