:root {
  --ink: #111827;
  --muted: #64748b;
  --line: #d8e2ea;
  --paper: #ffffff;
  --teal: #14b8a6;
  --blue: #2563eb;
  --amber: #f59e0b;
  --red: #ef4444;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  font-family: Inter, system-ui, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  background: #eef7f6;
  color: var(--ink);
}

button, input { font: inherit; }
button {
  border: 0;
  cursor: pointer;
  font-weight: 800;
  letter-spacing: 0;
  transition: transform .16s ease, filter .16s ease, box-shadow .16s ease;
}
button:hover:not(:disabled) { transform: translateY(-1px); filter: brightness(1.04); }
button:disabled { cursor: not-allowed; opacity: .44; }

.app { min-height: 100%; position: relative; }
.screen { position: absolute; inset: 0; display: none; }
.screen.is-active { display: flex; }

.menu-screen {
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
  background: linear-gradient(135deg, #f8fafc 0%, #e0f2fe 46%, #dcfce7 100%);
}
.menu-screen.is-exiting {
  animation: menuExit .18s ease both;
}

.menu-panel {
  width: min(760px, 100%);
  border: 1px solid #ffffff;
  border-radius: 8px;
  background: #ffffffee;
  box-shadow: 0 24px 80px #0f172a24;
  padding: 24px;
}

.title-row {
  display: flex;
  align-items: center;
  gap: 16px;
}
.brand-mark {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  background: var(--ink);
  display: grid;
  place-items: center;
  flex: 0 0 auto;
}
.brand-mark span {
  width: 28px;
  height: 28px;
  border: 5px solid #fff;
  border-left-color: var(--teal);
  border-bottom-color: var(--amber);
  transform: rotate(45deg);
}
.eyebrow {
  margin: 0;
  color: var(--blue);
  font-size: 13px;
  font-weight: 900;
  text-transform: uppercase;
}
h1 {
  margin: 2px 0 0;
  font-size: clamp(36px, 7vw, 64px);
  line-height: .95;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  margin-top: 24px;
}
.mode-card {
  min-height: 94px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f8fafc;
  color: var(--ink);
  padding: 16px;
  text-align: left;
}
.mode-card strong {
  display: block;
  font-size: 22px;
}
.mode-card span {
  display: block;
  margin-top: 6px;
  color: var(--muted);
  font-weight: 800;
}
.mode-card.is-selected {
  color: #fff;
  border-color: transparent;
  background: linear-gradient(135deg, var(--ink), #1d4ed8 58%, #0f766e);
  box-shadow: 0 12px 26px #1d4ed82b;
}
.mode-card.is-selected span { color: #dbeafe; }

.stock-panel, .preview-panel, .setting-panel, .difficulty-panel, .info {
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  padding: 14px;
}
.field-row, .action-row {
  display: flex;
  gap: 10px;
  align-items: center;
}
.field-row input, .number-panel input {
  flex: 1;
  min-width: 0;
  height: 48px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  padding: 0 14px;
  outline: none;
  font-weight: 750;
}
.field-row input:focus, .number-panel input:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 4px #2563eb1a;
}
.tool-button {
  min-height: 48px;
  border-radius: 8px;
  padding: 0 18px;
}
.primary { background: var(--blue); color: #fff; box-shadow: 0 6px 0 #1e40af; }
.secondary { background: var(--amber); color: #2f1b00; box-shadow: 0 6px 0 #b45309; }
.quiet { background: #ecfeff; color: #0f766e; border: 1px solid #99f6e4; }
.big { min-width: 156px; font-size: 20px; }

.results {
  display: grid;
  gap: 8px;
  max-height: 174px;
  overflow: auto;
  margin-top: 10px;
}
.result {
  display: grid;
  gap: 3px;
  border: 1px solid #dbeafe;
  border-radius: 8px;
  background: #f8fafc;
  color: var(--ink);
  padding: 11px 12px;
  text-align: left;
}
.result strong { font-size: 15px; }
.result span { color: var(--muted); font-size: 13px; font-weight: 750; }

.preview-panel {
  display: grid;
  gap: 8px;
  padding: 10px;
}
.preview-panel canvas {
  width: 100%;
  height: 150px;
  display: block;
  border-radius: 6px;
  background: #f8fafc;
}
.preview-panel span {
  color: #475569;
  font-size: 13px;
  font-weight: 850;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 180px;
  gap: 10px;
}
.number-panel {
  display: grid;
  gap: 8px;
  color: var(--muted);
  font-weight: 900;
}
.number-panel input { width: 100%; color: var(--ink); }
.difficulty-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}
.chip {
  min-height: 42px;
  border: 1px solid #dbeafe;
  border-radius: 8px;
  background: #f8fafc;
  color: #334155;
  padding: 0 8px;
  white-space: nowrap;
}
.chip.is-selected {
  background: #111827;
  border-color: #111827;
  color: #fff;
  box-shadow: 0 7px 0 #334155;
}

.range-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--muted);
  font-weight: 900;
}
.slider {
  width: 100%;
  height: 30px;
  appearance: none;
  background: transparent;
  margin-top: 10px;
}
.slider::-webkit-slider-runnable-track {
  height: 10px;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--teal), var(--amber));
}
.slider::-webkit-slider-thumb {
  appearance: none;
  width: 28px;
  height: 28px;
  margin-top: -9px;
  border-radius: 50%;
  background: #fff;
  border: 6px solid var(--ink);
}
.slider::-moz-range-track {
  height: 10px;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--teal), var(--amber));
}
.slider::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 6px solid var(--ink);
}
.info {
  min-height: 48px;
  color: #334155;
  font-weight: 800;
}
.action-row { margin-top: 14px; justify-content: flex-end; }

