:root{
  --bg:#121213;
  --panel:#1a1a1c;
  --text:#e7e7e7;
  --muted:#a9a9b2;
  --tile:#3a3a3c;
  --tile-border:#565758;
  --green:#538d4e;
  --yellow:#b59f3b;
  --gray:#3a3a3c;
  --key:#818384;
  --keyText:#111;
  --danger:#e05d5d;
  --safeAreaTop: env(safe-area-inset-top, 0px);
  --safeAreaBottom: env(safe-area-inset-bottom, 0px);
}

*{ box-sizing:border-box; }
html,body{ height:100%; }
body{
  margin:0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  background:var(--bg);
  color:var(--text);
}

.topbar{
  position:sticky;
  top:0;
  z-index:10;
  padding: calc(10px + var(--safeAreaTop)) 14px 10px;
  background:linear-gradient(to bottom, rgba(18,18,19,0.98), rgba(18,18,19,0.88));
  backdrop-filter: blur(10px);
  border-bottom:1px solid rgba(255,255,255,0.08);
  display:flex;
  align-items:center;
  justify-content:space-between;
}
.title{ margin:0; font-size:22px; letter-spacing:0.4px; }
.subtitle{ font-size:12px; color:var(--muted); margin-top:2px; }
.title-wrap{ display:flex; flex-direction:column; gap:2px; }
.actions{ display:flex; gap:8px; }

.icon-btn{
  appearance:none;
  border:1px solid rgba(255,255,255,0.15);
  background:rgba(255,255,255,0.06);
  color:var(--text);
  height:36px;
  min-width:36px;
  border-radius:10px;
  font-weight:700;
  font-size:16px;
}
.icon-btn:active{ transform: translateY(1px); }

.wrap{
  max-width:560px;
  margin:0 auto;
  padding: 14px 14px calc(14px + var(--safeAreaBottom));
  display:flex;
  flex-direction:column;
  gap:14px;
}

.toast{
  min-height:22px;
  font-size:14px;
  color:var(--muted);
}
.toast.show{ color:var(--text); }
.toast.danger{ color:var(--danger); }

.board{ display:flex; justify-content:center; }
.grid{
  display:grid;
  gap:8px;
  width:min(92vw, 420px);
  touch-action: manipulation;
  --tile-col: 1fr;
  --tile-size: 60px;
}
.grid.compact{
  width: max-content;
  margin: 0 auto;
  --tile-col: var(--tile-size);
}

