:root {
  --bg: #efe8dc;
  --paper: #f7f3ea;
  --ink: #1d1b18;
  --muted: #6b645a;
  --line: #cbbfaa;
  --accent: #2f5d4a;
  --accent-2: #3f6f8a;
  --review: #f4e6b3;
  --unknown: #f3d4cc;
  --conflict: #e8b4ad;
  --given: #1d1b18;
  --filled: #3f6f8a;
  --shadow: 0 10px 30px rgba(40, 32, 20, 0.08);
  --radius: 16px;
  --tap: 48px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", "Hiragino Sans", "Noto Sans JP", sans-serif;
  line-height: 1.45;
}

body {
  min-height: 100dvh;
}

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 14px 180px;
}

.top {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 92%, white);
  backdrop-filter: blur(10px);
  padding: 12px 0 8px;
}

h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
}

.sub {
  margin: 2px 0 10px;
  color: var(--muted);
  font-size: 0.82rem;
}

.steps {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.step {
  background: #e4ddd0;
  color: var(--muted);
  border-radius: 999px;
  text-align: center;
  font-size: 0.75rem;
  padding: 6px 0;
  font-weight: 600;
}

.step.is-active {
  background: var(--accent);
  color: white;
}

.step.is-done {
  background: #cfe0d6;
  color: var(--accent);
}

.status {
  margin: 8px 0 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.viewfinder {
  position: relative;
  margin: 12px 0;
  aspect-ratio: 3 / 4;
  max-height: min(62dvh, 560px);
  background: #2a312c;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

#preview-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #2a312c;
}

.preview-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #d7d1c5;
  font-size: 0.9rem;
  padding: 24px;
  text-align: center;
}

.guide {
  pointer-events: none;
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
}

.guide-square {
  position: relative;
  width: min(86%, 86%);
  aspect-ratio: 1;
  box-shadow: 0 0 0 999px rgba(12, 16, 14, 0.42);
}

.guide-grid {
  position: absolute;
  inset: 8%;
  background-image:
    linear-gradient(to right, rgba(255, 255, 255, 0.28) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(255, 255, 255, 0.28) 1px, transparent 1px);
  background-size: calc(100% / 9) calc(100% / 9);
  opacity: 0.85;
}

.guide-square .c {
  position: absolute;
  width: 22px;
  height: 22px;
  border: 3px solid #f6f1e6;
}

.guide-square .tl {
  top: -2px;
  left: -2px;
  border-right: 0;
  border-bottom: 0;
}
.guide-square .tr {
  top: -2px;
  right: -2px;
  border-left: 0;
  border-bottom: 0;
}
.guide-square .bl {
  bottom: -2px;
  left: -2px;
  border-right: 0;
  border-top: 0;
}
.guide-square .br {
  bottom: -2px;
  right: -2px;
  border-left: 0;
  border-top: 0;
}

.guide-caption {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  width: 90%;
  margin: 0;
  text-align: center;
  color: #f6f1e6;
  font-size: 0.8rem;
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.6);
}

.actions,
.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 10px 0;
}

.row-actions[hidden] {
  display: none !important;
}

