:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-muted: #f8fafc;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text: #0f172a;
  --text-muted: #64748b;
  --accent: #0f766e;
  --accent-hover: #0d9488;
  --accent-soft: #ccfbf1;
  --accent-ring: rgba(15, 118, 110, 0.22);
  --p0: #0f766e;
  --p0-soft: #f0fdfa;
  --p1: #c2410c;
  --p1-soft: #fff7ed;
  --path-bg: #fef3c7;
  --path-border: #f59e0b;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.07);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  background-image: radial-gradient(circle at 20% 0%, #e6f4f1 0%, transparent 45%),
                    radial-gradient(circle at 80% 100%, #e2e8f0 0%, transparent 45%);
  user-select: none;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 520px;
  height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  background: transparent;
}

.hidden {
  display: none !important;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  z-index: 10;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-logo {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 18px;
  letter-spacing: -0.02em;
  box-shadow: 0 2px 0 #115e59;
}

.brand-text {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.brand-name {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: -0.02em;
  color: var(--text);
}

.brand-sub {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.conn-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--danger);
}

.conn-pill.ok .conn-dot {
  background: #16a34a;
  box-shadow: 0 0 0 2px rgba(22, 163, 74, 0.2);
}

.conn-pill.ok {
  color: #15803d;
  border-color: #bbf7d0;
  background: #f0fdf4;
}

/* Top in-game actions */
.top-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.action-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.action-btn:hover {
  background: var(--surface-muted);
  border-color: var(--border-strong);
}

.action-btn.danger:hover {
  background: var(--danger-soft);
  border-color: #fecaca;
  color: var(--danger);
}

.action-btn span {
  display: none;
}

/* Scoreboard */
.scoreboard {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.player-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-muted);
  min-width: 0;
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}

.player-card.right {
  justify-content: flex-end;
  text-align: right;
}

.player-card.active.left {
  background: var(--p0-soft);
  border-color: var(--p0);
  box-shadow: 0 0 0 2px rgba(15, 118, 110, 0.12);
}

.player-card.active.right {
  background: var(--p1-soft);
  border-color: var(--p1);
  box-shadow: 0 0 0 2px rgba(194, 65, 12, 0.12);
}

.player-avatar {
  width: 24px;
  height: 24px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 800;
  color: #fff;
  flex-shrink: 0;
}

.player-avatar.p0 { background: var(--p0); }
.player-avatar.p1 { background: var(--p1); }

.player-info {
  min-width: 0;
  flex: 1;
}

.player-name {
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.player-time {
  font-size: 11px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.player-last-word {
  font-size: 11px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.player-last-word.muted {
  color: var(--text-muted);
  font-weight: 400;
}

.player-score {
  font-size: 18px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  padding: 0 2px;
}

.score-center {
  text-align: center;
  padding: 0 4px;
}

.turn-status {
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}

.turn-status.yours {
  background: #dcfce7;
  border-color: #86efac;
  color: #15803d;
}

.turn-status.theirs {
  background: #fee2e2;
  border-color: #fca5a5;
  color: #b91c1c;
}

.turn-status.ended {
  background: #e0f2fe;
  border-color: #7dd3fc;
  color: #0369a1;
}

/* Main area */
#main-area {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
  display: flex;
  flex-direction: column;
}

.view {
  display: flex;
  flex-direction: column;
}

/* Lobby */
.lobby-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.lobby-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
  box-shadow: var(--shadow-sm);
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.panel-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}

.inline-form {
  display: flex;
  gap: 8px;
}

input, select {
  width: 100%;
  padding: 9px 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}

input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

.create-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.span-2 {
  grid-column: 1 / -1;
}

.check-label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  padding: 4px 0;
}

.check-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  margin: 0;
}

button {
  font-family: inherit;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.08s;
}

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

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 9px 16px;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-ghost {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border-strong);
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
}

.btn-ghost:hover {
  background: var(--surface-muted);
}

.btn-small {
  padding: 5px 11px;
  font-size: 12px;
  font-weight: 600;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
}

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

.full-width {
  width: 100%;
}

.rooms-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.room-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  gap: 10px;
}

