:root {
  --bg0: #070b14;
  --card: #121a2b;
  --card-2: #1a2438;
  --line: #2a3650;
  --text: #f1f5f9;
  --muted: #8b9bb4;
  --accent: #5b9dff;
  --accent-hi: #7ab0ff;
  --accent-ink: #061018;
  --good: #3dd68c;
  --warn: #f5c542;
  --danger: #ff6b7a;
  --given: #e8eef8;
  --filled: #7ec8ff;
  --low: rgba(245, 197, 66, 0.28);
  --conflict: rgba(255, 107, 122, 0.35);
  --radius: 16px;
  --radius-sm: 12px;
  --touch: 48px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100dvh;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(91, 157, 255, 0.12), transparent 55%),
    radial-gradient(900px 500px at 100% 0%, rgba(61, 214, 140, 0.08), transparent 50%),
    var(--bg0);
  color: var(--text);
  line-height: 1.45;
  touch-action: manipulation;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 1rem 0.9rem calc(1.5rem + var(--safe-bottom));
}

.hero { margin-bottom: 0.9rem; }

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.logo {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 1.25rem;
  background: linear-gradient(145deg, var(--accent), #3b6fd4);
  color: #fff;
  box-shadow: 0 6px 18px rgba(91, 157, 255, 0.35);
}

header h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 750;
  letter-spacing: -0.03em;
}

.tagline {
  margin: 0.1rem 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.card {
  background: linear-gradient(180deg, var(--card) 0%, #0e1524 100%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.85rem;
  box-shadow: var(--shadow);
  margin-bottom: 0.85rem;
}

/* ----- capture ----- */

.video-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  background: #000;
  border-radius: calc(var(--radius) - 4px);
  overflow: hidden;
  border: 1px solid #1c2740;
}

.capture.camera-live .video-wrap {
  aspect-ratio: 3 / 4;
  max-height: min(62vh, 520px);
}

video, #preview-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.frame-guide {
  pointer-events: none;
  position: absolute;
  inset: 8%;
}

.frame-guide .corner {
  position: absolute;
  width: 22px;
  height: 22px;
  border: 3px solid var(--accent);
  opacity: 0.95;
}
.frame-guide .tl { top: 0; left: 0; border-right: 0; border-bottom: 0; border-radius: 4px 0 0 0; }
.frame-guide .tr { top: 0; right: 0; border-left: 0; border-bottom: 0; border-radius: 0 4px 0 0; }
.frame-guide .bl { bottom: 0; left: 0; border-right: 0; border-top: 0; border-radius: 0 0 0 4px; }
.frame-guide .br { bottom: 0; right: 0; border-left: 0; border-top: 0; border-radius: 0 0 4px 0; }

.scan-overlay {
  pointer-events: none;
  position: absolute;
  left: 10%;
  right: 10%;
  bottom: 12%;
  text-align: center;
}
.scan-overlay p {
  margin: 0;
  padding: 0.45rem 0.65rem;
  border-radius: 999px;
  background: rgba(7, 11, 20, 0.72);
  color: #dbe7ff;
  font-size: 0.78rem;
  backdrop-filter: blur(6px);
}

.capture-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

@media (min-width: 400px) {
  .capture-actions {
    grid-template-columns: repeat(4, 1fr);
  }
}

.capture.camera-live #btn-snap {
  box-shadow: 0 4px 14px rgba(91, 157, 255, 0.35);
}

/* ----- board result bar ----- */

.result-bar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

.thumb-wrap {
  flex: 0 0 auto;
  width: 56px;
  height: 56px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #000;
}

.thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.result-meta { min-width: 0; flex: 1; }

.result-meta h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.hint {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0.15rem 0 0;
}

.hint kbd {
  display: inline-block;
  padding: 0.05rem 0.35rem;
  border-radius: 4px;
  border: 1px solid #3a4a66;
  background: #0a1020;
  font-size: 0.75rem;
}

.swatch-row {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: 0.15rem;
  flex-wrap: wrap;
}

.low-conf-swatch, .conflict-swatch {
  display: inline-block;
  width: 0.8em;
  height: 0.8em;
  vertical-align: middle;
  border-radius: 3px;
}
.low-conf-swatch { background: var(--low); border: 1px solid var(--warn); }
.conflict-swatch { background: var(--conflict); border: 1px solid var(--danger); }

/* ----- board ----- */

