:root {
  color-scheme: light;
  --ink: #17211d;
  --muted: #6e7771;
  --paper: #f6f1e8;
  --panel: #fffdf8;
  --sage: #2f6f5e;
  --sage-soft: #dce9e3;
  --coral: #d86b52;
  --line: #c7c9c2;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  min-height: 100dvh;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 20% 0%, #fffdf8 0 22%, transparent 45%),
    linear-gradient(145deg, #f7f1e8, #e8efe9);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Yu Gothic UI", "Hiragino Sans", sans-serif;
}
.loading { position: fixed; inset: 0; z-index: 30; display: grid; place-content: center; justify-items: center; gap: 14px; background: var(--paper); color: var(--ink); }
.loading[hidden] { display: none; }
.loading span { width: 38px; height: 38px; border: 3px solid #cad8d1; border-top-color: var(--sage); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
button, select { font: inherit; }
button { touch-action: manipulation; }

.app {
  width: min(100%, 660px);
  min-height: 100dvh;
  margin: auto;
  padding: max(18px, env(safe-area-inset-top)) clamp(12px, 4vw, 28px) max(18px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.topbar { display: flex; align-items: end; justify-content: space-between; gap: 16px; }
.eyebrow { margin: 0 0 2px; color: var(--coral); font-size: .7rem; font-weight: 800; letter-spacing: .16em; }
h1 { margin: 0; font-family: Georgia, "Yu Mincho", serif; font-size: clamp(2rem, 8vw, 3.4rem); font-weight: 500; letter-spacing: .06em; }
.ghost, .primary {
  min-height: 42px;
  border: 1px solid #aeb6b0;
  border-radius: 999px;
  padding: 8px 16px;
  background: rgba(255,255,255,.55);
  color: var(--ink);
  cursor: pointer;
}
.primary { border-color: var(--sage); background: var(--sage); color: white; }
.status {
  margin: 16px 0 4px;
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 8px;
  align-items: end;
}
.status label, .status div { color: var(--muted); font-size: .74rem; }
.status div { text-align: right; }
.status span { display: block; }
.status strong { color: var(--ink); font-size: .9rem; }
select { width: 100%; margin-top: 4px; border: 0; border-bottom: 1px solid #aeb6b0; background: transparent; color: var(--ink); padding: 5px 0; }
.message { min-height: 1.4em; margin: 5px 0 8px; color: var(--muted); font-size: .84rem; }

.board {
  width: min(100%, 540px, calc(100dvh - 320px));
  aspect-ratio: 1;
  align-self: center;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  border: 3px solid var(--ink);
  border-radius: 10px;
  overflow: hidden;
  background: var(--panel);
  box-shadow: 0 18px 45px rgba(38,55,48,.13);
}
.cell {
  position: relative;
  min-width: 0;
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  border-radius: 0;
  background: transparent;
  color: #245f51;
  font-size: clamp(.95rem, 4vw, 1.8rem);
  font-weight: 650;
  cursor: pointer;
}
.cell:nth-child(9n) { border-right: 0; }
.cell:nth-child(n+73) { border-bottom: 0; }
.cell.box-right { border-right: 2px solid var(--ink); }
.cell.box-bottom { border-bottom: 2px solid var(--ink); }
.cell.given { color: var(--ink); font-weight: 800; }
.cell.peer { background: #eff2ec; }
.cell.same { background: #e6eee8; }
.cell.selected { background: var(--sage-soft); box-shadow: inset 0 0 0 2px var(--sage); }
.cell.error { color: #b23b2c; background: #f8ded7; animation: shake .18s linear 2; }
.notes-grid { position: absolute; inset: 2px; display: grid; grid-template-columns: repeat(3, 1fr); color: #517165; font-size: clamp(.38rem, 1.7vw, .7rem); font-weight: 700; }
.notes-grid i { display: grid; place-items: center; font-style: normal; }
@keyframes shake { 50% { transform: translateX(2px); } }

.tools, .number-pad { display: grid; gap: 7px; margin-top: 12px; }
.tools { grid-template-columns: repeat(4, 1fr); }
.tools button, .number-pad button {
  min-height: 48px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: rgba(255,255,255,.64);
  color: var(--ink);
  cursor: pointer;
}
.tools button span { display: block; color: var(--sage); font-size: 1.25rem; }
.tools button[aria-pressed="true"] { border-color: var(--sage); background: var(--sage-soft); }
.tools button:disabled { opacity: .38; cursor: default; }
.number-pad { grid-template-columns: repeat(9, 1fr); }
.number-pad button { min-width: 0; padding: 0; color: var(--sage); font-size: clamp(1rem, 4vw, 1.5rem); font-weight: 800; }

.modal { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 20px; background: rgba(20,29,25,.5); }
.modal[hidden] { display: none; }
.dialog { width: min(100%, 420px); padding: 26px; border-radius: 20px; background: var(--panel); box-shadow: 0 24px 80px rgba(0,0,0,.25); }
.dialog h2 { margin: 0 0 8px; font-family: Georgia, "Yu Mincho", serif; font-size: 1.8rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

button:focus-visible, select:focus-visible { outline: 3px solid #e19b43; outline-offset: 2px; }
body.paused::after { content: "一時停止中"; position: fixed; inset: 0; z-index: 20; display: grid; place-items: center; background: rgba(246,241,232,.92); color: var(--ink); font-size: 1.4rem; font-weight: 800; }

@media (max-height: 800px) and (orientation: landscape) {
  .app { width: min(100%, 940px); display: grid; grid-template-columns: minmax(0, 1fr) min(92vh, 470px); grid-template-rows: auto auto auto 1fr; column-gap: 20px; }
  .topbar, .status, .message, .tools, .number-pad { grid-column: 1; }
  .board { grid-column: 2; grid-row: 1 / 7; width: min(92vh, 470px); }
  .tools { align-self: end; }
}

@media (max-width: 360px) {
  .app { justify-content: flex-start; }
  .ghost { padding-inline: 10px; font-size: .78rem; }
  .tools button { min-height: 44px; padding: 4px 2px; font-size: .7rem; }
  .number-pad { gap: 3px; }
}
