:root {
  --bg: #050910;
  --ink: #f4f7fb;
  --muted: #93a4c3;
  --blue: #2f7bff;
  --blue-deep: #16356f;
  --gold: #ffd000;
  --gold-ink: #1a2744;
  --green: #3ddc6a;
  --red: #ff5d73;
  --cell: #0e1d3d;
  --line: rgba(126, 176, 230, 0.22);
  --safe-top: env(safe-area-inset-top);
  --safe-bottom: env(safe-area-inset-bottom);
  color-scheme: dark;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  max-height: 100dvh;
  overflow: hidden;
  background:
    radial-gradient(ellipse 90% 46% at 50% -8%, rgba(47, 123, 255, 0.38), transparent 58%),
    radial-gradient(ellipse 50% 32% at 100% 100%, rgba(255, 208, 0, 0.08), transparent 52%),
    var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; touch-action: manipulation; }

button:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 2px;
}

.app {
  width: min(100%, 520px);
  height: 100dvh;
  max-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding:
    max(8px, var(--safe-top))
    10px
    max(8px, var(--safe-bottom));
  overflow: hidden;
}

.header {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding-bottom: 8px;
}

.brand { margin: 0; line-height: 0; }

.logo {
  display: block;
  width: clamp(58px, 18vw, 84px);
  height: auto;
  aspect-ratio: 1;
  border-radius: 50%;
  object-fit: cover;
  filter: drop-shadow(0 8px 18px rgba(0, 0, 0, 0.5));
}

.toolbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.levels {
  display: flex;
  gap: 4px;
  flex: 1;
  min-width: 0;
  padding: 3px;
  border-radius: 999px;
  background: rgba(8, 18, 40, 0.72);
  border: 1px solid rgba(126, 176, 230, 0.18);
}

.level {
  flex: 1;
  min-width: 0;
  border: 0;
  border-radius: 999px;
  padding: 7px 4px;
  background: transparent;
  color: var(--muted);
  font-size: clamp(0.68rem, 2.7vw, 0.8rem);
  font-weight: 800;
  cursor: pointer;
}

