:root {
  --bg: #302721;
  --stage-paper: #9e907b;
  --panel: #3b3028;
  --line: #6b5543;
  --control-line: #b49980;
  --text: #faf0e1;
  --muted: #d7c3ad;
  --btn: #493b30;
  --btn-hover: #584637;
  --accent: #e7c599;
  --accent-hover: #f4d7b0;
  --on-accent: #302721;
  --accent-ring: rgba(231, 197, 153, 0.32);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif;
}

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 20px;
  padding: 12px clamp(12px, 3vw, 40px);
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.hud h1 {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 0.28em;
  font-size: clamp(1.05rem, 2.6vw, 1.45rem);
  font-weight: 800;
  letter-spacing: -0.035em;
}

.hud h1 img { margin-inline-end: 6px; }
.hud h1 span { color: var(--accent); }

.hud-right {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.counter {
  color: var(--muted);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}

.counter strong { color: var(--text); }

.stage {
  flex: 1;
  min-height: 0;
  position: relative;
  isolation: isolate;
  background-color: var(--stage-paper);
}

#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* 창살은 장면 좌우에만 두고, 안쪽으로 갈수록 한지색에 녹인다. */
.stage::before,
.stage::after {
  --lattice-ink: rgba(54, 37, 26, 0.22);
  --lattice-unit: clamp(20px, 2.8vw, 38px);
  content: '';
  position: absolute;
  z-index: 1;
  inset-block: 0;
  width: clamp(22px, 8%, 120px);
  pointer-events: none;
  background-image:
    linear-gradient(to right, var(--lattice-ink) 1px, transparent 0),
    linear-gradient(to bottom, var(--lattice-ink) 1px, transparent 0);
  background-position: 9px 0;
  background-size: calc(var(--lattice-unit) / 2) var(--lattice-unit);
  mask-image: linear-gradient(to right, #000, transparent);
}

.stage::before { inset-inline-start: 0; }
.stage::after { inset-inline-end: 0; transform: scaleX(-1); }

/* 좁은 화면에서 위쪽 양 구석에 보여 주는 숨은 면 (L·B) */
.face-peek {
  position: absolute;
  z-index: 2;
  top: 10px;
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  pointer-events: none; /* 아래 화면 드래그를 막지 않는다 */
}

.stage.is-narrow .face-peek { display: flex; }
.face-peek.left { left: 12px; }
.face-peek.right { right: 12px; }

.peek-label {
  font-size: 0.75rem;
  color: #282018;
}

.peek-label b {
  color: inherit;
  margin-right: 2px;
}

.peek-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 3px;
  width: clamp(72px, 24vw, 116px);
  aspect-ratio: 1;
  padding: 4px;
  border-radius: 9px;
  background: var(--panel);
  box-shadow: 0 0 0 1px var(--line);
}

.peek-grid span {
  border-radius: 3px;
  transition: background-color 0.25s;
}

/* ── 키 설정 다이얼로그 ─────────────────────────────────────── */
.settings {
  width: min(520px, calc(100vw - 32px));
  max-height: min(86dvh, 760px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
}

/* 열렸을 때만 레이아웃을 바꾼다 (닫힌 상태의 display:none을 덮지 않도록) */
.settings[open] { display: flex; }

/* 다이얼로그 안이나 배경에서 스크롤해도 뒤 페이지가 따라 움직이지 않게 */
.settings:modal { overscroll-behavior: contain; }
.settings::backdrop {
  overflow: hidden;
  overscroll-behavior: contain;
  background: rgba(29, 22, 17, 0.66);
  backdrop-filter: blur(2px);
}

.settings-form {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 0;
}

.settings-head,
.settings-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
}

.settings-head { border-bottom: 1px solid var(--line); }
.settings-foot { border-top: 1px solid var(--line); }

.settings-head h2 {
  margin: 0;
  font-size: 1rem;
}

.settings-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 16px 8px;
}

.settings-note {
  margin: 10px 0;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.5;
}

.settings h3 {
  margin: 16px 0 6px;
  font-size: 0.88rem;
}

