/* ── Reset & Base ─────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:         #0d1b2a;
  --bg2:        #1a2744;
  --bg3:        #243356;
  --gold:       #f5a623;
  --gold-dim:   #c8811a;
  --text:       #e8dcc8;
  --text-dim:   #8fa3b8;
  --text-muted: #5a7090;
  --border:     #2e4068;
  --green:      #4caf50;
  --yellow:     #f5a623;
  --gray:       #3a4a5c;
  --red:        #c0392b;
  --tile-size:  clamp(42px, 10vw, 64px);
  --gap:        5px;
  --radius:     6px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', sans-serif;
  font-size: 16px;
  display: flex;
  flex-direction: column;
}

/* ── Header ───────────────────────────────────── */
header {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.header-inner {
  max-width: 560px;
  margin: 0 auto;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.logo { display: flex; align-items: center; gap: 10px; }
.logo-img { height: 36px; width: auto; object-fit: contain; display: block; }
.logo-text {
  font-family: 'Cinzel', serif;
  font-size: 1.2rem; font-weight: 700;
  color: var(--gold); letter-spacing: 0.04em;
}
.header-actions { display: flex; gap: 6px; align-items: center; }
.btn-icon {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  width: 36px; height: 36px;
  border-radius: 50%;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  display: flex; align-items: center; justify-content: center;
}
.btn-icon:hover { background: var(--gold); color: var(--bg); }

/* Auth pill button — wider than icon buttons */
.btn-auth-pill {
  width: auto;
  border-radius: 20px;
  padding: 0 12px;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Main ─────────────────────────────────────── */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 14px 12px 8px;
  gap: 12px;
  max-width: 520px;
  margin: 0 auto;
  width: 100%;
}

/* ── Message bar ──────────────────────────────── */
.message-bar {
  background: var(--gold);
  color: var(--bg);
  font-family: 'Cinzel', serif;
  font-weight: 600;
  font-size: 0.9rem;
  padding: 8px 20px;
  border-radius: var(--radius);
  text-align: center;
  letter-spacing: 0.03em;
}
.message-bar.error { background: var(--red); color: #fff; }
.message-bar.hidden { display: none; }

/* ── Board ────────────────────────────────────── */
#board-container { display: flex; justify-content: center; align-items: center; width: 100%; }
#board { display: grid; gap: var(--gap); }
.tile {
  width: var(--tile-size); height: var(--tile-size);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  font-family: 'Cinzel', serif;
  font-size: clamp(1rem, 4vw, 1.6rem);
  font-weight: 700;
  color: var(--text);
  background: var(--bg2);
  text-transform: uppercase;
  user-select: none;
  transition: border-color 0.1s;
}
.tile.filled  { border-color: var(--text-dim); animation: popIn 0.1s ease; }
.tile.green   { background: var(--green);  border-color: var(--green);  color: #fff; }
.tile.yellow  { background: var(--yellow); border-color: var(--yellow); color: var(--bg); }
.tile.gray    { background: var(--gray);   border-color: var(--gray);   color: var(--text); }
.tile.flip    { animation: flipTile 0.6s ease forwards; }
.tile.shake   { animation: shakeTile 0.4s ease; }
.tile.bounce  { animation: bounceTile 0.5s ease; }

/* ── Keyboard ─────────────────────────────────── */
#keyboard { display: flex; flex-direction: column; gap: 6px; width: 100%; max-width: 500px; }
.kb-row   { display: flex; justify-content: center; gap: 5px; }
.key {
  height: 54px; min-width: 34px; flex: 1; max-width: 44px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  font-family: 'Inter', sans-serif; font-size: 0.8rem; font-weight: 600;
  cursor: pointer; text-transform: uppercase;
  user-select: none; display: flex; align-items: center; justify-content: center;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.key.wide   { max-width: 66px; font-size: 0.75rem; }
.key.green  { background: var(--green);  border-color: var(--green);  color: #fff; }
.key.yellow { background: var(--yellow); border-color: var(--yellow); color: var(--bg); }
.key.gray   { background: var(--gray);   border-color: var(--gray);   color: var(--text-dim); }

/* ── Modals ───────────────────────────────────── */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.72);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; padding: 16px;
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px;
  max-width: 420px; width: 100%;
  position: relative;
  max-height: 90vh; overflow-y: auto;
}
.modal-box h2 {
  font-family: 'Cinzel', serif;
  font-size: 1.3rem; font-weight: 700; color: var(--gold);
  margin-bottom: 14px; text-align: center;
}
.modal-box h3 {
  font-family: 'Cinzel', serif;
  font-size: 1rem; color: var(--gold);
  margin: 16px 0 10px; text-align: center;
}
.modal-box p   { font-size: 0.9rem; color: var(--text-dim); line-height: 1.6; margin-bottom: 10px; }
.modal-box ul  { padding-left: 18px; margin-bottom: 14px; }
.modal-box li  { font-size: 0.88rem; color: var(--text-dim); line-height: 1.7; }
.modal-box strong { color: var(--text); }
.modal-close {
  position: absolute; top: 12px; right: 14px;
  background: none; border: none;
  color: var(--text-dim); font-size: 1.1rem; cursor: pointer;
}
.modal-close:hover { color: var(--gold); }

/* example tiles */
.example-row { display: flex; gap: 5px; margin: 10px 0 4px; }
.ex-tile {
  width: 42px; height: 42px; border: 2px solid var(--border); border-radius: 4px;
  display: flex; align-items: center; justify-content: center;
  font-family: 'Cinzel', serif; font-size: 1.1rem; font-weight: 700;
  background: var(--bg2); color: var(--text); text-transform: uppercase;
}
.ex-tile.green  { background: var(--green);  border-color: var(--green);  color: #fff; }
.ex-tile.yellow { background: var(--yellow); border-color: var(--yellow); color: var(--bg); }
.ex-tile.gray   { background: var(--gray);   border-color: var(--gray);   color: var(--text); }
.ex-label { font-size: 0.85rem; color: var(--text-dim); margin-bottom: 12px; }

/* XP table in how-to-play */
.xp-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 4px; }
.xp-table th { color: var(--gold); font-family: 'Cinzel', serif; padding: 4px 8px; border-bottom: 1px solid var(--border); }
.xp-table td { text-align: center; padding: 4px 8px; color: var(--text-dim); }
.xp-table tr:first-child td { color: var(--gold); font-weight: 700; }

/* stats */
.stats-grid { display: grid; grid-template-columns: repeat(4,1fr); gap: 8px; margin-bottom: 18px; }
.stat-box   { display: flex; flex-direction: column; align-items: center; background: var(--bg3); border-radius: 8px; padding: 10px 4px; }
.stat-box span  { font-family: 'Cinzel', serif; font-size: 1.6rem; font-weight: 700; color: var(--gold); }
.stat-box label { font-size: 0.7rem; color: var(--text-dim); margin-top: 2px; }

/* distribution */
.dist-row { display: flex; align-items: center; gap: 8px; margin-bottom: 5px; font-size: 0.82rem; }
.dist-num { width: 14px; text-align: right; color: var(--text-dim); font-weight: 600; }
.dist-bar-wrap { flex: 1; background: var(--bg3); border-radius: 4px; height: 22px; }
.dist-bar {
  height: 100%; border-radius: 4px; background: var(--bg3);
  display: flex; align-items: center; padding: 0 8px;
  font-weight: 600; color: var(--text); font-size: 0.8rem;
  min-width: 28px; transition: width 0.6s ease;
}
.dist-bar.current { background: var(--gold); color: var(--bg); }

/* share */
.btn-share {
  display: block; width: 100%; margin-top: 16px;
  background: var(--gold); border: none; border-radius: 8px;
  color: var(--bg); font-family: 'Cinzel', serif;
  font-size: 0.95rem; font-weight: 700;
  padding: 12px; cursor: pointer; letter-spacing: 0.04em;
  transition: background 0.15s;
}
.btn-share:hover  { background: var(--gold-dim); }
.btn-share.hidden { display: none; }

/* ── Leaderboard ──────────────────────────────── */
.modal-lb-box { max-width: 480px; }

.lb-tabs {
  display: flex; gap: 0; margin-bottom: 18px;
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
}
.lb-tab {
  flex: 1; padding: 8px 0;
  background: var(--bg3); border: none;
  color: var(--text-dim); font-family: 'Inter', sans-serif;
  font-size: 0.85rem; font-weight: 600; cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.lb-tab.active { background: var(--gold); color: var(--bg); }
.lb-tab:first-child { border-radius: 7px 0 0 7px; }
.lb-tab:last-child  { border-radius: 0 7px 7px 0; }

.lb-status { text-align: center; color: var(--text-dim); font-size: 0.9rem; padding: 20px 0; }
.lb-status.hidden { display: none; }
.lb-err    { color: #e57373; }

.lb-table {
  width: 100%; border-collapse: collapse; font-size: 0.85rem;
}
.lb-table thead th {
  text-align: left; padding: 6px 8px;
  color: var(--gold); font-family: 'Cinzel', serif;
  border-bottom: 1px solid var(--border); font-size: 0.8rem;
}
.lb-table tbody td {
  padding: 8px 8px; color: var(--text-dim);
  border-bottom: 1px solid rgba(46,64,104,0.4);
}
.lb-table tbody tr:last-child td { border-bottom: none; }
.lb-table tbody tr:hover td { background: rgba(36,51,86,0.6); }
.lb-table tbody tr.lb-me td {
  color: var(--gold);
  background: rgba(245,166,35,0.08);
}
.lb-rank { font-size: 1rem; width: 36px; text-align: center; }
.lb-xp   { font-weight: 700; color: var(--gold) !important; }
.lb-hint { text-align: center; margin-top: 14px; font-size: 0.85rem; color: var(--text-dim); }
.lb-hint.hidden { display: none; }
.lb-hint a { color: var(--gold); text-decoration: none; }
.lb-hint a:hover { text-decoration: underline; }

/* ── Auth ─────────────────────────────────────── */
.auth-tabs {
  display: flex; gap: 0; margin-bottom: 20px;
  border: 1px solid var(--border); border-radius: 8px; overflow: hidden;
}
.auth-tab {
  flex: 1; padding: 8px 0;
  background: var(--bg3); border: none;
  color: var(--text-dim); font-family: 'Inter', sans-serif;
  font-size: 0.85rem; font-weight: 600; cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.auth-tab.active { background: var(--gold); color: var(--bg); }
.auth-tab:first-child { border-radius: 7px 0 0 7px; }
.auth-tab:last-child  { border-radius: 0 7px 7px 0; }

.auth-form { display: flex; flex-direction: column; gap: 0; }
.auth-form.hidden { display: none; }
.auth-form h2 { margin-bottom: 18px; }

.field-group { display: flex; flex-direction: column; gap: 10px; margin-bottom: 14px; }
.field-group input {
  width: 100%; padding: 10px 14px;
  background: var(--bg3); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  font-family: 'Inter', sans-serif; font-size: 0.9rem;
  outline: none; transition: border-color 0.15s;
}
.field-group input:focus { border-color: var(--gold); }
.field-group input::placeholder { color: var(--text-muted); }

.auth-error {
  background: rgba(192,57,43,0.15);
  border: 1px solid var(--red);
  border-radius: var(--radius);
  color: #e57373; font-size: 0.85rem;
  padding: 8px 12px; margin-bottom: 12px;
  text-align: center;
}
.auth-error.hidden { display: none; }

.btn-primary {
  width: 100%; padding: 12px;
  background: var(--gold); border: none; border-radius: 8px;
  color: var(--bg); font-family: 'Cinzel', serif;
  font-size: 0.95rem; font-weight: 700;
  cursor: pointer; letter-spacing: 0.04em;
  transition: background 0.15s;
}
.btn-primary:hover   { background: var(--gold-dim); }
.btn-primary.btn-danger { background: var(--red); }
.btn-primary.btn-danger:hover { background: #a93226; }

/* ── Footer ───────────────────────────────────── */
footer {
  text-align: center; padding: 10px;
  font-size: 0.75rem; color: var(--text-dim);
  border-top: 1px solid var(--border);
}

/* ── Animations ───────────────────────────────── */
@keyframes popIn {
  0%   { transform: scale(0.92); }
  60%  { transform: scale(1.06); }
  100% { transform: scale(1); }
}
@keyframes flipTile {
  0%   { transform: rotateX(0deg); }
  50%  { transform: rotateX(-90deg); }
  100% { transform: rotateX(0deg); }
}
@keyframes shakeTile {
  0%,100% { transform: translateX(0); }
  20%     { transform: translateX(-7px); }
  40%     { transform: translateX(7px); }
  60%     { transform: translateX(-4px); }
  80%     { transform: translateX(4px); }
}
@keyframes bounceTile {
  0%,100% { transform: translateY(0); }
  30%     { transform: translateY(-18px); }
  60%     { transform: translateY(-7px); }
  80%     { transform: translateY(-12px); }
}

/* ── Responsive ───────────────────────────────── */
@media (max-width: 360px) {
  :root { --tile-size: 40px; }
  .key  { height: 48px; }
  .btn-auth-pill { max-width: 100px; font-size: 0.72rem; }
}
