:root {
  color-scheme: light dark;
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --line: #c7cbd1;
  --line-strong: #2b2f36;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --given: #1c1e21;
  --entered: #2563eb;
  --conflict: #dc2626;
  --conflict-bg: #fde2e1;
  --selected-bg: #dbe7ff;
  --peer-bg: #eef2fb;
  --success-bg: #dcfce7;
  --success-text: #166534;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1e2126;
    --text: #eef0f2;
    --muted: #9aa1ab;
    --line: #3a3f47;
    --line-strong: #eef0f2;
    --accent: #3b82f6;
    --accent-contrast: #0b0d10;
    --given: #eef0f2;
    --entered: #7aa5f7;
    --conflict: #f87171;
    --conflict-bg: #4a1f1f;
    --selected-bg: #223052;
    --peer-bg: #1c2434;
    --success-bg: #103824;
    --success-text: #86efac;
  }
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --panel: #1e2126;
  --text: #eef0f2;
  --muted: #9aa1ab;
  --line: #3a3f47;
  --line-strong: #eef0f2;
  --accent: #3b82f6;
  --accent-contrast: #0b0d10;
  --given: #eef0f2;
  --entered: #7aa5f7;
  --conflict: #f87171;
  --conflict-bg: #4a1f1f;
  --selected-bg: #223052;
  --peer-bg: #1c2434;
  --success-bg: #103824;
  --success-text: #86efac;
}

:root[data-theme="light"] {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1e21;
  --muted: #6b7280;
  --line: #c7cbd1;
  --line-strong: #2b2f36;
  --accent: #2563eb;
  --accent-contrast: #ffffff;
  --given: #1c1e21;
  --entered: #2563eb;
  --conflict: #dc2626;
  --conflict-bg: #fde2e1;
  --selected-bg: #dbe7ff;
  --peer-bg: #eef2fb;
  --success-bg: #dcfce7;
  --success-text: #166534;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior-y: contain;
}

.app {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.app-header {
  padding: 0.75rem 1rem 0.25rem;
  text-align: center;
}
.app-header h1 {
  margin: 0;
  font-size: 1.4rem;
  letter-spacing: 0.02em;
}

.tabs {
  display: flex;
  gap: 0.4rem;
  padding: 0.5rem 0.75rem;
  overflow-x: auto;
}
.tab-btn {
  flex: 1 1 0;
  min-width: 44px;
  padding: 0.55rem 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  position: relative;
}
.tab-btn.active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}
.tab-btn .dot {
  position: absolute;
  top: 4px;
  right: 6px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success-text);
}

.mode-toggle {
  display: flex;
  gap: 0.4rem;
  padding: 0 0.75rem 0.5rem;
}
.mode-btn {
  flex: 1;
  padding: 0.5rem 0;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.mode-btn.active {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}

.board-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0.25rem 0.75rem 1rem;
  gap: 0.75rem;
}

.status {
  margin: 0;
  min-height: 1.4em;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--muted);
  text-align: center;
}
.status.solved {
  color: var(--success-text);
  background: var(--success-bg);
  padding: 0.35rem 0.9rem;
  border-radius: 999px;
}
.status.error {
  color: var(--conflict);
}

.board-wrap {
  position: relative;
  width: min(94vw, 460px);
  aspect-ratio: 1 / 1;
}

.board {
  position: absolute;
  inset: calc(100% / 11);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  background: var(--line-strong);
  border: 2px solid var(--line-strong);
  border-radius: 8px;
  overflow: hidden;
  gap: 1px;
}

.ink-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  pointer-events: none;
  touch-action: none;
}
.ink-canvas.drawing {
  pointer-events: auto;
  cursor: crosshair;
}

.cell {
  background: var(--panel);
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1rem, 4.2vw, 1.5rem);
  font-weight: 500;
  color: var(--entered);
  padding: 0;
  margin: 0;
  cursor: pointer;
  position: relative;
}
.cell.given {
  color: var(--given);
  font-weight: 700;
  cursor: default;
}
.cell.selected { background: var(--selected-bg); }
.cell.peer { background: var(--peer-bg); }
.cell.conflict { background: var(--conflict-bg); color: var(--conflict); }
.cell.given.conflict { color: var(--conflict); }

.cell.border-right { border-right: 2px solid var(--line-strong); }
.cell.border-bottom { border-bottom: 2px solid var(--line-strong); }

.numpad[hidden], .actions[hidden], .pen-toolbar[hidden] {
  display: none;
}
.numpad {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 0.4rem;
  width: min(94vw, 460px);
}
.num-btn {
  padding: 0.75rem 0;
  font-size: 1.2rem;
  font-weight: 600;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.num-btn:active { background: var(--selected-bg); }
.erase-btn { color: var(--conflict); }

.pen-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  width: min(94vw, 460px);
}
.pen-list {
  display: flex;
  gap: 0.4rem;
  flex-wrap: wrap;
}
.pen-swatch {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}
.pen-swatch.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
}
.pen-dot {
  border-radius: 50%;
  display: block;
}
.tool-btn {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0;
}
.tool-btn.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
}
.pen-toolbar .action-btn {
  flex: 1 1 auto;
  min-width: 140px;
}

.pen-edit-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  margin-bottom: 1rem;
  max-height: 40vh;
  overflow-y: auto;
}
.pen-edit-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}
.pen-edit-row input[type="color"] {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  border: 1px solid var(--line);
  padding: 2px;
  background: var(--panel);
  flex: 0 0 auto;
}
.pen-edit-row input[type="range"] {
  flex: 1;
}
.pen-edit-row .width-label {
  width: 3.2em;
  text-align: right;
  font-size: 0.85rem;
  color: var(--muted);
  flex: 0 0 auto;
}
.icon-btn {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--conflict);
  cursor: pointer;
  flex: 0 0 auto;
}
.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}
.modal-title {
  margin: 0 0 0.75rem;
  font-size: 1.1rem;
}

.actions {
  display: flex;
  gap: 0.6rem;
  width: min(94vw, 460px);
  margin-top: 0.25rem;
}
.action-btn {
  flex: 1;
  padding: 0.7rem 0.5rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.action-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}
.action-btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.modal-overlay[hidden] {
  display: none;
}
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  z-index: 10;
}
.modal {
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 1.25rem;
  max-width: 360px;
  width: 100%;
}
.modal p { margin: 0 0 1rem; line-height: 1.4; }
.modal-hint {
  font-size: 0.85rem;
  color: var(--muted);
}
.modal-error {
  color: var(--conflict);
  font-size: 0.85rem;
}
.paste-textarea {
  width: 100%;
  min-height: 110px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  padding: 0.6rem;
  resize: vertical;
  box-sizing: border-box;
  margin-bottom: 0.75rem;
}
.modal-actions {
  display: flex;
  gap: 0.6rem;
}
.modal-actions .action-btn { flex: 1; }
