:root {
  --paper: #f4f1ea;
  --canvas: #ffffff;
  --preview: #eeeae1;
  --ink: #1e211f;
  --muted: #7a7568;
  --accent: #c96a3a;
  --accent-dark: #a04d24;
  --rule: #d9d3c5;
  --rule-strong: #b8b1a2;
  --shadow: 0 12px 28px rgba(30, 33, 31, 0.08);
  --frame-shadow: 0 24px 60px rgba(30, 33, 31, 0.16);
  --topbar: 60px;
  --rail: 280px;
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);
  color-scheme: light;
}

[data-theme='dark'] {
  --paper: #1a1c1a;
  --canvas: #23262a;
  --preview: #14161a;
  --ink: #efe9dc;
  --muted: #9d968a;
  --accent: #d97c48;
  --accent-dark: #ee9e6f;
  --rule: #333833;
  --rule-strong: #4a4f4a;
  --shadow: 0 14px 30px rgba(0, 0, 0, 0.45);
  --frame-shadow: 0 24px 60px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: 'DM Sans', system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button { border: 0; background: transparent; color: inherit; font: inherit; cursor: pointer; }
a { color: inherit; text-decoration: none; }
input[type='search'] { font: inherit; color: inherit; }
input[type='search']::-webkit-search-cancel-button { display: none; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Shell */
.vault-shell {
  display: grid;
  grid-template-rows: var(--topbar) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
}

/* Topbar */
.topbar {
  display: grid;
  grid-template-columns: auto auto minmax(200px, 480px) 1fr;
  align-items: center;
  gap: 14px;
  padding: 0 20px 0 14px;
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  margin-right: 12px;
  font-family: 'Montserrat', 'DM Sans', sans-serif;
  font-weight: 800;
  font-size: 14px;
  letter-spacing: -0.02em;
}
.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--ink);
  color: var(--paper);
  font-size: 11px;
}
.search-field {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px 7px 12px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  background: var(--canvas);
}
.search-field:focus-within { border-color: var(--rule-strong); }
.search-field iconify-icon { color: var(--muted); font-size: 15px; }
.search-field input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: transparent;
  outline: 0;
  font-size: 13px;
}
.kbd {
  padding: 1px 6px;
  border: 1px solid var(--rule);
  border-radius: 4px;
  color: var(--muted);
  font-family: inherit;
  font-size: 11px;
  font-weight: 700;
}
.topbar-actions {
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}
.library-count {
  padding: 4px 10px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}
.icon-button {
  display: grid;
  place-items: center;
  flex: none;
  width: 34px;
  height: 34px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  color: var(--ink);
  transition: border-color 0.15s ease, color 0.15s ease, background 0.15s ease;
}
.icon-button:hover { border-color: var(--ink); }
.icon-button:active { transform: scale(0.96); }
.icon-button iconify-icon { font-size: 16px; }
.icon-button[aria-pressed='true'] { color: var(--accent-dark); border-color: var(--accent); }

/* Main */
.vault-main {
  display: grid;
  grid-template-columns: var(--rail) minmax(0, 1fr);
  min-height: 0;
  transition: grid-template-columns 0.24s var(--ease-out);
}
.rail-collapsed .vault-main { grid-template-columns: 0 minmax(0, 1fr); }

/* Sidebar tree */
.library-sidebar {
  min-width: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 8px 0 40px;
  border-right: 1px solid var(--rule);
  background: var(--paper);
}
.rail-collapsed .library-sidebar { visibility: hidden; border-right: 0; }
#library-tree { width: var(--rail); }
.side-label {
  margin: 18px 20px 6px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.tree-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 16px 7px 14px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
}
.tree-toggle:hover { background: var(--canvas); }
.tree-toggle.has-current .tree-label { color: var(--accent-dark); }
.tree-caret {
  flex: none;
  color: var(--muted);
  font-size: 12px;
  transition: transform 0.18s var(--ease-out);
}
.tree-toggle[aria-expanded='true'] .tree-caret { transform: rotate(90deg); }
.is-favorites .tree-caret { visibility: hidden; }
.tree-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-badge {
  min-width: 24px;
  padding: 1px 7px;
  border: 1px solid var(--rule);
  border-radius: 999px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  text-align: center;
}
.tree-pieces {
  display: flex;
  flex-direction: column;
  margin: 2px 0 8px 21px;
  border-left: 1px solid var(--rule);
}
.tree-piece {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: -1px;
  padding: 6px 14px 6px 12px;
  border-left: 2px solid transparent;
  color: var(--muted);
  font-size: 12.5px;
  text-align: left;
}
.tree-piece:hover { color: var(--ink); background: var(--canvas); }
.tree-piece.is-active {
  border-left-color: var(--accent);
  background: var(--canvas);
  color: var(--ink);
  font-weight: 600;
}
.tree-code {
  flex: none;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}