.row{ display:grid; gap:8px; }
.tile{
  width:100%;
  aspect-ratio: 1 / 1;
  max-width: 60px;
  max-height: 60px;
  border-radius:10px;
  border:2px solid var(--tile-border);
  background:transparent;
  display:flex;
  align-items:center;
  justify-content:center;
  font-weight:800;
  text-transform:uppercase;
  letter-spacing:0.8px;
  user-select:none;
}
.tile.filled{ border-color:#7a7a7c; }
.tile.reveal{
  border-color: transparent;
  color:#fff;
}
.tile.green{ background:var(--green); }
.tile.yellow{ background:var(--yellow); }
.tile.gray{ background:var(--gray); }

.keyboard{ width:100%; }

/* Hint button row (between grid and keyboard) */
.hint-row{
  display:flex;
  justify-content:center;
  margin: 4px 0 2px;
}
.hint-button{
  appearance:none;
  border:1px solid rgba(255,255,255,0.15);
  background:rgba(255,255,255,0.06);
  color:var(--text);
  height:40px;
  padding: 0 14px;
  border-radius:12px;
  font-weight:800;
  letter-spacing:0.2px;
  display:inline-flex;
  align-items:center;
  justify-content:center;
  gap:8px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.hint-button.used{
  background: var(--green);
  border-color: rgba(0,0,0,0);
  color: #fff;
}
.hint-button:active{ transform: translateY(1px); }
.hint-icon{ font-size: 18px; line-height: 1; }
.hint-text{ font-size: 14px; }

.kb{ display:flex; flex-direction:column; gap:8px; }
.kb-row{ display:flex; gap:6px; justify-content:center; }
.key{
  appearance:none;
  border:none;
  background:var(--key);
  color:var(--keyText);
  border-radius:12px;
  height:52px;
  padding:0 10px;
  font-weight:800;
  text-transform:uppercase;
  flex: 1 1 0;
  max-width:44px;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.key.wide{ max-width:90px; flex: 1.5 1 0; }
.key:active{ transform: translateY(1px); }

.key.green{ background:var(--green); color:#fff; }
.key.yellow{ background:var(--yellow); color:#fff; }
.key.gray{ background:var(--gray); color:#fff; }

.modal {
  border: none;
  background: transparent;
  padding: 0;
  margin: auto;
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
}
.modal::backdrop{ background: rgba(0,0,0,0.55); }
.modal-card{
  border:1px solid rgba(255,255,255,0.12);
  border-radius:16px;
  padding:14px;
  background:var(--panel);
  color:var(--text);
  width:min(92vw, 520px);
}
.modal-head{ display:flex; justify-content:space-between; align-items:center; gap:10px; }
.modal-head h2{ margin:0; font-size:18px; }
.modal-foot{ display:flex; justify-content:flex-end; margin-top:10px; }
.btn{
  border:none;
  background:#2d2d31;
  color:var(--text);
  padding:10px 14px;
  border-radius:12px;
  font-weight:700;
}
.muted{ color:var(--muted); }
.chip{ display:inline-block; width:14px; height:14px; border-radius:4px; vertical-align:middle; margin-right:6px; }
.chip.green{ background:var(--green); }
.chip.yellow{ background:var(--yellow); }
.chip.gray{ background:var(--gray); }

/* Stats modal */
#stats .modal-card {
  width: min(88vw, 360px);
}
#hint .modal-card {
  width: min(88vw, 360px);
}
#help .modal-card {
  width: min(88vw, 360px);
}
.stats-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(64px, 1fr));
  gap: 8px;
  margin: 16px 0;
}
.stat-item {
  text-align: center;
}
.stat-value {
  font-size: 1.5rem;
  font-weight: 800;
  color: var(--text);
}
.stat-label {
  font-size: 0.65rem;
  color: var(--muted);
  text-transform: uppercase;
  line-height: 1.2;
}
.dist-title {
  font-size: 0.9rem;
  text-transform: uppercase;
  color: var(--muted);
  margin: 16px 0 10px 0;
}
.distribution {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.dist-row {
  display: flex;
  align-items: center;
  gap: 6px;
}
.dist-label {
  width: 14px;
  text-align: center;
  font-weight: 700;
  color: var(--muted);
}
.dist-bar {
  min-width: 20px;
  padding: 2px 8px;
  background: var(--gray);
  color: #fff;
  font-weight: 700;
  font-size: 0.85rem;
  text-align: right;
  border-radius: 4px;
}
.dist-bar.highlight {
  background: var(--green);
}

@media (max-width:380px), (max-height: 700px){
  .wrap { padding: 8px 8px calc(8px + var(--safeAreaBottom)); gap: 8px; }
  .grid { gap:4px; width: min(85vw, 340px); }
  .row { gap:4px; }
  .key{ height:44px; border-radius:8px; max-width:34px; font-size: 13px; }
  .key.wide{ max-width:70px; }
  .topbar { padding: 4px 10px; min-height: 48px; }
  .title { font-size: 18px; }
  .subtitle { font-size: 11px; }
  .icon-btn { height: 32px; min-width: 32px; font-size: 14px; }
  .tile { border-width: 1.5px; font-size: 24px; max-width: 52px; max-height: 52px; }
  .grid { --tile-size: 52px; }
}

@media (max-height: 600px) {
  /* Ultra short screens */
  .grid { width: min(70vw, 280px); }
  .key { height: 36px; }
  .tile { max-width: 44px; max-height: 44px; font-size: 20px; }
  .grid { --tile-size: 44px; }
}