.level.is-active {
  background: linear-gradient(135deg, #1d4ed8, #3b82f6);
  color: #fff;
  box-shadow: 0 4px 14px rgba(47, 123, 255, 0.45);
}

.toolbar-side {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.clock {
  min-width: 4.4rem;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  letter-spacing: 0.03em;
  font-size: 0.92rem;
  color: #e8f1ff;
  background: rgba(8, 18, 40, 0.82);
  border: 1px solid rgba(126, 176, 230, 0.22);
  border-radius: 999px;
  padding: 7px 8px;
}

.btn-new {
  border: 0;
  border-radius: 999px;
  padding: 8px 12px;
  background: linear-gradient(180deg, #ffe36a, #f5b400);
  color: var(--gold-ink);
  font-weight: 800;
  font-size: 0.82rem;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(245, 180, 0, 0.32);
}

.btn-new:active { transform: translateY(1px); }

.btn-new--wide { width: 100%; margin-top: 14px; padding: 12px 16px; font-size: 0.95rem; }

.btn-new:disabled,
.level:disabled,
.action:disabled,
.key:disabled {
  opacity: 0.38;
  cursor: default;
  transform: none;
  box-shadow: none;
}

.stage {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  overflow: hidden;
  padding-top: 8px;
  transform-origin: center top;
}

.board-wrap,
.dock {
  width: min(100%, 30rem, calc((100dvh - 15.5rem) * 0.92));
}

.board-wrap,
.dock {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.dock { flex-shrink: 0; }

.board {
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  grid-template-rows: repeat(9, minmax(0, 1fr));
  border: 2px solid var(--gold);
  border-radius: 12px;
  overflow: hidden;
  background: #081428;
  box-shadow:
    0 16px 40px rgba(0, 0, 0, 0.38),
    inset 0 0 24px rgba(47, 123, 255, 0.16);
  container-type: inline-size;
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  background: var(--cell);
  color: #ffd76a;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  font-size: calc(100cqi / 13.5);
  font-weight: 700;
  cursor: pointer;
}

.cell:nth-child(3n) { border-right: 2px solid rgba(255, 208, 0, 0.85); }
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) { border-bottom: 2px solid rgba(255, 208, 0, 0.85); }
.cell:nth-child(n+73) { border-bottom: 0; }

.cell.given {
  color: #f7fbff;
  font-weight: 800;
  background: #10284f;
}

.cell.related { background: #143056; }
.cell.given.related { background: #173662; }
.cell.same { background: #1a3f78; color: #ffe08a; }
.cell.selected {
  background: #1d4e9e;
  box-shadow: inset 0 0 0 2px var(--gold);
  z-index: 1;
}
.cell.conflict { color: #ff9aab; }
.cell.error {
  background: #4a1824;
  color: #ffd0d8;
  box-shadow: inset 0 0 0 2px var(--red);
}
.cell.hinted { animation: hintFlash 0.7s ease; }

@keyframes hintFlash {
  0%, 100% { background: #1d4e9e; }
  45% { background: #14663a; color: #e9fff1; }
}

.notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  pointer-events: none;
  font-size: calc(100cqi / 42);
  font-weight: 600;
  color: #9ec4ef;
  line-height: 1;
}

.notes span {
  display: flex;
  align-items: center;
  justify-content: center;
}

.status {
  margin: 0;
  height: 1.15rem;
  line-height: 1.15rem;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status.good { color: var(--green); }
.status.bad { color: #ff8fa0; }

.pad {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 0;
  padding-inline: 2px;
}

.key {
  position: relative;
  aspect-ratio: 1;
  width: calc(100% - 6px);
  margin: 3px auto;
  border: 1px solid rgba(126, 200, 255, 0.28);
  border-radius: 10px;
  background: linear-gradient(180deg, #1a3a6e, #102646);
  color: #e8f4ff;
  font-size: clamp(0.95rem, 4.4vw, 1.2rem);
  font-weight: 800;
  cursor: pointer;
  padding: 0;
}

.key-left {
  position: absolute;
  top: 2px;
  right: 3px;
  font-size: 0.52rem;
  font-weight: 700;
  color: var(--gold);
  line-height: 1;
}

.key.is-selected {
  border-color: var(--gold);
  box-shadow: inset 0 0 0 1px var(--gold);
  color: var(--gold);
}

.key.is-complete {
  background: linear-gradient(180deg, #14663a, #0e3d28);
  border-color: rgba(61, 220, 106, 0.55);
  color: #d9ffe8;
}

.key:active:not(:disabled) { transform: scale(0.96); }

.actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  padding-inline: 2px;
}

.action {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 2.7rem;
  padding: 6px 2px;
  border-radius: 12px;
  border: 1px solid rgba(126, 176, 230, 0.2);
  background: rgba(12, 26, 52, 0.9);
  color: #d5e4f8;
  font-size: 0.68rem;
  font-weight: 800;
  cursor: pointer;
}

.action svg { width: 1.1rem; height: 1.1rem; display: block; }

.action.is-active {
  color: var(--gold-ink);
  background: linear-gradient(180deg, #ffe36a, #f5b400);
  border-color: transparent;
}

.action:active:not(:disabled) { transform: scale(0.98); }

.action--ghost {
  min-height: 2.6rem;
  flex-direction: row;
  font-size: 0.85rem;
  background: transparent;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(2, 6, 14, 0.72);
  backdrop-filter: blur(6px);
}

.modal[hidden] { display: none; }

.modal-card {
  width: min(100%, 320px);
  background: linear-gradient(180deg, #16315f, #0c1730);
  border: 1px solid rgba(255, 208, 0, 0.45);
  border-radius: 22px;
  padding: 22px 18px 16px;
  text-align: center;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.5),
    inset 0 0 28px rgba(47, 123, 255, 0.18);
}

.modal-card h2 {
  margin: 4px 0 0;
  font-size: 1.45rem;
}

.modal-kicker {
  margin: 0;
  color: var(--gold);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.win-time {
  margin: 8px 0 0;
  font-size: 2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #fff;
}

.modal-detail {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
}

.modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 16px;
}

.stage.is-loading .board { opacity: 0.45; }

@media (max-height: 700px) {
  .logo { width: 52px; }
  .header { gap: 6px; padding-bottom: 4px; }
  .board-wrap,
  .dock { width: min(100%, 30rem, calc((100dvh - 13.2rem) * 0.92)); }
  .board-wrap, .dock, .stage { gap: 6px; }
  .level { padding: 6px 2px; }
}

@media (max-height: 580px) {
  .logo { width: 42px; }
  .action { min-height: 2.35rem; }
  .clock, .btn-new { padding-block: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  .cell.hinted { animation: none; }
  .key:active, .action:active, .btn-new:active { transform: none; }
}