.settings h3 small {
  margin-left: 6px;
  color: var(--muted);
  font-weight: 400;
}

.key-list {
  display: grid;
  gap: 4px;
}

.key-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 44px;
  padding: 2px 4px 2px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.02);
}

.key-row .action-name b {
  display: inline-block;
  min-width: 1.4em;
  margin-right: 4px;
}

.key-row .key-btn {
  min-width: 84px;
  min-height: 38px;
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.95rem;
}

.key-row.is-default .key-btn { color: var(--muted); }

.key-btn.is-listening {
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 0 0 3px var(--accent-ring);
  font-family: inherit;
  font-size: 0.8rem;
}

.key-row.is-changed {
  animation: key-changed 0.9s ease-out;
}

@keyframes key-changed {
  from { background: var(--accent-ring); }
}

.settings-status {
  min-height: 1.4em;
  margin: 12px 0 0;
  color: var(--accent);
  font-size: 0.82rem;
}

.btn.primary {
  min-height: 38px;
  padding: 0 18px;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  font-size: 0.9rem;
  font-weight: 600;
}

.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }

#scene.can-drag { cursor: grab; }
#scene.can-drag:active { cursor: grabbing; }

.panel {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 12px 16px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
}

.view-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  flex-wrap: wrap;
}

.dpad {
  display: flex;
  gap: 6px;
}

.btn {
  min-width: 44px;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--control-line);
  border-radius: 10px;
  background: var(--btn);
  color: var(--text);
  font: inherit;
  font-size: 1.05rem;
  cursor: pointer;
  transition: background 0.12s, transform 0.08s, opacity 0.15s, box-shadow 0.12s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn:hover:not(:disabled) { background: var(--btn-hover); }
.btn:active:not(:disabled) { transform: translateY(1px) scale(0.97); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: 0.32; cursor: not-allowed; }

.btn.ghost {
  min-height: 34px;
  font-size: 0.85rem;
  background: transparent;
}

.btn.icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  padding: 0 8px;
}

.btn.icon .mute { display: none; }
.btn.icon.is-muted { color: var(--muted); }
.btn.icon.is-muted .waves { display: none; }
.btn.icon.is-muted .mute { display: inline; }

.hint {
  margin: 10px 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.8rem;
  min-height: 1.2em;
}

.faces {
  display: grid;
  grid-template-columns: repeat(8, minmax(0, 1fr));
  gap: 10px;
  max-width: 980px;
  margin: 0 auto;
}

