:root {
  --bg: #fbfbfd;
  --panel: #ffffff;
  --ink: #202124;
  --muted: #7a7f87;
  --line: #d7dbe3;
  --blue: #0d57a7;
  --blue-soft: #dbeaf8;
  --green: #0f9f75;
  --green-soft: #d8f5ea;
  --amber: #f59e0b;
  --rose: #e11d48;
  --cell: #fbfdff;
  --fixed: #eef6ff;
  --shadow: 0 10px 28px rgba(31, 41, 55, .12);
}

* { box-sizing: border-box; }

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  min-height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.app {
  width: 100vw;
  height: 100dvh;
  min-height: 100svh;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  gap: 8px;
  padding: calc(6px + env(safe-area-inset-top)) 0 calc(6px + env(safe-area-inset-bottom));
}

@media (min-width: 520px) {
  .app {
    width: min(100vw, 480px);
  }
}

.topbar {
  display: grid;
  grid-template-columns: 40px 1fr 40px;
  align-items: center;
  gap: 8px;
  padding: 0 6px;
}

.brand {
  text-align: center;
  font-size: 20px;
  font-weight: 750;
  line-height: 1.1;
}

.nav-icon,
.icon-btn {
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--blue);
  font-size: 24px;
  font-weight: 650;
  cursor: pointer;
}

.icon-btn {
  justify-self: end;
  background: #eef2f7;
  color: #5c6674;
  font-size: 17px;
  font-weight: 850;
}

.statbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  text-align: center;
  padding: 0 6px;
}

.statbar div {
  min-width: 0;
}

.statbar span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  line-height: 1.15;
}

.statbar strong {
  display: block;
  margin-top: 2px;
  color: #4b5563;
  font-size: 16px;
  font-weight: 750;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.coach {
  min-height: 54px;
  padding: 8px 10px;
  border: 1px solid #d7e6fa;
  border-radius: 6px;
  background: #f7fbff;
  box-shadow: none;
}

.coach:not(.hinting) {
  display: none;
}

.coach-kicker {
  display: inline-flex;
  align-items: center;
  height: 18px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--blue-soft);
  color: var(--blue);
  font-size: 10px;
  font-weight: 850;
}

.coachText {
  margin-top: 4px;
  font-size: 12px;
  line-height: 1.35;
  font-weight: 650;
}

.coach.hinting {
  border-color: rgba(13, 87, 167, .45);
  background: #f3f8ff;
}

.board-wrap {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 0;
  padding: 0;
}

.board {
  width: min(100vw, 480px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  border: 2.5px solid #30343a;
  border-radius: 2px;
  overflow: hidden;
  background: #30343a;
  box-shadow: none;
}

.cell {
  position: relative;
  min-width: 0;
  min-height: 0;
  border: 1px solid #d1d5db;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--blue);
  font-size: clamp(20px, 7vw, 34px);
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
  user-select: none;
}

.cell:nth-child(3n) { border-right-width: 3px; }
.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) { border-bottom-width: 3px; }
.cell:nth-child(9n) { border-right-width: 1px; }

.cell.fixed {
  color: var(--ink);
  background: #ffffff;
}

.cell.peer {
  background: #e9eef4;
}

.cell.same {
  background: #dce8f6;
}

.cell.selected {
  background: #bfd8f4;
  box-shadow: inset 0 0 0 2px var(--blue);
  z-index: 1;
}

.cell.hint {
  background: #dbeafe;
  box-shadow: inset 0 0 0 2px #2563eb;
  z-index: 2;
}

.cell.bad {
  color: var(--rose);
  background: #fff1f3;
}

.notes {
  width: 82%;
  height: 82%;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 0;
  color: var(--muted);
  font-size: clamp(7px, 2.3vw, 11px);
  font-weight: 800;
  line-height: 1;
}

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

.controls {
  display: grid;
  gap: 6px;
  padding: 0 6px;
}

