:root {
  --bg-1: #2b1055;
  --bg-2: #7597de;
  --card: #ffffff;
  --text: #22163d;
  --muted: #7a6f93;
  --line: #ece8f4;
  --primary: #ff5c8a;
  --primary-dark: #e23d6e;
  --secondary: #5b4bdb;
  --secondary-dark: #4535c4;
  --soft: #f5f2fb;
  --radius: 18px;
  --shadow: 0 10px 0 rgba(0, 0, 0, 0.12), 0 20px 40px rgba(20, 0, 60, 0.25);
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }
html { background: var(--bg-1); }

body {
  font-family: 'Fredoka', system-ui, sans-serif;
  color: var(--text);
  background: linear-gradient(160deg, var(--bg-1), #5d2a8f 45%, var(--bg-2));
  background-attachment: fixed;
  min-height: 100vh;
}

[hidden] { display: none !important; }

h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 2rem; }
h2 { font-size: 1.15rem; margin-bottom: 12px; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); font-weight: 400; }

.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 24px;
}

.center-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}

.logo { font-size: 3rem; line-height: 1; }

/* ---------- Inputs & buttons ---------- */

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

input:not([type=range]), select {
  width: 100%;
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: var(--soft);
  font-size: 1.05rem;
  outline: none;
  transition: border-color .15s, background .15s;
}
input:not([type=range]):focus, select:focus { border-color: var(--secondary); background: #fff; }

.btn {
  border: 0;
  border-radius: 12px;
  padding: 12px 20px;
  font-weight: 600;
  font-size: 1.05rem;
  cursor: pointer;
  transition: transform .08s, box-shadow .08s, background .15s;
  white-space: nowrap;
}
.btn:active { transform: translateY(3px); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 4px 0 var(--primary-dark); }
.btn-primary:hover { background: #ff7099; }
.btn-primary:active { box-shadow: 0 1px 0 var(--primary-dark); }
.btn-secondary { background: var(--secondary); color: #fff; box-shadow: 0 4px 0 var(--secondary-dark); }
.btn-secondary:hover { background: #6b5ce6; }
.btn-secondary:active { box-shadow: 0 1px 0 var(--secondary-dark); }
.btn-ghost { background: rgba(255,255,255,.15); color: #fff; }
.btn-ghost:hover { background: rgba(255,255,255,.25); }
.btn-small { padding: 7px 12px; font-size: .9rem; }
.btn-block { width: 100%; }
.btn-big { padding: 16px 36px; font-size: 1.25rem; }

/* ---------- Login ---------- */

.login-card {
  width: min(360px, 100%);
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}
.login-card p { margin: 0; }
.form-error { color: var(--primary-dark); font-weight: 500; }
.shake { animation: shake .35s; }
@keyframes shake {
  20%, 60% { transform: translateX(-8px); }
  40%, 80% { transform: translateX(8px); }
}

/* ---------- Home ---------- */

.home-card {
  width: min(520px, 100%);
  display: flex;
  flex-direction: column;
  gap: 20px;
  text-align: center;
}

.field { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.field > span { font-weight: 500; }
.name-row { display: flex; align-items: center; gap: 12px; }

.avatar {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: #fff;
  background: var(--secondary);
  text-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.avatar-lg { width: 52px; height: 52px; font-size: 1.4rem; background: var(--primary); }

.home-actions { display: flex; flex-direction: column; gap: 4px; }
.home-action { padding: 14px; border-radius: 14px; transition: background .2s; }
.home-action h2 { font-size: 1rem; margin-bottom: 10px; }
.home-action.highlight { background: #efeaff; }
.join-row { display: flex; gap: 10px; }
.join-row input {
  text-transform: uppercase;
  letter-spacing: .35em;
  text-align: center;
  font-weight: 600;
}
.or { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: .9rem; }
.or::before, .or::after { content: ''; flex: 1; height: 2px; background: var(--line); }

/* ---------- Lobby ---------- */

.lobby { max-width: 1040px; margin: 0 auto; padding: 16px 16px 40px; }

.lobby-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
  color: #fff;
  flex-wrap: wrap;
}
.brand { font-weight: 600; font-size: 1.2rem; margin-right: auto; }
.code-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 6px 6px 16px;
  background: rgba(255,255,255,.15);
  border-radius: 14px;
}
.code-chip .muted { color: rgba(255,255,255,.75); }
.code-chip strong { font-size: 1.4rem; letter-spacing: .2em; }
.code-chip .btn { background: #fff; color: var(--secondary); }

.lobby-grid {
  display: grid;
  grid-template-columns: minmax(240px, 320px) 1fr;
  gap: 18px;
  align-items: start;
}

.players { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.player {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--soft);
}
.player.new { animation: pop .25s ease-out; }
.player.is-me { outline: 2px solid #d9d1f5; }
.player.offline { opacity: .5; }
.player-name { flex: 1; min-width: 0; font-weight: 500; overflow-wrap: anywhere; }
.player-hint { padding: 10px; font-size: .9rem; text-align: center; }
@keyframes pop { from { transform: scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }

.tag {
  display: inline-block;
  font-size: .72rem;
  font-weight: 500;
  padding: 1px 7px;
  border-radius: 99px;
  background: #e4defa;
  color: var(--secondary);
  vertical-align: middle;
}
.tag-host { background: #fff1c2; color: #a26b00; }
.tag-off { background: #eee; color: #777; }
.tag-ai { background: #e3fafc; color: #0b7285; }
.player-add { display: flex; justify-content: center; padding-top: 4px; }
.btn-add-ai { background: var(--soft); color: var(--secondary); border: 2px dashed #c9bff3; }
.btn-add-ai:hover { background: #efeaff; }
.ai-note { margin: 10px 0 0; color: #0b7285; font-weight: 500; text-align: center; }
#start:disabled { opacity: .45; cursor: not-allowed; box-shadow: none; }

.player-actions { display: flex; gap: 4px; opacity: 0; transition: opacity .15s; }
.player:hover .player-actions, .player:focus-within .player-actions { opacity: 1; }
@media (hover: none) { .player-actions { opacity: 1; } }
.icon-btn {
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font-size: .9rem;
}
.icon-btn:hover { background: #ffe3eb; }

.games { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px; }
.game-option {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  padding: 14px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: #fff;
  cursor: pointer;
  text-align: left;
  transition: border-color .15s, transform .1s, background .15s;
}
.game-option:hover:not(:disabled) { border-color: #c9bff3; transform: translateY(-2px); }
.game-option.selected { border-color: var(--secondary); background: #f3f0ff; }
.games.readonly .game-option { cursor: default; }
.games.readonly .game-option:hover { transform: none; }
.game-option:disabled { opacity: .45; }
.game-emoji { font-size: 1.8rem; }
.game-name { font-weight: 600; }
.game-desc { font-size: .85rem; line-height: 1.3; }

.settings-title { margin: 22px 0 10px; }
.settings { display: flex; flex-direction: column; gap: 10px; }
.setting {
  display: grid;
  grid-template-columns: 150px 1fr;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--soft);
  border-radius: 12px;
}
.setting label { font-weight: 500; }
.setting select { padding: 8px 12px; background: #fff; }
.setting textarea {
  width: 100%;
  padding: 8px 12px;
  border: 2px solid var(--line);
  border-radius: 10px;
  font: inherit;
  resize: vertical;
  outline: none;
}
.setting textarea:focus { border-color: var(--secondary); }
.setting input.toggle { width: 22px; height: 22px; accent-color: var(--secondary); justify-self: start; }
.setting-value { font-weight: 600; color: var(--secondary); }
.setting.locked { opacity: .5; }
.setting.locked input { cursor: not-allowed; }
.range-wrap { display: flex; align-items: center; gap: 12px; }
.range-wrap input { flex: 1; accent-color: var(--secondary); }
.range-wrap .setting-value { min-width: 2.5em; text-align: right; }

.start-row { margin-top: 24px; display: flex; flex-direction: column; align-items: center; }
.waiting { margin: 8px 0; animation: pulse 1.6s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .5; } }

@media (max-width: 720px) {
  .lobby-grid { grid-template-columns: 1fr; }
  .code-chip { order: 3; flex-basis: 100%; }
  .code-chip .btn { margin-left: auto; }
  .setting { grid-template-columns: 1fr; gap: 6px; }
}

/* ---------- Add AI player dialog ---------- */

.ai-dialog {
  width: min(400px, calc(100% - 32px));
  padding: 22px;
  border: 0;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  font-family: inherit;
}
.ai-dialog::backdrop { background: rgba(34, 22, 61, .55); }
.ai-dialog form { display: flex; flex-direction: column; gap: 12px; }
.ai-dialog h2, .ai-dialog p { margin: 0; }
.ai-dialog-buttons { display: flex; justify-content: flex-end; gap: 8px; }
.btn-ghost-dark { background: var(--soft); color: var(--muted); }
.btn-ghost-dark:hover { background: var(--line); }

/* ---------- Word list viewer ---------- */

.inspect-wrap { display: flex; align-items: center; gap: 8px; min-width: 0; }
.inspect-wrap select { flex: 1; min-width: 0; }
.inspect-wrap .setting-value { flex: 1; }
.btn-inspect { background: #fff; color: var(--secondary); border: 2px solid var(--line); }
.btn-inspect:hover { border-color: var(--secondary); }

.list-dialog {
  width: min(560px, calc(100% - 32px));
  max-height: min(80vh, 720px);
  padding: 20px;
  border: 0;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  color: var(--text);
  font-family: inherit;
}
.list-dialog[open] { display: flex; flex-direction: column; gap: 10px; }
.list-dialog::backdrop { background: rgba(34, 22, 61, .55); }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.list-head h2 { margin: 0; }
.list-head .icon-btn { background: var(--soft); }
.list-count { margin: 0; font-size: .85rem; }
.list-items {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  min-height: 0;
  columns: 3 120px;
  column-gap: 16px;
}
.list-items li { padding: 3px 0; break-inside: avoid; font-weight: 500; }

/* ---------- Playing placeholder ---------- */

.playing-card {
  width: min(460px, 100%);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.playing-card p { margin: 0; }
.settings-dump {
  font-family: inherit;
  margin: 4px 0;
  padding: 12px 18px;
  background: var(--soft);
  border-radius: 12px;
  text-align: left;
}

/* ---------- Toast & connection ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  background: var(--text);
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,.3);
  animation: pop .2s ease-out;
  z-index: 10;
}

.conn-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 8px;
  text-align: center;
  background: #ffd43b;
  color: #5c4500;
  font-weight: 500;
  z-index: 10;
}
