/* Letterloom — classic board styling */

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

:root {
  --board-bg: #1b6b45;
  --board-cell: #1f6b47;
  --board-gap: 2px;
  --cell-size: 36px;
  --tw: #c0392b;
  --dw: #f4a6b8;
  --tl: #1a6fa8;
  --dl: #a8d4f0;
  --tile-bg: #f5e6c8;
  --tile-border: #c9a66b;
  --tile-shadow: #8b6914;
  --text-dark: #2c2416;
  --text-muted: #5c5346;
  --sidebar-bg: #f7f3eb;
  --panel-border: #d4c4a8;
  --accent: #1b6b45;
  --accent-hover: #145a38;
  --warning: #d35400;
  --warning-text: #8a3600;
  --error: #c0392b;
  --radius: 6px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

body {
  margin: 0;
  font-family: var(--font);
  background: linear-gradient(160deg, #e8f0e8 0%, #d4e4d4 100%);
  color: var(--text-dark);
  min-height: 100vh;
  min-height: 100dvh;
}

/* Touch interaction defaults (all devices) */
button,
.btn,
.board-cell,
.rack-tile,
.setup-import-btn {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-nav {
  position: relative;
  z-index: 2000;
}

.skip-link {
  position: absolute;
  left: 0.75rem;
  top: 0.75rem;
  z-index: 2000;
  padding: 0.6rem 1rem;
  background: #fff;
  color: var(--text-dark);
  font-weight: 700;
  border-radius: var(--radius);
  text-decoration: none;
  transform: translateY(-200%);
}

.skip-link + .skip-link:focus {
  top: 3.25rem;
}

.skip-link:focus {
  transform: none;
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* App shell */

.letterloom-app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem;
}

.app-message {
  margin-top: 1rem;
  padding: 0.75rem 1rem;
  background: #e8f5e9;
  border: 1px solid #a5d6a7;
  border-radius: var(--radius);
  color: #2e7d32;
  font-size: 0.95rem;
}

.app-message.error {
  background: #fdecea;
  border-color: #f5b7b1;
  color: var(--error);
}

/* Setup screen */

.setup-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 70vh;
}

.setup-card {
  background: #fff;
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 2.5rem 3rem;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  text-align: center;
  width: 100%;
  max-width: 520px;
}

.setup-title {
  margin: 0 0 0.25rem;
  font-size: 2.5rem;
  color: var(--accent);
  letter-spacing: 0.04em;
}

.setup-tagline {
  margin: 0 0 1.5rem;
  font-size: 0.95rem;
  color: var(--text-muted);
  font-style: italic;
}

.setup-subtitle {
  margin: 0 0 2rem;
  color: var(--text-muted);
}

.setup-loading {
  text-align: center;
  color: var(--text-muted);
  padding: 3rem;
}

.setup-error {
  margin: 0;
  color: var(--error);
  line-height: 1.5;
}

.setup-saves {
  text-align: left;
  margin-bottom: 1.25rem;
}

.setup-saves-title {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
  color: var(--accent);
}

.setup-saves-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  max-height: 280px;
  overflow-y: auto;
}

.setup-save-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: #f9f6f0;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
}

.setup-save-info {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-width: 0;
}

.setup-save-name {
  font-weight: 700;
  font-size: 0.95rem;
}

.setup-save-status {
  font-size: 0.85rem;
  color: var(--accent);
}

.setup-save-scores {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.setup-save-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.setup-save-actions {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex-shrink: 0;
}

.setup-divider {
  border: none;
  border-top: 1px solid var(--panel-border);
  margin: 0 0 1.5rem;
}

.setup-import {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--panel-border);
}

.setup-import-btn {
  display: inline-block;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.setup-import-input,
.save-import-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
}

.setup-import-hint {
  margin: 0.5rem 0 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  text-align: left;
}

.form-field label {
  display: block;
  margin-bottom: 0.35rem;
  font-weight: 600;
  font-size: 0.9rem;
}