.mode-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  align-items: center;
}

.tool,
.primary,
.check {
  height: 54px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  font-size: 12px;
  font-weight: 650;
}

.tool {
  height: 54px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: #6b7280;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 12px;
  font-weight: 650;
  cursor: pointer;
}

.tool span {
  width: 24px;
  height: 24px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #6b7280;
  font-size: 20px;
  font-weight: 850;
  line-height: 1;
}

.tool.active {
  color: var(--blue);
}

.tool.strong {
  color: var(--blue);
}

.check {
  min-width: 0;
  padding: 0;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  color: var(--muted);
  white-space: nowrap;
}

.check input {
  width: 16px;
  height: 16px;
  accent-color: var(--blue);
}

.numpad {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 0;
}

.num {
  height: 58px;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--blue);
  font-size: 34px;
  font-weight: 450;
  cursor: pointer;
}

.num.active {
  color: var(--blue);
  background: var(--blue-soft);
}

.num.done {
  opacity: .35;
}

.ad-slot {
  height: 46px;
  border: 1px dashed #9aa8bc;
  border-radius: 8px;
  background: linear-gradient(180deg, #ffffff, #f6f8fb);
  color: var(--muted);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 9px;
  padding: 0 12px;
  font-size: 12px;
  box-shadow: 0 3px 10px rgba(31, 41, 55, .05);
}

.ad-slot span {
  width: 30px;
  height: 22px;
  border-radius: 5px;
  background: #e2e8f0;
  color: #475569;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: 900;
}

.ad-slot strong {
  font-size: 12px;
  font-weight: 850;
  color: #475569;
}

.ad-slot small {
  font-size: 11px;
  font-weight: 800;
  color: #94a3b8;
}

.result {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(17, 24, 39, .42);
  z-index: 10;
}

.result.hidden { display: none; }

.result-card {
  width: min(100%, 360px);
  border-radius: 8px;
  background: var(--panel);
  padding: 24px;
  text-align: center;
  box-shadow: 0 24px 80px rgba(15, 23, 42, .34);
}

.result-title {
  font-size: 28px;
  font-weight: 900;
}

.result-sub {
  margin: 8px 0 18px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
  line-height: 1.5;
}

.primary {
  width: 100%;
  background: var(--green);
  border-color: var(--green);
  color: #fff;
  cursor: pointer;
}

@media (max-width: 350px) {
  .app {
    gap: 5px;
    padding: calc(5px + env(safe-area-inset-top)) 0 calc(5px + env(safe-area-inset-bottom));
  }
  .brand { font-size: 18px; }
  .topbar { grid-template-columns: 34px 1fr 34px; }
  .nav-icon,
  .icon-btn { width: 34px; height: 34px; }
  .statbar { gap: 4px; }
  .statbar span { font-size: 10px; }
  .statbar strong { font-size: 13px; }
  .coach { min-height: 44px; padding: 6px 8px; }
  .coach-kicker { height: 18px; font-size: 10px; }
  .coachText { margin-top: 3px; font-size: 10.5px; line-height: 1.25; }
  .board { width: 100vw; }
  .mode-row { grid-template-columns: repeat(4, 1fr); gap: 3px; }
  .tool,
  .check { height: 42px; font-size: 10.5px; }
  .tool span { width: 20px; height: 20px; font-size: 17px; }
  .check { min-width: 0; padding: 0 5px; }
  .check input { width: 14px; height: 14px; }
  .num { height: 42px; font-size: 27px; }
  .ad-slot {
    height: 38px;
    padding: 0 9px;
    gap: 6px;
  }
  .ad-slot small { display: none; }
}

@media (max-width: 350px) and (max-height: 620px) {
  .board { width: 100vw; }
  .num { height: 34px; font-size: 24px; }
  .tool,
  .check { height: 34px; }
  .coach { display: none; }
  .statbar span { display: none; }
  .statbar strong { font-size: 12px; }
}
