@property --bw { syntax: '<length>'; inherits: true; initial-value: 0px; }
@property --bh { syntax: '<length>'; inherits: true; initial-value: 0px; }
@property --bd { syntax: '<length>'; inherits: true; initial-value: 0px; }

:root {
  --bg: #14161b;
  --panel: #1c1f26;
  --panel-raised: #23262f;
  --line: #2c303a;
  --ink: #eef0f4;
  --ink-dim: #8991a1;
  --accent: #ff8a3d;
  --accent-dim: #7a4b28;
  --fits: #57d98a;
  --tight: #ffcf4d;
  --check: #8a93ff;
  --nofit: #ff5f6e;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --status-color: var(--fits);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  overscroll-behavior-y: contain;
}

body { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: 18px 16px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* ---------- topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.wordmark {
  font-weight: 800;
  font-size: 15px;
  letter-spacing: 0.12em;
  color: var(--ink);
}
.wordmark-q { color: var(--accent); }

.unit-toggle {
  display: flex;
  background: var(--panel);
  border-radius: 999px;
  padding: 3px;
  gap: 2px;
}
.unit-btn {
  border: 0;
  background: transparent;
  color: var(--ink-dim);
  font-size: 13px;
  font-weight: 700;
  padding: 7px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.unit-btn.is-active { background: var(--accent); color: #1a1108; }

/* ---------- mode toggle ---------- */

.mode-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.mode-btn {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: var(--radius-md);
  padding: 10px 12px;
  color: var(--ink-dim);
  text-align: left;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.mode-btn.is-active {
  border-color: var(--accent);
  background: linear-gradient(180deg, rgba(255,138,61,0.14), rgba(255,138,61,0.04));
  color: var(--ink);
}
.mode-title { font-weight: 800; font-size: 13px; letter-spacing: 0.06em; }
.mode-sub { font-size: 11px; color: var(--ink-dim); }
.mode-btn.is-active .mode-sub { color: #d8ac82; }

/* ---------- 3D stage ---------- */

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

.stage {
  width: 100%;
  height: 240px;
  perspective: 900px;
  touch-action: none;
  cursor: grab;
  border-radius: var(--radius-lg);
  background: radial-gradient(120% 100% at 50% 15%, #23262f 0%, #181a20 70%);
  border: 1px solid var(--line);
  position: relative;
  overflow: hidden;
}
.stage:active { cursor: grabbing; }

.stage-hint { font-size: 11px; color: var(--ink-dim); letter-spacing: 0.04em; }

.autospin {
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  animation: autospin 70s linear infinite;
}
.autospin.is-paused { animation-play-state: paused; }
@keyframes autospin {
  from { transform: rotateY(0deg); }
  to   { transform: rotateY(360deg); }
}

.scene {
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  display: flex;
  align-items: center;
  justify-content: center;
}

.box {
  position: absolute;
  top: 50%;
  left: 50%;
  transform-style: preserve-3d;
  transform: translate3d(-50%, -50%, 0);
  transition: --bw 0.5s cubic-bezier(.2,.8,.2,1), --bh 0.5s cubic-bezier(.2,.8,.2,1), --bd 0.5s cubic-bezier(.2,.8,.2,1);
}

.box--object {
  transition: --bw 0.6s cubic-bezier(.34,1.56,.64,1), --bh 0.6s cubic-bezier(.34,1.56,.64,1), --bd 0.6s cubic-bezier(.34,1.56,.64,1), transform 0.6s cubic-bezier(.34,1.56,.64,1);
}

.face { position: absolute; }

.face--front, .face--back { width: var(--bw); height: var(--bh); left: calc(50% - var(--bw) / 2); top: calc(50% - var(--bh) / 2); }
.face--front  { transform: translateZ(calc(var(--bd) / 2)); }
.face--back   { transform: rotateY(180deg) translateZ(calc(var(--bd) / 2)); }

.face--left, .face--right { width: var(--bd); height: var(--bh); left: calc(50% - var(--bd) / 2); top: calc(50% - var(--bh) / 2); }
.face--right  { transform: rotateY(90deg) translateZ(calc(var(--bw) / 2)); }
.face--left   { transform: rotateY(-90deg) translateZ(calc(var(--bw) / 2)); }

.face--top, .face--bottom { width: var(--bw); height: var(--bd); left: calc(50% - var(--bw) / 2); top: calc(50% - var(--bd) / 2); }
.face--top    { transform: rotateX(90deg) translateZ(calc(var(--bh) / 2)); }
.face--bottom { transform: rotateX(-90deg) translateZ(calc(var(--bh) / 2)); }

.box--space .face {
  background: rgba(138, 147, 255, 0.03);
  border: 1.5px dashed rgba(138, 147, 255, 0.35);
}

.box--object .face {
  background: linear-gradient(160deg, rgba(255,138,61,0.55), rgba(255,138,61,0.28));
  border: 1px solid rgba(255, 184, 130, 0.65);
}
.box--object .face--top { background: rgba(255,166,105,0.65); }
.box--object .face--bottom { background: rgba(180,90,30,0.55); }

#result[data-status="NO_FIT"] .box--object .face {
  background: linear-gradient(160deg, rgba(255,95,110,0.5), rgba(255,95,110,0.25));
  border-color: rgba(255,150,160,0.6);
}
#result[data-status="CHECK"] .box--object .face {
  background: linear-gradient(160deg, rgba(138,147,255,0.5), rgba(138,147,255,0.25));
  border-color: rgba(180,186,255,0.6);
}
#result[data-status="FITS"] .box--object .face,
#result[data-status="TIGHT"] .box--object .face {
  background: linear-gradient(160deg, rgba(87,217,138,0.5), rgba(87,217,138,0.25));
  border-color: rgba(150,235,180,0.6);
}

/* ---------- result ---------- */

.result[data-status="FITS"] { --status-color: var(--fits); }
.result[data-status="TIGHT"] { --status-color: var(--tight); }
.result[data-status="CHECK"] { --status-color: var(--check); }
.result[data-status="NO_FIT"] { --status-color: var(--nofit); }

.result {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px 18px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.result-status {
  font-size: 44px;
  font-weight: 900;
  letter-spacing: 0.02em;
  color: var(--status-color);
  transition: color 0.3s ease;
  line-height: 1.05;
}

.result-detail {
  font-size: 15px;
  color: var(--ink-dim);
  font-weight: 600;
}

.result-checks {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin-top: 6px;
  font-size: 13px;
  font-weight: 600;
}
.check-line.is-good { color: var(--fits); }
.check-line.is-bad { color: var(--nofit); }

.rotate-btn {
  margin-top: 12px;
  border: none;
  background: var(--accent);
  color: #1a1108;
  font-weight: 800;
  font-size: 14px;
  letter-spacing: 0.04em;
  padding: 14px 22px;
  border-radius: 999px;
  cursor: pointer;
  animation: pulse 1.6s ease-in-out infinite;
}
.rotate-icon { display: inline-block; }
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,138,61,0.5); }
  50% { box-shadow: 0 0 0 10px rgba(255,138,61,0); }
}

/* ---------- panels ---------- */

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 16px;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}
.panel-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: var(--ink-dim);
}
.text-btn {
  border: none;
  background: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  padding: 4px 6px;
}

