:root {
  --bg: #ebe3d2;
  --bg-outer: #ddd2be;
  --surface: #f7f2e6;
  --surface-elev: #fbf8f0;
  --ink: #221e1a;
  --ink-muted: #6c6154;
  --ink-line: rgba(34, 30, 26, 0.14);
  --wood: #3a2c20;
  --wood-inner: #1d1814;
  --hanji: #f5efe0;
  --hanji-back: #e7dec8;
  --accent: #b83b2a;
  --accent-fg: #fbf7f0;
  --gold: #c48e2c;
  --kit-bg: #1e1915;
  --kit-sheet: #241e19;
  --kit-fg: #f4eee1;
  --font-serif: "AppleMyungjo", "Batang", "Noto Serif KR", Georgia, serif;
  --font-sans: system-ui, -apple-system, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
}

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

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg-outer);
  color: var(--ink);
  font-family: var(--font-sans);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

/* Subtle architectural hanji backdrop on wide desktop screens */
body {
  display: flex;
  justify-content: center;
  align-items: stretch;
  background-image:
    radial-gradient(circle at 20% 15%, rgba(255, 255, 255, 0.25), transparent 55%),
    repeating-linear-gradient(
      90deg,
      rgba(34, 30, 26, 0.02) 0px,
      rgba(34, 30, 26, 0.02) 1px,
      transparent 1px,
      transparent 32px
    );
}

.app {
  width: 100%;
  max-width: 480px;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px calc(env(safe-area-inset-bottom, 0px) + 34px);
  gap: 8px;
  position: relative;
  box-shadow: 0 0 40px rgba(24, 18, 12, 0.14);
}

.hidden {
  display: none !important;
}

/* Top bar: reserve right space for Kit's fixed gear button on mobile widths */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 46px;
  padding-right: 52px;
  flex-shrink: 0;
}

@media (min-width: 600px) {
  .app {
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
  }
  .topbar {
    padding-right: 0;
  }
}

.brand-group {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-shrink: 0;
}

.brand-title {
  font-family: var(--font-serif);
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--ink);
  line-height: 1;
  white-space: nowrap;
}

.seal {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  border: 1.5px solid var(--accent);
  border-radius: 3px;
  color: var(--accent);
  font-family: var(--font-serif);
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  transform: rotate(-3deg);
}

.stage-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 44px;
  padding: 0 11px;
  border-radius: 10px;
  border: 1px solid var(--ink-line);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  max-width: 170px;
  overflow: hidden;
  touch-action: manipulation;
}

.stage-pill:active {
  transform: scale(0.97);
}

.stage-num {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  flex-shrink: 0;
}

.stage-name {
  font-family: var(--font-serif);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.stage-caret {
  font-size: 10px;
  color: var(--ink-muted);
  flex-shrink: 0;
}

.top-tools {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  min-width: 44px;
  min-height: 44px;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--ink-line);
  background: var(--surface);
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-size: 16px;
  touch-action: manipulation;
}

.icon-btn:active {
  transform: scale(0.95);
}

/* HUD strip */
.hud-strip {
  display: flex;
  align-items: stretch;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--ink-line);
  border-radius: 14px;
  padding: 8px 10px;
  flex-shrink: 0;
}

.target-card {
  width: 82px;
  min-height: 82px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 5px;
  border-radius: 10px;
  border: 1.5px solid var(--wood);
  background: var(--wood-inner);
  cursor: pointer;
  touch-action: manipulation;
}

.target-card:active {
  transform: scale(0.97);
}

