:root {
  --bg: #f6eefc;
  --bg-2: #efe2fb;
  --panel: #ffffff;
  --panel-2: #f7f0fe;
  --line: #e7d6f7;
  --text: #4b3a63;
  --muted: #a692c0;
  --correct: #86cf8e;   /* soft green */
  --present: #f0c36a;   /* warm amber */
  --absent: #cdbfe0;    /* muted lilac */
  --primary: #a976e6;   /* lilac purple */
  --primary-dark: #8e58d6;
  --key: #d9c6f2;       /* lilac keycap */
  --key-text: #5a4578;
  --mini-empty: #c3a9e6;
  --me-bg: #fbf5ff;
  --radius: 12px;
  --shadow: 0 6px 20px rgba(150, 110, 200, 0.18);
}

/* ---- Dark lilac theme ---- */
:root[data-theme="dark"] {
  --bg: #1c1626;
  --bg-2: #241a33;
  --panel: #271d38;
  --panel-2: #2f2342;
  --line: #3d2e55;
  --text: #ece3fb;
  --muted: #a28fc0;
  --correct: #5ea368;
  --present: #c79a42;
  --absent: #4a3c63;
  --primary: #b98aef;
  --primary-dark: #9a63e0;
  --key: #463660;
  --key-text: #ece3fb;
  --mini-empty: #352748;
  --me-bg: #342748;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

/* Theme toggle button (fixed top-right on every screen) */
.theme-toggle {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 50;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 2px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform .12s ease, filter .12s ease;
}
.theme-toggle:hover { transform: scale(1.08); }

body {
  margin: 0;
  background: linear-gradient(160deg, var(--bg), var(--bg-2));
  background-attachment: fixed;
  color: var(--text);
  font-family: "Nunito", "Quicksand", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}

.screen { display: none; }
.screen.active { display: flex; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }
.error { color: #d6689a; min-height: 1.2em; margin: .5rem 0 0; }
.hint { color: var(--muted); font-size: .85rem; }

/* ---------------- Landing / Waiting cards ---------------- */
#screen-landing, #screen-waiting {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}
.card {
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 22px;
  padding: 2rem;
  width: 100%;
  max-width: 460px;
  box-shadow: var(--shadow);
}
.logo {
  font-size: 2.5rem;
  letter-spacing: .08em;
  margin: 0 0 .25rem;
  font-weight: 800;
  color: var(--primary-dark);
}
.logo span { color: var(--present); font-weight: 800; }
.tagline { color: var(--muted); margin-top: 0; }

.field { display: block; margin: 1rem 0; }
.field > span { display: block; font-size: .85rem; color: var(--muted); margin-bottom: .35rem; }
.field input, .share input {
  width: 100%;
  padding: .75rem .85rem;
  font-size: 1rem;
  border-radius: var(--radius);
  border: 2px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  outline: none;
}
.field input:focus, .share input:focus { border-color: var(--primary); }
#code-input { text-transform: uppercase; letter-spacing: .2em; }

.btn-row { display: flex; gap: .6rem; margin-top: 1rem; }
.btn {
  flex: 1;
  padding: .75rem 1rem;
  border-radius: 999px;
  border: 2px solid var(--line);
  background: var(--panel-2);
  color: var(--primary-dark);
  font-size: .95rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform .1s ease, filter .12s ease;
}
.btn:hover { filter: brightness(1.03); transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn.primary {
  background: linear-gradient(180deg, var(--primary), var(--primary-dark));
  border-color: var(--primary-dark);
  color: #fff;
  box-shadow: 0 4px 12px rgba(142, 88, 214, 0.35);
}

/* ---------------- Waiting room ---------------- */
.room-code { font-size: 1.1rem; margin: .5rem 0; }
.room-code strong { letter-spacing: .2em; color: var(--primary-dark); }
.share { display: flex; gap: .5rem; margin: .5rem 0; }
.share input { flex: 1; font-size: .85rem; }
.share .btn { flex: 0 0 auto; }

.waiting-list { list-style: none; padding: 0; margin: .5rem 0 1.2rem; }
.waiting-list li {
  padding: .7rem .9rem;
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  margin-bottom: .5rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.tag { font-size: .7rem; padding: .18rem .55rem; border-radius: 999px; background: var(--primary); color: #fff; font-weight: 700; }
.tag.off { background: var(--absent); color: #fff; }

.last-round {
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: .6rem .8rem;
  margin: .5rem 0;
  font-size: .9rem;
}
.last-round b { color: var(--primary-dark); letter-spacing: .12em; }

/* Scoreboard rows */
.sb-info { display: flex; flex-direction: column; }
.sb-sub { font-size: .72rem; color: var(--muted); }
.sb-right { display: flex; align-items: center; gap: .5rem; }
.sb-score {
  font-size: 1.1rem; font-weight: 800; color: var(--primary-dark);
  min-width: 2.2rem; text-align: right;
}
.sb-score small { font-size: .62rem; color: var(--muted); font-weight: 600; display: block; }

/* ---------------- Game layout ---------------- */
#screen-game { flex-direction: column; }
.game-layout {
  position: relative;
  display: flex;
  justify-content: center; /* board + keyboard centered in the window */
  padding: 1rem;
  width: 100%;
}

/* Left panel — narrow and pinned to the far left of the window */
.players-panel {
  position: fixed;
  left: 12px;
  top: 68px;
  width: 188px;
  max-height: calc(100vh - 88px);
  overflow-y: auto;
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 18px;
  padding: .85rem;
  box-shadow: var(--shadow);
}
.players-panel h3 { margin: 0 0 .75rem; color: var(--primary-dark); }
.player-card {
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: .55rem .65rem;
  margin-bottom: .55rem;
  display: flex;
  flex-direction: column;
  gap: .4rem;
}
.player-card.me { border-color: var(--primary); background: var(--me-bg); }
.player-card .pc-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: .3rem;
}
.player-card .pc-name { font-weight: 700; font-size: .9rem; }
.player-card .pc-rank {
  font-size: .68rem; background: var(--correct); color: #fff;
  padding: .12rem .45rem; border-radius: 999px; white-space: nowrap;
}
.player-card .pc-rank.done { background: var(--absent); }

/* stats row + tiny minimap side by side */
.pc-body { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.pc-stats {
  display: flex; flex-direction: column; gap: .2rem;
  font-size: .68rem; color: var(--muted);
}
.pc-stats .stat b { color: var(--text); }
.pc-stats .stat.g b { color: #4ba557; }
.pc-stats .stat.y b { color: #c89026; }

/* Minimap — intentionally very small */
.minimap { display: grid; grid-template-rows: repeat(6, 1fr); gap: 1px; width: 44px; flex: 0 0 44px; }
.minimap .mini-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 1px; }
.minimap .mini-cell { width: 100%; aspect-ratio: 1; border-radius: 1px; background: var(--mini-empty); }
.mini-cell.correct { background: var(--correct); }
.mini-cell.present { background: var(--present); }
.mini-cell.absent { background: var(--absent); }

/* Center board */
.board-area {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.board-head {
  width: 100%;
  max-width: 350px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .5rem;
  margin-bottom: .5rem;
  font-size: .9rem;
  font-weight: 700;
  color: var(--primary-dark);
}
.game-status { color: var(--muted); font-weight: 700; }
.timer-group { display: flex; align-items: center; gap: .4rem; }
.timer {
  font-variant-numeric: tabular-nums;
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--primary-dark);
  background: var(--panel-2);
  border: 2px solid var(--line);
  border-radius: 999px;
  padding: .12rem .6rem;
}
.timer.low { color: #d6689a; border-color: #d6689a; }
.timer.paused { color: var(--muted); }
.btn.tiny { flex: 0 0 auto; padding: .3rem .7rem; font-size: .75rem; }
.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 6px;
  margin-bottom: .5rem;
}
.board .row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.tile {
  width: 58px;
  height: 58px;
  border: 2px solid var(--line);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  font-weight: 800;
  text-transform: uppercase;
  color: var(--text);
  background: var(--panel);
}
.tile.filled { border-color: var(--primary); }
.tile.correct { background: var(--correct); border-color: var(--correct); color: #fff; }
.tile.present { background: var(--present); border-color: var(--present); color: #fff; }
.tile.absent { background: var(--absent); border-color: var(--absent); color: #fff; }

.message { min-height: 1.5rem; margin: .25rem 0; font-size: .95rem; font-weight: 700; color: var(--primary-dark); }

/* Keyboard */
.keyboard { display: flex; flex-direction: column; gap: 6px; width: 100%; max-width: 480px; }
.kb-row { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1;
  min-width: 0;
  height: 52px;
  border-radius: 8px;
  border: none;
  background: var(--key);
  color: var(--key-text);
  font-weight: 800;
  font-size: .95rem;
  cursor: pointer;
  text-transform: uppercase;
  transition: transform .06s ease, filter .06s ease, background .2s ease;
}
.key.wide { flex: 1.5; font-size: .72rem; }
.key:hover { filter: brightness(1.05); }
/* pressed feedback — darker, works for physical + on-screen presses */
.key.active { transform: scale(0.9); filter: brightness(0.72); }
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent { background: var(--absent); color: #fff; }

/* Overlay */
.overlay {
  position: fixed; inset: 0;
  z-index: 200;
  background: rgba(90, 60, 130, .45);
  backdrop-filter: blur(3px);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
  animation: reveal-fade .18s ease-out;
}
@keyframes reveal-fade { from { opacity: 0; } to { opacity: 1; } }
.reveal-label { margin: .4rem 0 0; color: var(--muted); font-size: .9rem; text-transform: uppercase; letter-spacing: .08em; }
.reveal-word {
  font-size: 2.6rem; font-weight: 800;
  letter-spacing: .3em; text-transform: uppercase;
  color: var(--primary-dark);
  margin: .15rem 0 .4rem .3em; /* .3em right-pads the trailing letter-spacing */
}
.reveal-sub { margin: .2rem 0 1.3rem; font-size: 1rem; }
.overlay-card {
  background: var(--panel);
  border: 2px solid var(--line);
  border-radius: 22px;
  padding: 2rem;
  width: 100%;
  max-width: 420px;
  text-align: center;
  box-shadow: var(--shadow);
}
.overlay-card h2 { color: var(--primary-dark); margin-top: 0; }
.overlay-word { font-size: 1.4rem; letter-spacing: .25em; text-transform: uppercase; color: #4ba557; font-weight: 800; }
.results-list { text-align: left; padding-left: 1.2rem; }
.results-list li { margin: .35rem 0; }

/* Key-sound picker (fixed bottom-center pill) */
.ksound {
  position: fixed; left: 50%; bottom: 14px; transform: translateX(-50%);
  z-index: 60;
  display: flex; align-items: center; gap: .5rem;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: .4rem .55rem .4rem .8rem;
  box-shadow: var(--shadow); font-size: .85rem; color: var(--text);
  max-width: calc(100vw - 20px);
}
.ksound-ic { font-size: 1rem; line-height: 1; }
.ksound-label { color: var(--muted); white-space: nowrap; }
.ksound-select {
  border: 1px solid var(--line); background: var(--bg); color: var(--text);
  border-radius: 999px; padding: .25rem .6rem; font: inherit; cursor: pointer;
}
.ksound-select:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
/* keep the on-screen keyboard from hiding behind the pill */
.board-area { padding-bottom: 64px; }

@media (max-width: 760px) {
  .ksound { font-size: .8rem; bottom: 10px; padding: .35rem .5rem .35rem .7rem; }
  .game-layout { flex-direction: column; align-items: stretch; }
  .players-panel {
    position: static;
    left: auto; top: auto;
    width: auto;
    max-height: none;
    margin-bottom: 1rem;
  }
  .tile { width: 46px; height: 46px; font-size: 1.4rem; }
}