.dims {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.dim { display: flex; flex-direction: column; gap: 6px; position: relative; }
.dim label {
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--ink-dim);
  text-align: center;
}

.dim-control {
  display: flex;
  align-items: center;
  background: var(--panel-raised);
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 1px solid var(--line);
}
.dim-step {
  border: none;
  background: transparent;
  color: var(--ink-dim);
  font-size: 20px;
  font-weight: 700;
  width: 34px;
  height: 52px;
  cursor: pointer;
  flex-shrink: 0;
}
.dim-step:active { background: rgba(255,255,255,0.06); }

.dim-input {
  width: 100%;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--ink);
  font-size: 22px;
  font-weight: 800;
  text-align: center;
  padding: 0;
  font-variant-numeric: tabular-nums;
}
.dim-input:focus { outline: none; color: var(--accent); }

.unlimited-btn {
  border: 1px dashed var(--line);
  background: transparent;
  color: var(--ink-dim);
  font-size: 10px;
  font-weight: 700;
  border-radius: 999px;
  padding: 4px 0;
  cursor: pointer;
  letter-spacing: 0.02em;
}
.unlimited-btn.is-active { border-color: var(--check); color: var(--check); background: rgba(138,147,255,0.1); }

.saved-row {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}
.saved-chip {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel-raised);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 10px 7px 14px;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  white-space: nowrap;
}
.saved-chip-remove {
  border: none;
  background: rgba(255,255,255,0.08);
  color: var(--ink-dim);
  width: 18px;
  height: 18px;
  border-radius: 50%;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

/* ---------- minimum opening ---------- */

.min-opening {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 12px 16px;
  font-size: 12px;
}
.min-opening-label { color: var(--ink-dim); font-weight: 700; letter-spacing: 0.02em; }
.min-opening-value { color: var(--ink); font-weight: 800; font-variant-numeric: tabular-nums; }

/* ---------- actions ---------- */

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.action-btn {
  border: none;
  background: var(--accent);
  color: #1a1108;
  font-weight: 800;
  font-size: 14px;
  padding: 15px;
  border-radius: var(--radius-md);
  cursor: pointer;
}
.action-btn--ghost {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--ink-dim);
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--panel-raised);
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 20;
}

