:root {
  --bg: #16181d;
  --panel: #1f232b;
  --panel-2: #2a2f39;
  --text: #e8e6e1;
  --muted: #9a9ca3;
  --go: #30a46c;
  --star: #f5d90a;
  --warn: #e5484d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

.screen {
  max-width: 520px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 16px max(20px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  min-height: 100vh;
  min-height: 100dvh;
}

h1, h2, h3, p { margin: 0; }
.sub, .count, .fine, .help, .best { color: var(--muted); }
.sub { font-size: 0.9rem; }
.fine { font-size: 0.88rem; }
.count { font-variant-numeric: tabular-nums; }

/* Buttons */
.btn {
  min-height: 48px;
  padding: 0 20px;
  border: 0;
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.btn:disabled { opacity: 0.35; cursor: default; }
.btn--big { min-width: 220px; min-height: 56px; font-size: 1.15rem; font-weight: 600; background: var(--go); color: #0c1a12; }
.btn--go { background: var(--go); color: #0c1a12; font-weight: 600; }
.btn--warn { background: transparent; border: 1px solid var(--warn); color: var(--warn); }
button:focus-visible, a:focus-visible, input:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.icon-btn {
  width: 44px; height: 44px;
  border: 0; border-radius: 10px;
  background: transparent; color: var(--text);
  font-size: 1.6rem; line-height: 1;
  cursor: pointer;
}
.icon-btn:hover { background: var(--panel); }

.bar {
  width: 100%;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  text-align: center;
}
.bar h1 { font-size: 1.2rem; }

/* Home */
.home { justify-content: center; gap: 16px; }
.logo { width: 96px; height: 96px; border-radius: 22px; }
.home h1 { font-size: 2.2rem; letter-spacing: 0.04em; }
.tagline { color: var(--muted); margin-bottom: 12px; }
.site { color: var(--muted); font-size: 0.9rem; margin-top: 18px; text-decoration: none; }
.site:hover { color: var(--text); }

/* Levels */
.levels {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
}
.levels .size { grid-column: 1 / -1; margin: 10px 0 0; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.lvl {
  aspect-ratio: 1;
  border: 0; border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font: inherit; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer;
}
.lvl .mark { font-size: 0.95rem; line-height: 1; min-height: 1em; color: var(--go); }
.lvl.is-star .mark { color: var(--star); }
.lvl.is-at { box-shadow: inset 0 0 0 2px var(--text); }
.lvl:disabled { opacity: 0.3; cursor: default; }

/* Play */
.bar-mid { display: flex; flex-direction: column; align-items: center; }
#board {
  /* As wide as fits, but never taller than about 60% of the screen. */
  width: min(100%, calc(60vh * var(--w, 1) / var(--h, 1)));
  width: min(100%, calc(60dvh * var(--w, 1) / var(--h, 1)));
  aspect-ratio: var(--ratio, 1);
  background: var(--panel);
  border-radius: 10px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: pointer;
}
.status { min-height: 1.4em; font-variant-numeric: tabular-nums; text-align: center; }
.best { font-size: 0.88rem; min-height: 1.2em; }
.controls { display: flex; gap: 10px; }
.help { font-size: 0.9rem; text-align: center; max-width: 34ch; }

.result {
  position: fixed;
  left: 50%; bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  width: min(calc(100% - 32px), 440px);
  padding: 18px 16px;
  border-radius: 16px;
  background: var(--panel-2);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  text-align: center;
  animation: rise 0.25s ease-out;
}
.result-mark { font-size: 2.6rem; line-height: 1; color: var(--go); }
.result-mark.is-star { color: var(--star); animation: pop 0.5s ease-out; }
.result p { color: var(--muted); }
@keyframes rise { from { transform: translate(-50%, 20px); opacity: 0; } }
@keyframes pop { 0% { transform: scale(0.3); } 70% { transform: scale(1.25); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .result, .result-mark.is-star { animation: none; } }

/* Settings */
.settings { width: 100%; display: flex; flex-direction: column; gap: 22px; padding-top: 6px; }
.set { display: flex; flex-direction: column; gap: 8px; }
.set .label { font-weight: 600; }
.set .row { justify-content: flex-start; }
.segs { display: flex; background: var(--panel); border-radius: 12px; padding: 4px; gap: 4px; }
.seg {
  flex: 1; min-height: 40px;
  border: 0; border-radius: 9px;
  background: transparent; color: var(--text);
  font: inherit; cursor: pointer;
}
.seg.is-on { background: var(--go); color: #0c1a12; font-weight: 600; }
#code-in {
  flex: 1; min-width: 0; min-height: 48px;
  padding: 0 12px;
  border: 1px solid var(--panel-2); border-radius: 12px;
  background: var(--bg); color: var(--text);
  font: inherit; font-size: 0.9rem;
}