.target-mini {
  width: 62px;
  height: 62px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.target-mini svg {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 2px;
}

.target-cap {
  font-size: 10px;
  font-weight: 600;
  color: rgba(245, 239, 224, 0.82);
  letter-spacing: 0.02em;
  line-height: 1;
}

.hud-meta {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 5px;
}

.phase-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.phase-badge {
  display: inline-flex;
  align-items: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  background: rgba(184, 59, 42, 0.1);
  border: 1px solid rgba(184, 59, 42, 0.24);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.phase-badge.phase-cut {
  color: var(--wood);
  background: rgba(58, 44, 32, 0.1);
  border-color: rgba(58, 44, 32, 0.24);
}

.phase-badge.phase-result {
  color: #8a5e14;
  background: rgba(196, 142, 44, 0.16);
  border-color: rgba(196, 142, 44, 0.35);
}

.best-stars {
  font-size: 14px;
  letter-spacing: 1px;
  color: var(--gold);
  line-height: 1;
}

.counters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.counter-box {
  background: var(--surface-elev);
  border: 1px solid var(--ink-line);
  border-radius: 8px;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.counter-box.active {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px rgba(184, 59, 42, 0.2);
}

.counter-label {
  font-size: 11px;
  color: var(--ink-muted);
  font-weight: 600;
}

.counter-val {
  font-size: 13px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.guide-text {
  margin: 0;
  font-size: 12px;
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Main paper workspace */
.workspace {
  flex: 1;
  min-height: 0;
  width: 100%;
  container-type: size;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px 0;
}

.lattice-frame {
  position: relative;
  width: min(100cqw, 100cqh);
  height: min(100cqw, 100cqh);
  border-radius: 14px;
  background: var(--wood-inner);
  border: 4px solid var(--wood);
  box-shadow:
    inset 0 0 28px rgba(0, 0, 0, 0.55),
    0 6px 20px rgba(34, 24, 14, 0.18);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#paper-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: grab;
}

#paper-canvas.mode-cut {
  cursor: crosshair;
}

#paper-canvas.dragging {
  cursor: grabbing;
}

.layer-badge {
  position: absolute;
  top: 8px;
  left: 8px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(24, 20, 16, 0.72);
  border: 1px solid rgba(245, 239, 224, 0.22);
  color: #f5efe0;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
  backdrop-filter: blur(4px);
}

.swipe-label {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  padding: 5px 12px;
  border-radius: 999px;
  background: rgba(184, 59, 42, 0.92);
  color: #fffaf2;
  font-size: 12px;
  font-weight: 700;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
}

.compare-legend {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  gap: 8px;
  padding: 4px 9px;
  border-radius: 999px;
  background: rgba(24, 20, 16, 0.78);
  color: #f5efe0;
  font-size: 11px;
  font-weight: 600;
  pointer-events: none;
}

.leg-item {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.leg-dot {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  display: inline-block;
}

.leg-miss {
  border: 1.5px dashed #ff8c66;
  background: rgba(255, 140, 102, 0.25);
}

.leg-extra {
  background: rgba(196, 59, 42, 0.72);
}

/* Subbar: consistent 48px height across all phases */
.subbar {
  min-height: 48px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
}

.fold-bar {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.fold-btn {
  min-height: 44px;
  border-radius: 10px;
  border: 1px solid var(--ink-line);
  background: var(--surface);
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 0 4px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.fold-btn:active:not(:disabled) {
  transform: scale(0.96);
  background: var(--surface-elev);
}

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

.cut-bar, .result-bar {
  width: 100%;
  min-height: 46px;
  padding: 0 10px;
  border-radius: 12px;
  background: var(--surface);
  border: 1px solid var(--ink-line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.cut-tip {
  font-size: 12px;
  color: var(--ink-muted);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.crease-swatch {
  width: 14px;
  height: 0;
  border-top: 2px dashed var(--accent);
  display: inline-block;
  flex-shrink: 0;
}

.result-score {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.res-stars {
  font-size: 18px;
  color: var(--gold);
  letter-spacing: 1px;
  line-height: 1;
}

.res-pct {
  font-size: 17px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.res-verdict {
  font-size: 12px;
  font-weight: 600;
  color: var(--ink-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip-btn {
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px;
  border-radius: 9px;
  border: 1px solid var(--ink-line);
  background: var(--surface-elev);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
  touch-action: manipulation;
  flex-shrink: 0;
}

.chip-btn[aria-pressed="true"] {
  background: var(--wood);
  color: #f5efe0;
  border-color: var(--wood);
}

/* Bottom action bar */
.actions {
  display: grid;
  grid-template-columns: 1fr 0.9fr 1.5fr;
  gap: 8px;
  flex-shrink: 0;
}

.act-btn {
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--ink-line);
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s ease, background 0.18s ease, opacity 0.15s ease;
}

.act-btn:active:not(:disabled) {
  transform: scale(0.97);
}

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

.act-btn.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: transparent;
  box-shadow: 0 3px 10px rgba(184, 59, 42, 0.28);
}

.act-btn.primary.ready-pulse {
  animation: gentle-pulse 1.5s ease-in-out infinite;
}

@keyframes gentle-pulse {
  0%, 100% { box-shadow: 0 3px 10px rgba(184, 59, 42, 0.28); }
  50% { box-shadow: 0 3px 18px rgba(184, 59, 42, 0.58); }
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 90000;
  background: rgba(20, 16, 12, 0.62);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  backdrop-filter: blur(3px);
}

.modal-card {
  width: 100%;
  max-width: 380px;
  max-height: 84vh;
  overflow-y: auto;
  background: var(--surface);
  color: var(--ink);
  border: 1px solid var(--ink-line);
  border-radius: 16px;
  padding: 14px 16px 16px;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.36);
}

.stage-modal-card {
  max-width: 420px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.modal-title {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 19px;
  font-weight: 700;
}

.modal-sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--ink-muted);
}

.target-large {
  width: min(260px, 72vw);
  height: min(260px, 72vw);
  margin: 0 auto 12px;
  border: 4px solid var(--wood);
  border-radius: 10px;
  background: var(--wood-inner);
  overflow: hidden;
  padding: 6px;
}

.target-large svg {
  width: 100%;
  height: 100%;
  display: block;
}

.modal-tools {
  display: flex;
  justify-content: center;
  gap: 8px;
}

.hint-box {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(184, 59, 42, 0.08);
  border: 1px solid rgba(184, 59, 42, 0.2);
  font-size: 13px;
  line-height: 1.45;
  color: var(--ink);
  text-align: center;
}

/* Stage grid */
.stage-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stage-tile {
  min-height: 104px;
  padding: 8px 6px 6px;
  border-radius: 12px;
  border: 1px solid var(--ink-line);
  background: var(--surface-elev);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  touch-action: manipulation;
}

.stage-tile.current {
  border: 2px solid var(--accent);
  background: rgba(184, 59, 42, 0.06);
}

.stage-tile:active {
  transform: scale(0.97);
}

.tile-svg {
  width: 48px;
  height: 48px;
  border-radius: 4px;
  border: 2px solid var(--wood);
  background: var(--wood-inner);
  padding: 2px;
}

.tile-svg svg {
  width: 100%;
  height: 100%;
  display: block;
}

.tile-title {
  font-family: var(--font-serif);
  font-size: 12px;
  font-weight: 700;
  line-height: 1.1;
}

.tile-meta {
  font-size: 10px;
  color: var(--ink-muted);
}

.tile-stars {
  font-size: 12px;
  color: var(--gold);
  letter-spacing: 1px;
  line-height: 1;
}

/* Narrow 320px phone tuning */
@media (max-width: 350px) {
  .app {
    padding: calc(env(safe-area-inset-top, 0px) + 6px) 8px calc(env(safe-area-inset-bottom, 0px) + 34px);
    gap: 6px;
  }
  .brand-title {
    font-size: 17px;
  }
  .stage-pill {
    padding: 0 8px;
    font-size: 12px;
    max-width: 132px;
  }
  .target-card {
    width: 72px;
    min-height: 72px;
  }
  .target-mini {
    width: 54px;
    height: 54px;
  }
  .fold-btn {
    font-size: 11px;
    gap: 2px;
  }
  .fold-btn svg {
    width: 15px;
    height: 15px;
  }
  .act-btn {
    font-size: 13px;
    gap: 4px;
  }
  .res-verdict {
    display: none;
  }
}

/* Short viewport tuning */
@media (max-height: 640px) {
  .app {
    gap: 5px;
  }
  .hud-strip {
    padding: 6px 8px;
  }
  .target-card {
    width: 68px;
    min-height: 68px;
  }
  .target-mini {
    width: 50px;
    height: 50px;
  }
  .act-btn {
    min-height: 44px;
  }
}