.face {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.face-name {
  font-size: 0.8rem;
  color: var(--muted);
  white-space: nowrap;
}

.face-name b {
  color: var(--text);
  font-size: 0.95rem;
  margin-right: 2px;
}

.pair {
  display: flex;
  gap: 6px;
  width: 100%;
}

.pair .btn {
  flex: 1;
  min-width: 0;
  min-height: 48px;
  padding: 0;
}

.pair .btn svg {
  display: block;
  width: 34px;
  height: 34px;
  margin: 0 auto;
}

.pair .btn text {
  fill: currentColor;
  font-size: 15px;
  font-weight: 700;
}

/* 키보드로 눌렀을 때 */
.btn.is-pressed:not(:disabled) {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
  transform: translateY(1px) scale(0.94);
  box-shadow: 0 0 0 3px var(--accent-ring);
}

/* ── 풀이 가이드 모드 ─────────────────────────────────────── */
.guide-nav,
.guide-detail { display: none; }

#guide-toggle[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

body.guide-mode {
  display: grid;
  grid-template-columns: 190px minmax(0, 1fr) minmax(320px, 420px);
  grid-template-rows: auto minmax(0, 1fr);
}

body.guide-mode .hud { grid-column: 1 / -1; }
body.guide-mode .panel,
body.guide-mode .play-only { display: none; }

body.guide-mode .guide-nav {
  display: block;
  overflow-y: auto;
  padding: 8px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-right: 1px solid var(--line);
}

.guide-nav ol {
  display: grid;
  gap: 4px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.guide-step {
  display: grid;
  gap: 2px;
  width: 100%;
  min-height: 48px;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.guide-step:hover { background: var(--btn); }
.guide-step:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.guide-step-no { font-size: 0.72rem; font-weight: 700; letter-spacing: 0.06em; }
.guide-step-title { font-size: 0.86rem; color: var(--text); }

.guide-step[aria-current="step"] {
  background: var(--btn);
  border-color: var(--accent);
}
.guide-step[aria-current="step"] .guide-step-no { color: var(--accent); }

body.guide-mode .guide-detail {
  display: block;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
  border-left: 1px solid var(--line);
  font-size: 0.88rem;
  line-height: 1.55;
}

.guide-kicker {
  margin: 0;
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.06em;
}

.guide-head h2 { margin: 2px 0 0; font-size: 1.15rem; }
.guide-section { margin-top: 20px; }

.guide-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.guide-section-head h3 { margin: 0; font-size: 0.92rem; }

.btn.small {
  min-width: 0;
  min-height: 32px;
  padding: 0 12px;
  font-size: 0.8rem;
}

.guide-goal { margin: 0; white-space: pre-line; }

.guide-legend {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 0.8rem;
}

.guide-dim-chip {
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: #3b3e47;
  box-shadow: 0 0 0 2px #07080a;
}

.guide-goal-input,
.algo-form input,
.algo-form textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--control-line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  line-height: 1.5;
  resize: vertical;
}

.guide-goal-input:focus-visible,
.algo-form input:focus-visible,
.algo-form textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.guide-actions {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 8px;
}

.algo-list { display: grid; gap: 10px; }

.algo {
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.02);
}

.algo h4 { margin: 0 0 6px; font-size: 0.9rem; }
.algo p { margin: 6px 0 0; }
.algo-condition b {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 600;
}
.algo-note { color: var(--muted); font-size: 0.82rem; }

.algo-moves {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin: 10px 0 0;
  padding: 0;
  list-style: none;
}

.algo-moves li {
  min-width: 32px;
  padding: 4px 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--btn);
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.92rem;
  font-weight: 700;
  text-align: center;
}


.guide-empty { margin: 0; color: var(--muted); }

.guide-notation { margin-top: 20px; color: var(--muted); font-size: 0.82rem; }
.guide-notation summary { cursor: pointer; }
.guide-notation dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 8px 0 0;
}
.guide-notation dt {
  color: var(--text);
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  white-space: nowrap;
}
.guide-notation dd { margin: 0; }
.guide-restore { margin-top: 16px; }

.algo-form { display: grid; gap: 12px; padding-top: 14px; }
.algo-form label { display: grid; gap: 4px; font-size: 0.84rem; font-weight: 600; }
.algo-form label small { color: var(--muted); font-weight: 400; }
.algo-form input[name="moves"] { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; }
.algo-error { margin: 0; color: #ff8a80; }

/* 좁은 화면: 단계 메뉴는 가로 줄, 설명은 큐브 아래 */
@media (max-width: 900px) {
  body.guide-mode {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(200px, 38%) minmax(0, 1fr);
  }
  body.guide-mode .guide-nav {
    overflow-x: auto;
    overflow-y: hidden;
    padding: 6px 8px;
    border-right: 0;
  }
  .guide-nav ol { display: flex; }
  .guide-step { width: max-content; min-height: 44px; padding: 5px 10px; }
  .guide-step-title { white-space: nowrap; }
  body.guide-mode .guide-detail { border-left: 0; }
}

.hud h1 { white-space: nowrap; }

@media (max-width: 560px) {
  .hud-right { width: 100%; }
  .hud .counter { margin-inline-end: auto; }
}

@media (max-width: 420px) {
  .hud { padding-inline: 12px; }
  .hud-right { gap: 6px; }
  .hud .btn.ghost:not(.icon) { padding: 0 9px; }
  .face-name {
    min-height: 2.4em;
    font-size: 0.7rem;
    line-height: 1.2;
    text-align: center;
    white-space: normal;
  }
  .face-name b { font-size: 0.8rem; }
  .pair .btn svg { max-width: 100%; }
}

@media (max-width: 780px) {
  .faces { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  .btn { transition: none; }
  .key-row.is-changed { animation: none; }
}