.game-screen {
  align-items: stretch;
  justify-content: stretch;
  background: #f8fafc;
}
.game-screen.is-active { animation: sceneEnter .56s cubic-bezier(.2, .9, .2, 1) both; }
.game-screen.is-starting #game { animation: gameStart .52s cubic-bezier(.2, .9, .2, 1); }
.game-screen.is-ending #game { animation: gameEnd .42s ease both; }
#game {
  width: 100vw;
  height: 100vh;
  display: block;
  touch-action: none;
}
.game-overlay {
  pointer-events: none;
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  opacity: 0;
}
.game-overlay span {
  min-width: 210px;
  border-radius: 8px;
  background: #111827e8;
  color: #fff;
  padding: 18px 30px;
  text-align: center;
  font-size: clamp(34px, 8vw, 76px);
  font-weight: 950;
  letter-spacing: 0;
  box-shadow: 0 24px 70px #0f172a45;
  transform: translateY(22px) scale(.86);
}
.game-overlay.show {
  opacity: 1;
}
.game-overlay.show span {
  animation: messagePop .82s cubic-bezier(.16, 1, .3, 1) both;
}
.game-overlay.show.finish span {
  background: #0f766ee8;
  animation-duration: 1.38s;
}
.game-overlay.show.end span {
  background: #334155e8;
}
.game-overlay.show.record span {
  background: #f59e0be8;
  color: #111827;
  box-shadow: 0 26px 78px #f59e0b48;
  animation-duration: 1.16s;
}
.back-button {
  position: absolute;
  right: 16px;
  top: 118px;
  width: 46px;
  height: 46px;
  border-radius: 8px;
  background: #ffffffe6;
  border: 1px solid #e2e8f0;
  box-shadow: 0 12px 28px #0f172a1f;
}
.back-button::before, .back-button::after {
  content: "";
  position: absolute;
  width: 24px;
  height: 5px;
  left: 10px;
  top: 20px;
  border-radius: 4px;
  background: var(--ink);
}
.back-button::before { transform: rotate(45deg); }
.back-button::after { transform: rotate(-45deg); }

.mobile-controls {
  position: absolute;
  inset: auto 20px 22px 20px;
  display: none;
  justify-content: space-between;
  pointer-events: none;
}
.mobile-controls button {
  pointer-events: auto;
  width: 82px;
  height: 82px;
  border-radius: 8px;
  background: #ffffffe8;
  border: 1px solid #e2e8f0;
  box-shadow: 0 14px 32px #0f172a30;
  position: relative;
}
.mobile-controls button span::before {
  content: "";
  position: absolute;
  width: 30px;
  height: 30px;
  left: 25px;
  border-left: 8px solid var(--ink);
  border-top: 8px solid var(--ink);
}
.mobile-controls [data-hold="up"] span::before {
  top: 30px;
  transform: rotate(45deg);
}
.mobile-controls [data-hold="down"] span::before {
  top: 20px;
  transform: rotate(225deg);
}

@keyframes menuExit {
  from { opacity: 1; transform: scale(1); filter: blur(0); }
  to { opacity: 0; transform: scale(.98); filter: blur(3px); }
}
@keyframes sceneEnter {
  from { opacity: 0; transform: translateY(18px) scale(1.025); filter: blur(6px); }
  to { opacity: 1; transform: translateY(0) scale(1); filter: blur(0); }
}
@keyframes gameStart {
  from { opacity: 0; transform: scale(1.04); filter: blur(4px); }
  to { opacity: 1; transform: scale(1); filter: blur(0); }
}
@keyframes gameEnd {
  from { filter: saturate(1); }
  to { filter: saturate(.35) brightness(.82); }
}
@keyframes messagePop {
  0% { opacity: 0; transform: translateY(26px) scale(.78); }
  18% { opacity: 1; transform: translateY(0) scale(1.06); }
  72% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-18px) scale(.95); }
}

@media (pointer: coarse), (max-width: 820px) {
  .mobile-controls { display: flex; }
}

@media (max-width: 720px) {
  body { overflow: auto; }
  .menu-screen { align-items: flex-start; padding: 14px; }
  .menu-panel { padding: 16px; }
  .title-row { align-items: flex-start; }
  .brand-mark { width: 48px; height: 48px; }
  .mode-grid, .settings-grid { grid-template-columns: 1fr; }
  .difficulty-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .field-row, .action-row { flex-wrap: wrap; }
  .field-row input { flex-basis: 100%; }
  .tool-button { flex: 1 1 auto; }
  .big { flex-basis: 100%; }
}

@media (max-height: 560px) and (orientation: landscape) {
  .menu-screen { align-items: flex-start; }
  .menu-panel { padding: 14px; }
  .mode-grid { margin-top: 14px; }
  .mode-card { min-height: 70px; }
  .stock-panel, .preview-panel, .setting-panel, .difficulty-panel, .info { margin-top: 10px; padding: 10px; }
  .preview-panel canvas { height: 96px; }
}
