:root {
  color-scheme: light;
  --paper: #f3f0e8;
  --ink: #28352e;
  --muted: #64695e;
  --line: #d8d8cb;
  --accent: #a94730;
  --green: #45634e;
  --card: #fcfbf7;
  --wash: #ecebe2;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  touch-action: manipulation;
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 5px;
}

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

.page-shell {
  width: min(100% - 80px, 1000px);
  min-height: 100vh;
  min-height: 100svh;
  margin-inline: auto;
  display: flex;
  flex-direction: column;
}

/* ---------- Header and nav ---------- */

.site-header,
.site-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
}

.site-header {
  padding-block: 30px;
  border-bottom: 1px solid var(--line);
}

.wordmark {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: -0.5px;
}

.brand-mark {
  display: grid;
  grid-template-columns: repeat(2, 7px);
  gap: 3px;
  transform: rotate(-8deg);
}

.brand-mark span {
  width: 7px;
  height: 7px;
  background: var(--ink);
  border-radius: 1px;
}

.brand-mark span:last-child { background: var(--accent); }

.edition,
.eyebrow,
.options-label {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 1.5px;
  text-transform: uppercase;
}

.app-nav {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--wash);
  border-radius: 999px;
}

.app-nav a {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 999px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}

.app-nav a[aria-current="page"] {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(40 53 46 / 0.12);
}

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

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  text-decoration: none;
}

.user-chip #chipName {
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.loading {
  flex: 1;
  margin: 0;
  padding: 80px 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

/* ---------- Layout: intro or controls beside the board ---------- */

.game-layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 420px);
  align-items: center;
  gap: clamp(40px, 7vw, 100px);
  padding: 64px 28px;
}

#playView { align-items: start; }

.eyebrow { margin: 0 0 22px; }

h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(68px, 7.5vw, 96px);
  font-weight: 400;
  line-height: 0.95;
  letter-spacing: -5px;
}

h1 span { display: block; }

.play-title {
  font-size: clamp(44px, 5vw, 60px);
  letter-spacing: -2.5px;
  line-height: 1;
}

.play-title span { display: inline; }

.title-accent {
  color: var(--accent);
  font-style: italic;
}

.intro-copy {
  margin: 26px 0;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.7;
}

h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(40px, 5vw, 56px);
  font-weight: 400;
  letter-spacing: -2px;
  line-height: 1;
}

h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
}

.view:not(.game-layout) {
  flex: 1;
  padding: 56px 28px 64px;
}

.view-heading { margin-bottom: 28px; }
.view-heading .eyebrow { margin-bottom: 10px; }

/* ---------- Mode picker ---------- */

.mode-picker {
  display: grid;
  gap: 10px;
  margin-top: 32px;
}

.mode-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 14px;
  align-items: baseline;
  width: 100%;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

.mode-card[aria-checked="true"] {
  border-color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
}

.mode-number {
  grid-row: span 2;
  color: var(--accent);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 15px;
  font-style: italic;
}

.mode-name {
  font-size: 15px;
  font-weight: 650;
}

.mode-desc {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.mode-options {
  margin-top: 22px;
  padding-top: 20px;
  border-top: 1px solid var(--line);
}

.options-label { margin: 0 0 10px; }

.segmented {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--wash);
  border-radius: 10px;
}

.segmented button,
.tab {
  flex: 1;
  padding: 8px 12px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.segmented button[aria-checked="true"],
.tab[aria-selected="true"] {
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 1px 2px rgb(40 53 46 / 0.12);
}

.segmented + .hint { margin-top: 10px; }

.opponent-picker {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 14px;
  font-size: 12px;
  font-weight: 600;
}

/* ---------- Board ---------- */

.player-key,
.player-key > span {
  display: flex;
  align-items: center;
}

.player-key {
  flex-wrap: wrap;
  gap: 8px 24px;
  margin-bottom: 16px;
  font-size: 12px;
  font-weight: 500;
}

.player-key > span {
  gap: 8px;
  min-width: 0;
  overflow-wrap: anywhere;
}

.player-symbol {
  display: grid;
  flex-shrink: 0;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 50%;
  font-size: 16px;
  font-weight: 600;
}

.player-x,
.cell.X { color: var(--accent); }

.player-o,
.cell.O { color: var(--green); }

.board-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin-bottom: 14px;
}

.status-text {
  margin: 0;
  font-size: 14px;
  font-weight: 650;
}

.board-caption {
  color: var(--muted);
  font-size: 12px;
}

.game-board {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
  padding: 12px;
  background: #e2e4d9;
  border: 1px solid #d6dacb;
  border-radius: 20px;
}

.cell {
  display: grid;
  place-items: center;
  min-width: 0;
  padding: 0;
  aspect-ratio: 1;
  border: 1px solid #e3e2d9;
  border-radius: 10px;
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 3px 0 #ced2c4;
  font-size: clamp(34px, 6vw, 64px);
  font-weight: 500;
  cursor: pointer;
  transition: background-color 150ms ease, transform 150ms ease, box-shadow 150ms ease;
}

.cell:active:not(:disabled) {
  transform: translateY(2px);
  box-shadow: 0 1px 0 #ced2c4;
}

.cell:disabled { cursor: default; }

.cell.win {
  background: #f6e8df;
  border-color: var(--accent);
  box-shadow: 0 3px 0 #e2b8a6;
}

/* waiting for the other side: the board is shown but can't be played */
.game-board[data-waiting="true"] .cell:disabled:empty { opacity: 0.6; }

.reset-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  margin-top: 24px;
  padding: 12px 20px;
  border: 1px solid var(--ink);
  border-radius: 8px;
  background: var(--ink);
  color: var(--card);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 150ms ease;
}

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