@media (min-width: 520px) {
  .app {
    width: min(92vw, 820px);
    max-width: 820px;
    padding: 28px 24px 40px;
    gap: 18px;
  }

  .drop-zone {
    min-height: 420px;
  }

  .result-status {
    font-size: 50px;
  }
}

/* ---------- license / entitlement ---------- */
.license-panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius-md);
  padding: 11px 12px; display: grid; gap: 9px;
}
.license-head { display:flex; align-items:flex-start; justify-content:space-between; gap:10px; }
.license-title-row { display:flex; align-items:center; gap:8px; margin-bottom:3px; }
.license-title { font-size:11px; font-weight:900; letter-spacing:.08em; }
.tier-badge { font-size:10px; line-height:1; font-weight:900; letter-spacing:.08em; padding:5px 7px; border-radius:999px; background:var(--panel-raised); color:var(--ink-dim); border:1px solid var(--line); }
.tier-badge.is-pro { color:var(--accent-ink, #111); background:var(--accent); border-color:var(--accent); }
.license-copy { font-size:11px; line-height:1.35; color:var(--ink-dim); }
.license-form { display:grid; grid-template-columns:minmax(0,1fr) auto; gap:7px; }
.license-input { min-width:0; background:var(--panel-raised); border:1px solid var(--line); color:var(--ink); border-radius:10px; padding:9px 10px; font-size:12px; }
.license-input:focus { outline:none; border-color:var(--accent); }
.license-button { border:0; border-radius:10px; background:var(--accent); color:var(--accent-ink, #111); font-weight:900; padding:9px 12px; cursor:pointer; }
.license-button:disabled { opacity:.5; cursor:wait; }
.license-foot { min-height:18px; display:flex; align-items:center; gap:10px; flex-wrap:wrap; }
.license-message { font-size:10px; color:var(--ink-dim); flex:1; }
.license-link, .license-forget { font:inherit; font-size:10px; font-weight:800; color:var(--accent); background:none; border:0; padding:0; cursor:pointer; text-decoration:none; }
.license-panel.is-nudge { border-color:var(--accent); box-shadow:0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent); }
.pro-locked-note { width:100%; border:1px dashed var(--line); background:transparent; color:var(--ink-dim); border-radius:10px; padding:9px 11px; font-size:11px; font-weight:800; cursor:pointer; }

.min-opening.is-locked { cursor:pointer; border-style:dashed; opacity:.9; }
.min-opening.is-locked .min-opening-value { color:var(--accent); font-weight:900; }
