:root {
  color-scheme: light;
  --shell: oklch(77% 0.035 262);
  --shell-shadow: oklch(48% 0.035 262);
  --shell-light: oklch(88% 0.018 262);
  --ink: oklch(28% 0.055 115);
  --ink-soft: oklch(39% 0.052 115);
  --lcd: oklch(73% 0.095 112);
  --lcd-light: oklch(79% 0.08 112);
  --lcd-shadow: oklch(58% 0.075 112);
  --accent: oklch(43% 0.16 8);
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 24px;
  font-family: "Trebuchet MS", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  width: 100%;
  height: 100%;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  display: grid;
  place-items: center;
  padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  color: var(--ink);
  background:
    radial-gradient(circle at 50% 18%, oklch(43% 0.04 265 / 0.35), transparent 30%),
    oklch(27% 0.025 270);
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }

.console {
  width: min(100%, 560px);
  height: min(100%, 860px);
  min-height: 0;
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: clamp(8px, 1.8vh, 16px);
  padding: clamp(10px, 2vh, 22px);
  border-radius: 18px 18px 52px 18px;
  background: var(--shell);
  box-shadow:
    inset 3px 3px 0 var(--shell-light),
    inset -4px -4px 0 var(--shell-shadow),
    0 14px 34px oklch(10% 0.01 270 / 0.48);
}

.screen-bezel {
  min-height: 0;
  padding: clamp(8px, 1.8vh, 18px);
  border-radius: 12px 12px 34px 12px;
  background: oklch(35% 0.025 263);
  box-shadow: inset 2px 2px 0 oklch(24% 0.02 263), inset -2px -2px 0 oklch(49% 0.025 263);
}

.screen {
  height: 100%;
  min-height: 0;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: clamp(5px, 1vh, 10px);
  padding: clamp(10px, 1.8vh, 18px);
  border: 3px solid var(--ink);
  border-radius: 3px;
  background:
    repeating-linear-gradient(0deg, transparent 0 3px, oklch(50% 0.05 112 / 0.08) 3px 4px),
    var(--lcd);
  box-shadow: inset 0 0 22px var(--lcd-shadow), 0 0 0 2px var(--lcd-shadow);
}

.topbar,
.statusbar,
.legend,
.controls {
  display: flex;
  align-items: center;
}

.topbar { justify-content: space-between; gap: var(--space-md); }
.brand-lockup { display: flex; align-items: center; gap: var(--space-sm); min-width: 0; }
.brand-lockup h1 {
  margin: 0;
  font-family: Impact, Haettenschweiler, sans-serif;
  font-size: clamp(1.65rem, 5.8vw, 2.55rem);
  line-height: 0.9;
  letter-spacing: 0.055em;
  text-shadow: 2px 2px 0 var(--lcd-shadow);
}
.brand-lockup p {
  margin: 4px 0 0;
  font-family: "Lucida Console", Monaco, monospace;
  font-size: clamp(0.42rem, 1.7vw, 0.62rem);
  letter-spacing: 0.12em;
}
.power-light {
  width: 8px;
  aspect-ratio: 1;
  flex: 0 0 auto;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 2px var(--lcd-shadow);
}

.size-control { display: grid; grid-template-columns: 32px auto 32px; align-items: center; border: 2px solid var(--ink); }
.size-control button {
  width: 32px;
  height: 32px;
  border: 0;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
  font-family: "Lucida Console", Monaco, monospace;
  font-weight: 700;
}
.size-control button:first-child { border-right: 2px solid var(--ink); }
.size-control button:last-child { border-left: 2px solid var(--ink); }
.size-control output {
  min-width: 49px;
  padding: 0 5px;
  text-align: center;
  font-family: "Lucida Console", Monaco, monospace;
  font-size: 0.72rem;
  font-weight: 700;
}

.statusbar {
  min-height: 26px;
  justify-content: space-between;
  gap: var(--space-sm);
  padding: 5px 0;
  border-top: 2px dotted var(--ink-soft);
  border-bottom: 2px dotted var(--ink-soft);
  font-family: "Lucida Console", Monaco, monospace;
  font-size: clamp(0.58rem, 2.3vw, 0.72rem);
  font-weight: 700;
  text-transform: uppercase;
}
.statusbar p { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.puzzle-wrap {
  min-height: 0;
  display: grid;
  place-items: center;
  container-type: size;
}

.board {
  --size: 5;
  width: min(100cqw, 100cqh);
  height: min(100cqw, 100cqh);
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr)) minmax(28px, 0.88fr);
  grid-template-rows: repeat(var(--size), minmax(0, 1fr)) minmax(28px, 0.88fr);
  gap: 2px;
  padding: 2px;
  background: var(--ink);
  box-shadow: 3px 3px 0 var(--lcd-shadow);
}

.cell,
.sum-cell,
.corner-cell {
  min-width: 0;
  min-height: 0;
  display: grid;
  place-items: center;
  border: 0;
  color: var(--ink);
  background: var(--lcd-light);
  font-family: "Lucida Console", Monaco, monospace;
  font-weight: 700;
}