.setup-input {
  width: 100%;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  /* 16px minimum prevents iOS focus zoom */
  font-size: 16px;
  font-family: inherit;
}

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

.setup-start {
  margin-top: 0.5rem;
  width: 100%;
}

/* Game layout */

.game-main {
  display: flex;
  gap: 1.5rem;
  align-items: flex-start;
}

.board-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.game-sidebar {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Board */

.board-container {
  padding: 8px;
  background: var(--board-bg);
  border-radius: 8px;
  box-shadow:
    0 4px 16px rgba(0, 0, 0, 0.2),
    inset 0 0 0 2px rgba(255, 255, 255, 0.1);
}

.letterloom-board {
  display: flex;
  flex-direction: column;
  gap: var(--board-gap);
}

.board-row {
  display: grid;
  grid-template-columns: repeat(15, var(--cell-size));
  gap: var(--board-gap);
}

.board-cell {
  position: relative;
  width: var(--cell-size);
  height: var(--cell-size);
  padding: 0;
  border: none;
  border-radius: 3px;
  background: var(--board-cell);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.7rem;
  font-weight: 700;
  color: #fff;
  transition: filter 0.12s ease;
  user-select: none;
  -webkit-user-select: none;
}

.board-cell.occupied {
  cursor: default;
  background: transparent;
}

.board-cell.pending-cell {
  background: rgba(255, 255, 255, 0.15);
}

.board-cell.premium-tw {
  background: var(--tw);
}

.board-cell.premium-dw {
  background: var(--dw);
  color: #7a3040;
}

.board-cell.premium-tl {
  background: var(--tl);
}

.board-cell.premium-dl {
  background: var(--dl);
  color: #1a5276;
}

.premium-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  line-height: 1;
  letter-spacing: -0.02em;
}

.center-star {
  position: absolute;
  font-size: 1rem;
  color: #fff;
  pointer-events: none;
}

.board-cell.occupied .premium-label,
.board-cell.pending-cell .premium-label,
.board-cell.occupied .center-star,
.board-cell.pending-cell .center-star {
  display: none;
}

/* Tiles */