.room-title {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 2px;
}

.room-meta {
  font-size: 12px;
  color: var(--text-muted);
}

/* Game Workspace */
.game-workspace {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.board-section {
  flex: 1;
  align-items: center;
}

.banner {
  width: 100%;
  max-width: 440px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 7px 10px;
  margin-bottom: 8px;
  font-size: 13px;
  box-shadow: var(--shadow-sm);
}

.banner-label {
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

.banner-actions {
  display: flex;
  gap: 6px;
}

.alert-banner {
  background: #fffbeb;
  border-color: #fcd34d;
  color: #92400e;
  font-weight: 600;
}

.invite-url {
  flex: 1;
  min-width: 0;
  padding: 5px 8px;
  font-size: 12px;
  background: var(--surface-muted);
}

/* Board */
.board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  margin: 2px 0 6px;
}

.board {
  width: 100%;
  max-width: 390px;
  aspect-ratio: 1 / 1;
  display: grid;
  gap: 5px;
  padding: 7px;
  background: #cbd5e1;
  border: 1px solid #94a3b8;
  border-radius: var(--radius-md);
  box-shadow: inset 0 2px 4px rgba(15, 23, 42, 0.08);
  touch-action: none;
}

.cell {
  position: relative;
  background: var(--surface-muted);
  border: 1px solid #e2e8f0;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(19px, 5.8vw, 28px);
  font-weight: 800;
  color: var(--text);
  cursor: pointer;
  transition: transform 0.08s, background 0.12s, border-color 0.12s, box-shadow 0.12s;
}

.cell.has-letter {
  background: var(--surface);
  border-color: #cbd5e1;
  box-shadow: 0 2px 0 #94a3b8;
}

.cell.selected-empty {
  background: var(--accent-soft);
  border: 2px dashed var(--accent);
  box-shadow: 0 0 0 2px var(--accent-ring);
}

.cell.in-path {
  background: var(--path-bg);
  border-color: var(--path-border);
  box-shadow: 0 2px 0 #d97706;
}

.cell.new-letter {
  color: var(--accent);
  background: #f0fdfa;
  border-color: var(--accent);
  box-shadow: 0 2px 0 #0f766e;
}

.cell.in-path.new-letter {
  background: #fde68a;
  color: var(--accent);
  border-color: var(--accent);
}

.cell-arrow {
  position: absolute;
  font-size: 11px;
  color: #92400e;
  font-weight: 800;
  pointer-events: none;
  line-height: 1;
}

.cell-arrow.right { right: 2px; top: 50%; transform: translateY(-50%); }
.cell-arrow.left  { left: 2px; top: 50%; transform: translateY(-50%); }
.cell-arrow.down  { bottom: 2px; left: 50%; transform: translateX(-50%); }
.cell-arrow.up    { top: 2px; left: 50%; transform: translateX(-50%); }

/* Word Bar */
.word-bar {
  width: 100%;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 2px 0 6px;
}

.hint-text {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  padding: 5px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.word-pill {
  display: inline-flex;
  align-items: center;
  background: var(--accent);
  border-radius: 999px;
  overflow: hidden;
  box-shadow: var(--shadow-md);
}

.word-submit {
  background: transparent;
  border: none;
  padding: 7px 16px;
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  letter-spacing: 0.02em;
}

.word-submit:hover {
  background: rgba(255, 255, 255, 0.1);
}

.word-clear {
  background: rgba(0, 0, 0, 0.16);
  border: none;
  padding: 7px 13px;
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  border-radius: 0;
}

.word-clear:hover {
  background: rgba(0, 0, 0, 0.28);
}

/* Keyboard */
.keyboard {
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.kb-row {
  display: flex;
  justify-content: center;
  gap: 4px;
}

.kb-key {
  flex: 1;
  max-width: 38px;
  height: 38px;
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  box-shadow: 0 2px 0 #cbd5e1;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.kb-key:hover:not(:disabled) {
  background: var(--p0-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.kb-key:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: none;
}

.kb-key:disabled {
  opacity: 0.42;
  box-shadow: none;
  cursor: default;
}

/* Definition Bar */
.definition-bar {
  width: 100%;
  max-width: 440px;
  margin-top: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  font-size: 12px;
  line-height: 1.4;
  color: var(--text);
}

/* Dictionary Picker in Lobby */
.dict-picker {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.dict-option {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
}

.dict-option-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dict-option-count {
  font-size: 11px;
  color: var(--muted);
}

/* Active Dictionary Info Bar */
.dict-info-bar {
  width: 100%;
  max-width: 440px;
  margin-top: 6px;
  text-align: center;
  font-size: 11px;
  line-height: 1.3;
  color: var(--muted);
}

/* Quick Word Check */
.word-check-box {
  width: 100%;
  max-width: 440px;
  margin-top: 8px;
}

.word-check-form input {
  text-transform: uppercase;
  font-size: 13px;
  padding: 6px 10px;
  user-select: text;
}

.word-check-result {
  margin-top: 6px;
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 12px;
  line-height: 1.35;
  color: var(--text);
}

.word-check-result.is-ok {
  background: var(--p0-soft);
  border-color: var(--accent);
}

.word-check-result.is-used {
  background: var(--path-bg);
  border-color: var(--path-border);
}

.word-check-result.is-miss {
  background: var(--danger-soft);
  border-color: var(--danger);
}

/* Side Panels (Chat & History) */
.side-panels {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
}

.side-card {
  flex: 1;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px;
  box-shadow: var(--shadow-sm);
}

.side-card-header {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.chat-messages {
  flex: 1;
  min-height: 120px;
  overflow-y: auto;
  margin-bottom: 8px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.chat-line {
  padding: 5px 8px;
  background: var(--surface-muted);
  border-radius: 6px;
  word-break: break-word;
  line-height: 1.35;
}

.chat-line strong {
  color: var(--accent);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface-muted);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 7px 10px;
  font-size: 13px;
}

.history-item strong {
  color: var(--accent);
  font-weight: 700;
}

/* Bottom Navigation (Mobile) */
.bottom-nav {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.nav-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  border-top: 2px solid transparent;
  padding: 8px 4px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  border-radius: 0;
}

.nav-btn.active {
  color: var(--accent);
  border-top-color: var(--accent);
  background: var(--p0-soft);
}

.unread-dot {
  position: absolute;
  top: 7px;
  right: calc(50% - 16px);
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--p1);
  border: 2px solid var(--surface);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px;
  max-width: 360px;
  width: 100%;
  box-shadow: 0 20px 40px rgba(15, 23, 42, 0.2);
}

.modal-title {
  font-size: 15px;
  font-weight: 800;
  margin-bottom: 8px;
  color: var(--text);
}

.modal-box p {
  margin: 0 0 16px;
  font-size: 14px;
  line-height: 1.45;
  color: var(--text-muted);
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 68px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  z-index: 60;
  max-width: 90%;
  text-align: center;
  box-shadow: var(--shadow-md);
}

/* Desktop / Wide Tablet Layout */
@media (min-width: 900px) {
  #app {
    max-width: 1080px;
    padding: 0 16px;
  }

  .app-header {
    margin-top: 12px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
  }

  .action-btn span {
    display: inline;
  }

  .scoreboard {
    margin-top: 10px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
  }

  #main-area {
    padding: 14px 0;
  }

  .lobby-grid {
    display: grid;
    grid-template-columns: 380px 1fr;
    gap: 16px;
    align-items: start;
  }

  .rooms-panel {
    min-height: 410px;
  }

  /* When in a game on desktop, show Board + Side Panels together */
  #app.in-game .game-workspace {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 16px;
    align-items: stretch;
  }

  #app.in-game #view-board {
    display: flex !important;
  }

  #app.in-game .side-panels {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  #app.in-game #view-history,
  #app.in-game #view-chat {
    display: flex !important;
  }

  #app.in-game #view-history {
    flex: 1;
    max-height: 260px;
  }

  #app.in-game #view-chat {
    flex: 1.3;
  }

  .bottom-nav {
    display: none;
  }

  .toast {
    bottom: 24px;
  }
}