.board-wrap {
  width: 100%;
  margin: 0 auto;
  touch-action: none;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: 100%;
  border: 2.5px solid #dbe7ff;
  background: #050912;
  user-select: none;
  outline: none;
  border-radius: 8px;
  overflow: hidden;
}

.board:focus-visible {
  box-shadow: 0 0 0 3px rgba(91, 157, 255, 0.45);
}

.board.view-only .cell {
  cursor: default;
}

.board.editing .cell {
  cursor: pointer;
}

.cell {
  aspect-ratio: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.05rem, 5.4vw, 1.55rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  border: 1px solid #243147;
  background: #10192b;
  color: var(--given);
  cursor: default;
  margin: 0;
  padding: 0;
  appearance: none;
  -webkit-appearance: none;
  font-family: inherit;
  transition: background 0.1s, color 0.1s;
}

@media (hover: hover) {
  .board.editing .cell:hover { background: #18243a; }
}

.cell:nth-child(9n + 3),
.cell:nth-child(9n + 6) {
  border-right: 2px solid #dbe7ff;
}
.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom: 2px solid #dbe7ff;
}

.cell.low-conf {
  background: var(--low);
  color: #fff8e1;
}

.cell.conflict {
  background: var(--conflict);
  color: #fff;
}

.cell.solution {
  color: var(--filled);
  font-weight: 650;
}

.cell.given {
  color: var(--given);
}

.cell.active {
  outline: 2.5px solid var(--accent);
  outline-offset: -2.5px;
  z-index: 1;
  background: #153256;
}

/* ----- edit panel (hidden by default) ----- */

.edit-panel {
  margin-top: 0.75rem;
  padding: 0.65rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.22);
}

.edit-panel-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.edit-hint {
  margin: 0;
  flex: 1;
}

.numpad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.4rem;
}

.pad-key {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  min-height: var(--touch);
  font-size: 1.2rem;
  font-weight: 700;
  background: var(--card-2);
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  transition: background 0.1s;
}

@media (hover: hover) {
  .pad-key:hover { background: #243552; }
}

.pad-key:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.pad-wide {
  grid-column: span 2;
  font-size: 0.95rem;
  background: #2a1520;
  border-color: #5a2436;
  color: #ffc9d0;
}

.clear-btn {
  width: 100%;
  margin-top: 0.5rem;
}

/* ----- actions ----- */

.board-actions {
  display: grid;
  grid-template-columns: 1.2fr 1fr 0.9fr;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.btn, .file-label {
  appearance: none;
  border: none;
  border-radius: var(--radius-sm);
  min-height: var(--touch);
  padding: 0.5rem 0.65rem;
  font-size: 0.9rem;
  font-weight: 650;
  cursor: pointer;
  text-align: center;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  user-select: none;
  transition: background 0.12s, transform 0.08s, border-color 0.12s;
  font-family: inherit;
}

.btn-sm {
  min-height: 36px;
  padding: 0.35rem 0.7rem;
  font-size: 0.82rem;
  flex: 0 0 auto;
}

.btn:active:not(:disabled), .file-label:active, .pad-key:active {
  transform: scale(0.97);
}

.btn:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

.btn.primary {
  background: linear-gradient(180deg, var(--accent-hi), var(--accent));
  color: var(--accent-ink);
  box-shadow: 0 4px 14px rgba(91, 157, 255, 0.28);
}
.btn.primary:hover:not(:disabled) { filter: brightness(1.05); }

.btn.secondary, .file-label {
  background: var(--card-2);
  color: var(--text);
  border: 1px solid var(--line);
}
.btn.secondary:hover:not(:disabled), .file-label:hover {
  background: #22304a;
}

.btn.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px dashed var(--line);
}
.btn.ghost:hover:not(:disabled) {
  color: var(--text);
  border-style: solid;
  background: rgba(255, 255, 255, 0.03);
}

.btn.active-edit {
  background: #153256;
  border-color: var(--accent);
  color: var(--accent-hi);
}

.status {
  min-height: 1.25rem;
  margin: 0.55rem 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}
.status.error { color: var(--danger); }
.status.ok { color: var(--good); }

.meta {
  margin: 0.25rem 0 0;
  font-size: 0.75rem;
  color: #667891;
}

.hidden { display: none !important; }

footer {
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}
footer a { color: var(--accent-hi); text-decoration: none; }
footer a:hover { text-decoration: underline; }

@media (max-width: 380px) {
  .board-actions {
    grid-template-columns: 1fr 1fr;
  }
  .board-actions #btn-rescan {
    grid-column: 1 / -1;
  }
}