.tile-face {
  position: relative;
  width: calc(var(--cell-size) - 2px);
  height: calc(var(--cell-size) - 2px);
  background: linear-gradient(145deg, #faf0dc 0%, var(--tile-bg) 40%, #e8d4a8 100%);
  border: 1px solid var(--tile-border);
  border-radius: 4px;
  box-shadow:
    1px 2px 3px rgba(0, 0, 0, 0.25),
    inset 0 1px 0 rgba(255, 255, 255, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dark);
  user-select: none;
}

.tile-face.tile-small {
  width: calc(var(--cell-size) - 4px);
  height: calc(var(--cell-size) - 4px);
}

.tile-face.tile-pending {
  outline: 2px dashed var(--accent);
  outline-offset: -1px;
  opacity: 0.92;
}

.tile-letter {
  font-size: 1.15rem;
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
}

.tile-small .tile-letter {
  font-size: 1rem;
}

.tile-points {
  position: absolute;
  right: 3px;
  bottom: 2px;
  font-size: 0.65rem;
  font-weight: 700;
  line-height: 1;
}

/* Rack */

.letterloom-rack {
  width: 100%;
  max-width: calc(15 * var(--cell-size) + 14 * var(--board-gap) + 16px);
}

.rack-slots {
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 10px 14px;
  background: linear-gradient(180deg, #6b4423 0%, #4a2f18 100%);
  border-radius: 8px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.25);
}

.rack-slot {
  width: calc(var(--cell-size) + 4px);
  height: calc(var(--cell-size) + 8px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.rack-tile {
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 4px;
  transition: transform 0.1s ease;
  user-select: none;
  -webkit-user-select: none;
}

.rack-tile.selected .tile-face,
.rack-tile.exchange-mode.selected .tile-face {
  outline: 3px solid #f1c40f;
  outline-offset: 1px;
  transform: translateY(-6px);
}

.rack-tile.exchange-mode.selected .tile-face {
  outline-color: var(--warning);
}

.rack-player-label {
  text-align: center;
  margin-top: 0.5rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-weight: 600;
}

/* Auto-detected placement direction */

.direction-hint {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 1.75rem;
}

.direction-hint-text {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--accent-hover);
  letter-spacing: 0.02em;
}

.direction-hint-ambiguous {
  color: var(--text-muted);
  font-weight: 500;
}

/* Sidebar panels */

.status-panel,
.score-preview,
.controls-panel,
.save-panel {
  background: var(--sidebar-bg);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 1rem;
}

.status-heading {
  margin: 0 0 0.25rem;
  font-size: 1.1rem;
}

.status-subheading {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.status-current-player {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--accent);
}

.status-turn-number {
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.status-scores,
.status-bag,
.status-last-move {
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--panel-border);
}

.score-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.score-item {
  display: flex;
  justify-content: space-between;
  padding: 0.35rem 0;
  font-size: 0.95rem;
}

.score-item.active-player {
  font-weight: 700;
  color: var(--accent);
}

.score-breakdown {
  margin: 0.5rem 0 0;
  padding-left: 1.25rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.bag-count {
  margin: 0;
  font-size: 0.95rem;
}

.last-move-detail {
  margin: 0;
  font-size: 0.9rem;
}

/* Score preview */

.preview-heading {
  margin: 0 0 0.5rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.preview-total {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
}

.preview-empty,
.preview-error {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.preview-error {
  color: var(--error);
  font-weight: 600;
}

.preview-breakdown {
  margin: 0.5rem 0;
  padding-left: 1.25rem;
  font-size: 0.85rem;
}

.preview-words {
  margin: 0.5rem 0 0;
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Controls */

.control-actions,
.exchange-section {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.exchange-section {
  margin-top: 0.75rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--panel-border);
}

.control-message {
  margin: 0.75rem 0 0;
  font-size: 0.85rem;
  color: var(--warning-text);
  font-weight: 600;
}

.btn {
  min-height: 44px;
  padding: 0.6rem 1rem;
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  background: #fff;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.btn:disabled {
  background: #e8e2d6;
  border-color: #c9bda8;
  color: #5c5346;
  cursor: not-allowed;
}

.btn-primary:disabled {
  background: #8aa896;
  border-color: #8aa896;
  color: #fff;
}

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

.btn-warning {
  background: var(--warning);
  border-color: var(--warning);
  color: #fff;
}

.btn-secondary {
  background: #fff;
  color: var(--text-dark);
}

.btn-small {
  min-height: 44px;
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
}

.btn-danger {
  color: var(--error);
  border-color: #e8b4b0;
}

.save-panel {
  padding: 1rem;
  border-radius: var(--radius);
}

.save-status {
  margin: 0 0 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.save-actions {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.save-actions label.btn {
  display: inline-block;
  position: relative;
}

/* Game over overlay */

.app-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

.game-over-modal {
  width: 100%;
  max-width: 400px;
}

.game-over-card {
  background: #fff;
  border-radius: 12px;
  padding: 2rem;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
  text-align: center;
}

.game-over-title {
  margin: 0 0 0.5rem;
  font-size: 1.75rem;
  color: var(--accent);
}

.game-over-reason {
  margin: 0 0 0.5rem;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.game-over-winner {
  margin: 0 0 1.25rem;
  font-size: 1.35rem;
  font-weight: 700;
}

.final-scores {
  list-style: none;
  margin: 0 0 1.5rem;
  padding: 0;
  text-align: left;
}

.final-score-item {
  display: grid;
  grid-template-columns: 1.5rem 1fr auto;
  gap: 0.5rem;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--panel-border);
  font-size: 1rem;
}

.final-score-item:last-child {
  border-bottom: none;
}

.final-points {
  font-weight: 700;
  color: var(--accent);
}

.game-over-actions {
  display: flex;
  justify-content: center;
}

/* Hover affordances — only when a real fine pointer can hover
   (avoids sticky :hover after taps on touch devices) */
@media (hover: hover) and (pointer: fine) {
  .board-cell:hover:not(.occupied) {
    filter: brightness(1.12);
  }

  .rack-tile:hover {
    transform: translateY(-4px);
  }

  .btn:hover:not(:disabled) {
    background: #f0ebe0;
  }

  .btn-primary:hover:not(:disabled) {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
  }

  .btn-warning:hover:not(:disabled) {
    background: #b84600;
    border-color: #b84600;
  }

  .btn-danger:hover:not(:disabled) {
    background: #fdecea;
    border-color: var(--error);
  }
}

.board-cell:focus-visible,
.rack-tile:focus-visible,
.btn:focus-visible,
.setup-import-btn:focus-visible,
.skip-link:focus-visible {
  outline: 3px solid #f1c40f;
  outline-offset: 2px;
}

.board-cell:focus-visible {
  z-index: 2;
  outline-offset: 1px;
}

.rack-tile:focus-visible {
  outline-offset: 3px;
}

.app-dialog-host:not(.hidden) {
  position: fixed;
  inset: 0;
  z-index: 1100;
}

.app-dialog {
  background: #fff;
  border-radius: 12px;
  padding: 1.75rem;
  max-width: 420px;
  width: 100%;
  text-align: left;
}

.app-dialog-title {
  margin: 0 0 0.5rem;
  font-size: 1.35rem;
  color: var(--accent);
}

.app-dialog-message {
  margin: 0 0 1rem;
  color: var(--text-muted);
  line-height: 1.45;
}

.app-dialog-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1.25rem;
}

.letter-picker {
  display: grid;
  grid-template-columns: repeat(7, minmax(44px, 1fr));
  gap: 0.35rem;
  margin: 0.75rem 0 0;
}

.letter-picker .btn {
  min-width: 44px;
  padding: 0.4rem;
}

.help-list {
  margin: 0;
  padding-left: 1.2rem;
  text-align: left;
  color: var(--text-dark);
  line-height: 1.5;
}

.help-list li + li {
  margin-top: 0.4rem;
}

.setup-help {
  margin: 0 0 1.25rem;
  text-align: left;
}

.setup-help summary {
  cursor: pointer;
  font-weight: 700;
  color: var(--accent-hover);
  min-height: 44px;
  display: flex;
  align-items: center;
}

.setup-help summary:focus-visible {
  outline: 3px solid #f1c40f;
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }

  .rack-tile.selected .tile-face,
  .rack-tile.exchange-mode.selected .tile-face,
  .rack-tile:hover {
    transform: none;
  }
}

@media (forced-colors: active) {
  .board-cell,
  .tile-face,
  .btn,
  .rack-slots {
    border: 1px solid ButtonText;
  }

  .board-cell:focus-visible,
  .rack-tile:focus-visible,
  .btn:focus-visible {
    outline: 3px solid Highlight;
  }
}

/* Responsive
   Coarse layout fallbacks only. Finer mobile layout, fluid board sizing,
   and touch-oriented spacing are owned by css/mobile.css (loaded after). */

@media (max-width: 900px) {
  :root {
    --cell-size: 30px;
  }

  .game-main {
    flex-direction: column;
    align-items: center;
  }

  .game-sidebar {
    width: 100%;
    max-width: calc(15 * var(--cell-size) + 14 * var(--board-gap) + 16px);
  }
}

@media (max-width: 560px) {
  /* Intentionally no --cell-size or tile glyph sizes here.
     mobile.css owns fluid board sizing and scaled letter/points type. */

  .letterloom-app {
    padding: 0.75rem;
  }

  .setup-card {
    padding: 1.5rem;
  }
}