.reset-btn svg {
  width: 17px;
  height: 17px;
}

.board-help {
  margin: 14px 0 0;
  color: var(--muted);
  text-align: center;
  font-size: 11px;
  line-height: 1.6;
}

.board-error { text-align: center; }

/* ---------- Cards, forms and buttons ---------- */

.card {
  padding: 22px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
}

.auth-card { width: 100%; }

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  padding: 4px;
  background: var(--wash);
  border-radius: 10px;
}

.form {
  display: grid;
  gap: 14px;
}

.form label {
  display: grid;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
}

input,
select {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 400;
}

input {
  width: 100%;
  padding: 10px 12px;
}

select {
  min-height: 40px;
  max-width: 180px;
  padding: 8px 10px;
}

input:focus-visible,
select:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 1px;
  border-color: transparent;
}

input:disabled { background: #f0efe8; }

.hint {
  margin: -8px 0 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.5;
}

.primary-btn,
.ghost-btn {
  min-height: 40px;
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 150ms ease, border-color 150ms ease;
}

.primary-btn {
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--card);
}

.ghost-btn {
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
}

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

.primary-btn:disabled,
.ghost-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.form-error,
.form-success {
  margin: 12px 0 0;
  font-size: 12px;
  line-height: 1.5;
}

.form-error { color: var(--accent); }
.form-success { color: var(--green); }
.form-error:empty,
.form-success:empty { display: none; }

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

.inline-form + .inline-form { margin-top: 10px; }

.empty {
  margin: 16px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

/* ---------- Room lists (play and chat) ---------- */

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

.room-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.room-item[aria-current="true"] {
  background: var(--wash);
  border-color: var(--line);
}

.room-number {
  font-size: 13px;
  font-weight: 650;
  white-space: nowrap;
}

.room-members {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  border-radius: 50%;
  background: var(--accent);
}

.pill {
  flex-shrink: 0;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--wash);
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
}

.pill.your-turn {
  background: var(--accent);
  color: var(--card);
}

/* ---------- Chat ---------- */

.chat-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr);
  gap: 20px;
  align-items: start;
}

.chat-card {
  display: flex;
  flex-direction: column;
  min-height: 460px;
}

.chat-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.chat-members {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  overflow-wrap: anywhere;
}

.chat-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 360px;
  margin: 0;
  padding: 16px 0;
  overflow-y: auto;
  list-style: none;
}

.messages:empty { display: none; }
.chat-card .empty { flex: 1; }

.message {
  max-width: 80%;
  padding: 9px 12px;
  border-radius: 12px;
  background: var(--wash);
  font-size: 14px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.message.mine {
  align-self: flex-end;
  background: var(--ink);
  color: var(--card);
}

.message.system {
  align-self: center;
  max-width: 100%;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.message-meta {
  display: block;
  margin-bottom: 2px;
  font-size: 11px;
  font-weight: 600;
  opacity: 0.7;
}

/* ---------- Settings ---------- */

.settings-grid {
  display: grid;
  gap: 20px;
  max-width: 560px;
}

.account-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.account-name {
  margin: 0;
  font-size: 18px;
  font-weight: 650;
  overflow-wrap: anywhere;
}

.account-username {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.account-form { margin-top: 16px; }

.confirm-text {
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.account-username:empty { display: none; }

.connection {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 12px;
}

.dot {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
  border-radius: 50%;
  background: #b9b9ad;
}

[data-state="online"] > .dot,
.dot[data-state="online"] { background: var(--green); }

[data-state="connecting"] > .dot,
.dot[data-state="connecting"] { background: #c99a3a; }

/* ---------- Footer ---------- */

.site-footer {
  padding-block: 22px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  line-height: 1.6;
}

.footer-marks {
  flex-shrink: 0;
  color: var(--accent);
  font-size: 15px;
  letter-spacing: 5px;
}

.footer-marks span { color: var(--green); }

/* ---------- Hover, small screens, motion ---------- */

@media (hover: hover) {
  .cell:hover:not(:disabled) {
    background: #f0eee4;
    transform: translateY(-2px);
    box-shadow: 0 5px 0 #ced2c4;
  }

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

  .ghost-btn:hover:not(:disabled),
  .mode-card:hover { border-color: var(--ink); }

  .room-item:hover { background: #f0eee4; }
  .app-nav a:hover { color: var(--ink); }
}

@media (max-width: 760px) {
  .page-shell { width: min(100% - 32px, 480px); }

  .site-header {
    flex-wrap: wrap;
    gap: 14px;
    padding-block: 20px;
  }

  .app-nav {
    order: 3;
    width: 100%;
  }

  .app-nav a {
    flex: 1;
    justify-content: center;
  }

  .edition {
    font-size: 9px;
    letter-spacing: 0.8px;
  }

  .game-layout {
    grid-template-columns: minmax(0, 1fr);
    gap: 34px;
    padding: 36px 0;
  }

  .auth-view .intro { text-align: center; }

  h1 {
    font-size: clamp(48px, 10.5vw, 72px);
    letter-spacing: -3px;
    line-height: 1.1;
  }

  h1 span { display: inline; }

  .play-title { font-size: 40px; }

  .intro-copy {
    margin: 14px 0 0;
    font-size: 14px;
  }

  .intro-copy br { display: none; }

  .mode-picker { margin-top: 22px; }

  .view:not(.game-layout) { padding: 36px 0 48px; }

  .chat-grid { grid-template-columns: minmax(0, 1fr); }
  .chat-card { min-height: 380px; }
  .chat-head { flex-wrap: wrap; }
  .chat-actions { justify-content: flex-start; }

  .card { padding: 18px; }

  .game-board {
    gap: 8px;
    padding: 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after { transition: none !important; }
}

.link-btn {
  padding: 4px 6px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}