button,
.file-btn {
  appearance: none;
  border: 0;
  border-radius: 12px;
  min-height: var(--tap);
  padding: 0 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.primary {
  background: var(--accent);
  color: white;
  flex: 1 1 140px;
}

.secondary {
  background: #ece4d6;
  color: var(--ink);
  flex: 1 1 140px;
}

.ghost {
  background: transparent;
  color: var(--accent-2);
  min-height: 40px;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.message {
  white-space: pre-wrap;
  background: #e7eef2;
  border-radius: 12px;
  padding: 10px 12px;
  margin: 8px 0 14px;
  font-size: 0.92rem;
}

.message.is-ok {
  background: #dceadf;
}
.message.is-warn {
  background: #f4e9c8;
}
.message.is-error {
  background: #f3d9d4;
}

.board-wrap {
  background: var(--paper);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
}

.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  width: 100%;
  aspect-ratio: 1;
  border: 3px solid var(--ink);
  background: var(--ink);
  gap: 1px;
}

.cell {
  border: 1px solid #cfc6b6;
  border-radius: 0;
  min-height: 0;
  padding: 0;
  background: #fffdf8;
  font-size: clamp(16px, 4.6vw, 26px);
  color: var(--ink);
}

.cell.block-l {
  border-left: 3px solid var(--ink);
}
.cell.block-t {
  border-top: 3px solid var(--ink);
}
.cell.block-r {
  border-right: 3px solid var(--ink);
}
.cell.block-b {
  border-bottom: 3px solid var(--ink);
}

.cell.is-review {
  background: var(--review);
}
.cell.is-unknown {
  background: var(--unknown);
}
.cell.is-conflict {
  background: var(--conflict);
  outline: 2px solid #a3564c;
  outline-offset: -2px;
  z-index: 1;
}
.cell.is-selected {
  outline: 3px solid var(--accent-2);
  outline-offset: -3px;
  z-index: 2;
}
.cell.is-given {
  font-weight: 800;
  color: var(--given);
}
.cell.is-filled {
  font-weight: 500;
  color: var(--filled);
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
  font-size: 0.78rem;
  color: var(--muted);
}

.swatch {
  display: inline-block;
  width: 12px;
  height: 12px;
  border-radius: 3px;
  margin-right: 4px;
  vertical-align: -1px;
  border: 1px solid var(--line);
}

.swatch.review {
  background: var(--review);
}
.swatch.unknown {
  background: var(--unknown);
}
.swatch.given {
  background: #fff;
  font-weight: 800;
}

.numpad {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%) translateY(110%);
  width: min(560px, 100%);
  background: #f7f3ea;
  border-top: 1px solid var(--line);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  z-index: 30;
  transition: transform 0.18s ease;
}

.numpad.is-open {
  transform: translateX(-50%) translateY(0);
}

.numpad button {
  min-height: 52px;
  background: white;
  border: 1px solid var(--line);
}

.numpad .erase {
  grid-column: span 2;
  background: #ece4d6;
}

.busy {
  position: fixed;
  inset: 0;
  background: rgba(28, 26, 22, 0.35);
  display: grid;
  place-items: center;
  z-index: 50;
}

.busy[hidden] {
  display: none !important;
}

.busy-card {
  background: var(--paper);
  padding: 18px 22px;
  border-radius: 14px;
  font-weight: 700;
}

details.debug {
  margin: 18px 0;
  background: var(--paper);
  border-radius: var(--radius);
  padding: 10px 12px 14px;
}

details.debug summary {
  cursor: pointer;
  font-weight: 700;
  min-height: 40px;
  display: flex;
  align-items: center;
}

.debug-stages,
.debug-cells {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.debug-figure {
  margin: 0;
}

.debug-figure img,
.debug-cell img {
  width: 100%;
  background: #ddd;
  border-radius: 8px;
}

figcaption,
.muted {
  color: var(--muted);
  font-size: 0.78rem;
}

.debug-cells {
  grid-template-columns: repeat(9, 1fr);
  gap: 3px;
}

.debug-cell {
  position: relative;
}

.debug-cell-meta {
  display: block;
  font-size: 9px;
  text-align: center;
}

.debug-cell-meta.is-review {
  background: var(--review);
}
.debug-cell-meta.is-unknown {
  background: var(--unknown);
}

.debug-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.75rem;
}

.debug-table th,
.debug-table td {
  border-bottom: 1px solid var(--line);
  padding: 4px;
  text-align: left;
}

.debug-table tr.is-review {
  background: #f8efd2;
}
.debug-table tr.is-unknown {
  background: #f8e4df;
}

.debug-notes {
  padding-left: 18px;
}

.selftest {
  white-space: pre-wrap;
  font-size: 0.82rem;
  background: #efe8dc;
  padding: 8px;
  border-radius: 8px;
}

.hidden-input {
  display: none;
}

h2 {
  font-size: 1rem;
  margin: 18px 0 8px;
}

.sample-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.sample-row button {
  min-height: 40px;
  background: white;
  border: 1px solid var(--line);
  font-weight: 600;
  font-size: 0.82rem;
}

@media (min-width: 720px) {
  .app {
    max-width: 720px;
  }
  .cell {
    font-size: 28px;
  }
}