.cell,
.sum-cell {
  font-size: clamp(0.73rem, calc(7cqi / var(--size)), 1.55rem);
}

.cell {
  position: relative;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}
.cell::before,
.cell::after { content: ""; position: absolute; pointer-events: none; }
.cell.is-kept::before {
  inset: 12%;
  border: max(2px, 0.12em) solid var(--ink);
  border-radius: 50%;
}
.cell.is-crossed { color: var(--ink-soft); background: var(--lcd-shadow); }
.cell.is-crossed::before,
.cell.is-crossed::after {
  top: 48%; left: 8%; width: 84%; height: max(2px, 0.11em); background: var(--ink);
}
.cell.is-crossed::before { transform: rotate(45deg); }
.cell.is-crossed::after { transform: rotate(-45deg); }
.cell:focus-visible,
.size-control button:focus-visible,
.control-button:focus-visible { outline: 3px double var(--ink); outline-offset: -5px; }

.sum-cell {
  background: var(--lcd-shadow);
}
.sum-cell.is-met { color: var(--lcd-light); background: var(--ink); }
.sum-cell.is-over { text-decoration: line-through; }
.corner-cell { background: var(--ink); }

.legend {
  justify-content: center;
  gap: clamp(14px, 7vw, 36px);
  min-height: 20px;
  font-family: "Lucida Console", Monaco, monospace;
  font-size: clamp(0.52rem, 2vw, 0.68rem);
  font-weight: 700;
  text-transform: uppercase;
}
.legend span { display: flex; align-items: center; gap: 5px; }
.legend i { position: relative; width: 16px; height: 16px; display: inline-block; }
.legend-circle { border: 2px solid var(--ink); border-radius: 50%; }
.legend-cross::before,
.legend-cross::after { content: ""; position: absolute; top: 7px; left: 1px; width: 14px; height: 2px; background: var(--ink); }
.legend-cross::before { transform: rotate(45deg); }
.legend-cross::after { transform: rotate(-45deg); }

.controls {
  min-height: 62px;
  justify-content: space-between;
  gap: var(--space-sm);
  padding: 0 clamp(6px, 2.5vw, 18px);
}
.control-button {
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px;
  border: 0;
  color: oklch(31% 0.045 270);
  background: transparent;
  cursor: pointer;
  font-size: clamp(0.67rem, 2.6vw, 0.9rem);
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.control-button span {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border: 2px solid oklch(28% 0.06 7);
  border-radius: 50%;
  color: var(--shell-light);
  background: var(--accent);
  box-shadow: inset -3px -3px 0 oklch(29% 0.12 8), 2px 3px 0 var(--shell-shadow);
  font-family: "Lucida Console", Monaco, monospace;
}
.control-button:active span { transform: translate(1px, 2px); box-shadow: inset -2px -2px 0 oklch(29% 0.12 8); }
.secondary { flex-direction: row-reverse; }
.secondary span { background: oklch(39% 0.095 298); border-color: oklch(28% 0.055 298); box-shadow: inset -3px -3px 0 oklch(28% 0.07 298), 2px 3px 0 var(--shell-shadow); }
.speaker { width: 46px; display: grid; grid-template-columns: repeat(3, 5px); gap: 5px; transform: rotate(-12deg); }
.speaker i { width: 5px; height: 5px; border-radius: 50%; background: var(--shell-shadow); }

@media (max-height: 620px) {
  .console { grid-template-rows: minmax(0, 1fr) 50px; border-radius: 14px 14px 34px 14px; }
  .screen { padding: 8px; gap: 4px; }
  .screen-bezel { padding: 8px; border-radius: 8px 8px 22px 8px; }
  .brand-lockup p, .legend { display: none; }
  .control-button span { width: 38px; height: 38px; }
  .control-button { min-height: 42px; }
  .controls { min-height: 46px; }
}

@media (orientation: landscape) and (max-height: 560px) {
  .console { width: min(100%, 820px); grid-template-columns: minmax(0, 1fr) 190px; grid-template-rows: minmax(0, 1fr); border-radius: 14px 14px 34px 14px; }
  .controls { min-height: 0; flex-direction: column-reverse; justify-content: center; padding: 0; }
  .control-button, .secondary { width: 100%; flex-direction: row; justify-content: flex-start; }
  .speaker { margin: 6px 0; }
  .screen { grid-template-columns: 0.75fr minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  .topbar { grid-column: 1; grid-row: 1; flex-direction: column; align-items: flex-start; justify-content: flex-start; }
  .statusbar { grid-column: 1; grid-row: 2; flex-direction: column; justify-content: flex-end; align-items: flex-start; border-bottom: 0; }
  .puzzle-wrap { grid-column: 2; grid-row: 1 / span 2; }
  .legend { display: none; }
}

@media (min-width: 720px) and (min-height: 700px) {
  .console { max-height: 900px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