.tree-piece.is-active .tree-code { color: var(--accent-dark); }
.tree-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-piece iconify-icon { flex: none; color: var(--accent); font-size: 11px; }
.tree-empty {
  display: grid;
  gap: 10px;
  justify-items: start;
  padding: 24px 20px;
  color: var(--muted);
}
.tree-empty p { margin: 0; }
.tree-empty button {
  padding: 7px 14px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
}
.tree-empty button:hover { border-color: var(--ink); }
.sidebar-scrim { display: none; }

/* Stage */
.stage {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  background: var(--preview);
}
.stage-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--rule);
  background: var(--paper);
}
.stage-title { min-width: 0; }
.stage-crumb {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0 0 2px;
  color: var(--accent-dark);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.stage-crumb iconify-icon { color: var(--muted); font-size: 10px; }
.stage-title h1 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0;
  font-family: 'Montserrat', 'DM Sans', sans-serif;
  font-size: 19px;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.2;
}
.stage-title h1 > span:last-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stage-desc {
  margin: 3px 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 12.5px;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.piece-code {
  display: inline-block;
  flex: none;
  padding: 2px 7px;
  border: 1px solid var(--rule-strong);
  border-radius: 4px;
  background: var(--preview);
  color: var(--ink);
  font-family: 'DM Sans', ui-monospace, monospace;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
}
.stage-tools {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: none;
}
.segmented {
  display: inline-flex;
  margin-right: 6px;
  padding: 2px;
  border: 1px solid var(--rule);
  border-radius: 8px;
}
.segmented button {
  display: grid;
  place-items: center;
  width: 30px;
  height: 28px;
  border-radius: 6px;
  color: var(--muted);
  transition: background 0.15s ease, color 0.15s ease;
}
.segmented button:hover { color: var(--ink); }
.segmented button[aria-pressed='true'] { background: var(--ink); color: var(--paper); }
.segmented iconify-icon { font-size: 16px; }

/* Canvas + device frame */
.stage-canvas {
  position: relative;
  display: flex;
  justify-content: center;
  min-height: 0;
  overflow: hidden;
  transition: padding 0.24s var(--ease-out), background 0.2s ease;
}
.stage-canvas[data-backdrop='paper'] { background: var(--preview); }
.stage-canvas[data-backdrop='dark'] { background: #111312; }
.stage-canvas[data-backdrop='grid'] {
  background-color: var(--preview);
  background-image:
    linear-gradient(var(--rule) 1px, transparent 1px),
    linear-gradient(90deg, var(--rule) 1px, transparent 1px);
  background-size: 24px 24px;
}
.device-frame {
  position: relative;
  width: 100%;
  height: 100%;
  background: #fff;
  transition: width 0.28s var(--ease-out), border-radius 0.28s var(--ease-out);
}
.stage-canvas[data-device='tablet'],
.stage-canvas[data-device='mobile'] { padding: 24px; }
.stage-canvas[data-device='tablet'] .device-frame { width: min(834px, 100%); }
.stage-canvas[data-device='mobile'] .device-frame { width: min(390px, 100%); }
.stage-canvas[data-device='tablet'] .device-frame,
.stage-canvas[data-device='mobile'] .device-frame {
  overflow: hidden;
  border-radius: 14px;
  box-shadow: 0 0 0 1px var(--rule-strong), var(--frame-shadow);
}
.piece-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}
.piece-frame.is-current {
  z-index: 1;
  opacity: 1;
  pointer-events: auto;
}
.frame-loading {
  position: absolute;
  z-index: 2;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  overflow: hidden;
}
.frame-loading span {
  display: block;
  width: 40%;
  height: 100%;
  background: var(--accent);
  animation: loading-slide 0.9s ease-in-out infinite;
}
@keyframes loading-slide {
  from { transform: translateX(-100%); }
  to { transform: translateX(250%); }
}

/* Footer stepper */
.stage-foot {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 16px;
  padding: 8px 12px;
  border-top: 1px solid var(--rule);
  background: var(--paper);
}
.step-button {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 6px 10px;
  border-radius: 8px;
  text-align: left;
  transition: background 0.15s ease;
}
.step-button:hover { background: var(--canvas); }
.step-button:active { transform: scale(0.98); }
.step-button.is-next { justify-self: end; text-align: right; }
.step-button > iconify-icon { flex: none; font-size: 16px; }
.step-text { display: grid; min-width: 0; }
.step-text small {
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.step-text span {
  overflow: hidden;
  font-size: 12.5px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.stage-position {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* Focus mode: only the piece, with a HUD that appears on movement */
.vault-shell.is-focus { grid-template-rows: minmax(0, 1fr); }
.is-focus .topbar,
.is-focus .stage-head,
.is-focus .stage-foot,
.is-focus .library-sidebar,
.is-focus .sidebar-scrim { display: none !important; }
.vault-shell.is-focus .vault-main { grid-template-columns: minmax(0, 1fr); }
.is-focus .stage { grid-template-rows: minmax(0, 1fr); }
.focus-hud {
  position: fixed;
  z-index: 40;
  left: 50%;
  bottom: 20px;
  display: none;
  align-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 12px;
  background: rgba(20, 22, 20, 0.86);
  color: #efe9dc;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(10px);
  opacity: 0;
  transform: translate(-50%, 8px);
  transition: opacity 0.2s ease, transform 0.2s var(--ease-out);
  pointer-events: none;
}
.is-focus .focus-hud { display: flex; }
.focus-hud.is-visible,
.focus-hud:hover,
.focus-hud:focus-within {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}
.focus-hud button {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
}
.focus-hud button:hover { background: rgba(255, 255, 255, 0.12); }
.focus-hud button iconify-icon { font-size: 16px; }
.hud-label {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 50vw;
  padding: 0 8px;
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
}
.hud-label > span:nth-child(2) { overflow: hidden; text-overflow: ellipsis; }
.hud-label .piece-code { border-color: rgba(255, 255, 255, 0.25); background: transparent; color: inherit; }
.hud-count { color: #9d968a; font-size: 11px; font-weight: 700; }

/* Toast */
.toast {
  position: fixed;
  z-index: 50;
  right: 20px;
  bottom: 70px;
  padding: 9px 14px;
  border-radius: 8px;
  background: var(--ink);
  color: var(--paper);
  font-size: 12.5px;
  font-weight: 600;
  opacity: 0;
  transform: translateY(9px);
  transition: opacity 0.2s ease, transform 0.2s var(--ease-out);
  pointer-events: none;
}
.toast.is-visible { opacity: 1; transform: translateY(0); }

/* Responsive */
@media (max-width: 1180px) {
  .library-count { display: none; }
  .stage-tools #copy-link { display: none; }
}
@media (max-width: 900px) {
  :root { --topbar: 56px; }
  .topbar { grid-template-columns: auto auto minmax(0, 1fr) auto; gap: 10px; padding: 0 12px; }
  .brand { margin-right: 0; }
  .brand-name, .kbd { display: none; }
  .vault-main,
  .rail-collapsed .vault-main { grid-template-columns: minmax(0, 1fr); }
  .library-sidebar,
  .rail-collapsed .library-sidebar {
    position: fixed;
    z-index: 30;
    top: var(--topbar);
    bottom: 0;
    left: 0;
    width: min(320px, 86vw);
    visibility: visible;
    border-right: 1px solid var(--rule);
    box-shadow: var(--shadow);
    transform: translateX(-105%);
    transition: transform 0.22s var(--ease-out);
  }
  .library-sidebar.is-open { transform: translateX(0); }
  #library-tree { width: auto; }
  .sidebar-scrim {
    position: fixed;
    z-index: 25;
    inset: var(--topbar) 0 0;
    display: block;
    background: rgba(0, 0, 0, 0.3);
  }
  .stage-head { flex-direction: column; align-items: stretch; gap: 10px; padding: 10px 16px; }
  .stage-tools { overflow-x: auto; }
  .segmented { margin-right: auto; }
  .stage-canvas[data-device='tablet'],
  .stage-canvas[data-device='mobile'] { padding: 12px; }
  .stage-foot { grid-template-columns: auto 1fr auto; gap: 8px; padding: 6px 8px; }
  .stage-position { text-align: center; }
  .step-text { display: none; }
  .step-button { padding: 8px 12px; border: 1px solid var(--rule); }
}
@media (max-width: 560px) {
  .stage-desc { display: none; }
  .stage-tools #backdrop-toggle,
  .stage-tools #reload-piece { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
