/* Divs & Spans chrome. Deliberately quiet: the frames are the content. */
:root {
  color-scheme: light dark;
  --bg: light-dark(#ececea, #161617);
  --dot: light-dark(#d2d2ce, #2a2a2c);
  --panel: light-dark(#ffffff, #222224);
  --panel-2: light-dark(#f6f6f4, #2b2b2e);
  --line: light-dark(#dcdcd8, #38383b);
  --text: light-dark(#1d1d1f, #ececee);
  --muted: light-dark(#76767a, #9a9aa0);
  --accent: light-dark(#4f46e5, #8b85ff);
  --accent-soft: light-dark(#4f46e51a, #8b85ff26);
  --danger: light-dark(#d93d42, #ff6b6b);
  --shadow: 0 1px 2px #0000000f, 0 8px 24px #0000001a;
  --radius: 8px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
  /* room the phone dock takes at the bottom; 0 on desktop */
  --dock: 0px;
}
:root[data-chrome="light"] { color-scheme: light; }
:root[data-chrome="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overscroll-behavior: none; }
body {
  font: 13px/1.4 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
a { color: inherit; }

/* ---------- toolbar ---------- */
.bar {
  position: fixed;
  inset: 12px 12px auto 12px;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: none;
}
.bar > *, .bar .group { pointer-events: auto; flex: none; }
.bar > .group:first-child { flex: 0 1 auto; min-width: 0; }
/* the view settings sit in the bar like any other group; on phones they become a sheet */
.view-sheet { display: contents; }
.group {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.spacer { flex: 1; pointer-events: none; }
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 3px 10px 3px 8px;
  text-decoration: none;
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand-mark { font-family: var(--mono); font-size: 12px; color: var(--accent); }
.file-name {
  border: 0;
  background: transparent;
  padding: 4px 8px;
  border-radius: 6px;
  width: 22ch;
  min-width: 0;
  flex: 1 1 auto;
}
.file-name:hover { background: var(--panel-2); }
.file-name:focus { outline: 2px solid var(--accent); background: var(--panel-2); }
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 28px;
  padding: 0 10px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.btn.primary { background: var(--accent); color: white; }
.btn kbd {
  font: 11px var(--mono);
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
}
.btn[aria-pressed="true"] kbd { color: inherit; border-color: currentColor; }
.sep { width: 1px; height: 18px; background: var(--line); margin: 0 3px; }
.zoom { min-width: 5ch; justify-content: center; font-variant-numeric: tabular-nums; }
select.btn { appearance: none; padding-right: 10px; }

/* ---------- viewport ---------- */
.viewport {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: calc(24px * var(--z, 1)) calc(24px * var(--z, 1));
  background-position: var(--bx, 0) var(--by, 0);
  cursor: default;
  touch-action: none;
}
.viewport.panning, .viewport.panning * { cursor: grabbing !important; }
.viewport.space { cursor: grab; }
.viewport.space iframe, .viewport.panning iframe { pointer-events: none; }
.world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  display: flex;
  align-items: flex-start;
  gap: 120px;
  padding: 0;
}

/* ---------- flows ---------- */
.flows { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.flows .line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.flows .hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; cursor: pointer; }
.flows .src { fill: var(--accent); }
.flows .src.off { fill: var(--panel); stroke: var(--accent); stroke-width: 1.5; }
.flows .box { fill: none; stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 4 3; }
.flows marker path { fill: var(--accent); stroke: var(--accent); stroke-width: 1; stroke-linejoin: round; }
.flows .rest { opacity: 0.6; }
.flows .dim { opacity: 0.3; }
.flows .faint { opacity: 0.12; }
.flows .flow:hover { opacity: 1; }
.flows .flow:hover .line { stroke-width: 3; }
.flows .dead .line { stroke: var(--danger); stroke-dasharray: 4 4; }
.flows .dead .src, .flows .dead .end { fill: var(--danger); stroke: none; }
.flows .dead .x { stroke: var(--panel); stroke-width: 1.75; stroke-linecap: round; }
.viewport.panning .flows .hit, .viewport.space .flows .hit { pointer-events: none; }

/* ---------- frames ---------- */
.frame { position: relative; flex: none; }
.frame-label {
  container-type: inline-size;
  position: absolute;
  bottom: 100%;
  left: 0;
  right: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding-bottom: calc(8px / var(--z, 1));
  font-size: calc(13px / var(--z, 1));
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  transform-origin: 0 100%;
}
.viewport.tiny .frame-label .meta, .viewport.tiny .frame-label .copy { display: none; }
.viewport.tiny .task-text { display: none; }
.frame-label .name { flex: 0 1 auto; min-width: 0; color: var(--text); overflow: hidden; text-overflow: ellipsis; }
.frame-label .meta { flex: 0 1000 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; font-family: var(--mono); font-size: 0.85em; }
.frame-label .copy {
  margin-left: auto;
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: inherit;
  padding: 0;
}
.frame-label .copy:hover { color: var(--accent); }
.frame-label kbd {
  font: 0.85em var(--mono);
  border: calc(1px / var(--z, 1)) solid var(--line);
  border-radius: 0.3em;
  padding: 0 0.3em;
  margin-left: 0.3em;
}
/* em here is the label's own font size, so this holds at every zoom level */
@container (max-width: 24em) {
  .frame-label .copy { display: none; }
  .frame:hover .frame-label .copy { display: inline; flex: none; }
  .frame:hover .frame-label .meta { display: none; }
}
.frame.selected .frame-label .name { color: var(--accent); }
.frame-label .key { display: none; margin: 0; flex: none; }
.frame:is(:hover, .selected) .frame-label .key:not(:empty) { display: inline; }
.viewport.tiny .frame-label .key { display: none !important; }
.frame-body { display: flex; gap: 40px; }
.screen {
  position: relative;
  background: var(--panel);
  box-shadow: 0 0 0 1px var(--line), var(--shadow);
  overflow: hidden;
}
.frame.selected .screen { box-shadow: 0 0 0 calc(2px / var(--z, 1)) var(--accent), var(--shadow); }
.screen[data-scheme="light"] { color-scheme: light; }
.screen[data-scheme="dark"] { color-scheme: dark; }
.screen iframe {
  display: block;
  border: 0;
  width: 100%;
  height: 100%;
  background: transparent;
}
/* device chrome (D): drawn in CSS so it stays sharp at any zoom */
.frame.dressed .frame-body { gap: 80px; }
.frame.dressed .frame-label { margin-bottom: 24px; }
.screen.device {
  --bezel: 12px;
  border-radius: var(--radius);
  box-shadow: 0 0 0 var(--bezel) #0b0b0d, 0 0 0 calc(var(--bezel) + 1.5px) #3c3c41, var(--shadow);
}
.screen.device[data-kind="tablet"] { --bezel: 18px; }
.frame.selected .screen.device {
  box-shadow: 0 0 0 var(--bezel) #0b0b0d, 0 0 0 calc(var(--bezel) + 2px / var(--z, 1)) var(--accent), var(--shadow);
}
.chrome { display: none; position: absolute; inset: 0; pointer-events: none; z-index: 1; }
.screen.device .chrome { display: block; }
.screen[data-scheme="light"] .chrome { color: #000; }
.screen[data-scheme="dark"] .chrome { color: #fff; }
.chrome .status {
  position: absolute;
  inset: 0 0 auto;
  height: var(--safe-top);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 34px 0 52px;
  font: 600 17px/1 -apple-system, "SF Pro Text", system-ui, sans-serif;
  letter-spacing: -0.01em;
}
.chrome .icons { display: flex; align-items: center; gap: 6px; }
.chrome svg { fill: currentColor; }
.screen[data-kind="phone"] .chrome .status { height: 54px; }
.screen[data-kind="tablet"] .chrome .status { padding: 0 20px; font-size: 13px; }
.screen[data-kind="tablet"] .chrome .icons { transform: scale(0.8); transform-origin: right center; }
.screen[data-cutout="punch"] .chrome .status { padding: 0 24px; font: 500 14px/1 system-ui, sans-serif; }
.chrome .cutout { position: absolute; left: 50%; translate: -50% 0; background: #000; display: none; }
.screen[data-cutout="island"] .chrome .cutout { display: block; top: 11px; width: 124px; height: 36px; border-radius: 20px; }
.screen[data-cutout="punch"] .chrome .cutout { display: block; top: 13px; width: 14px; height: 14px; border-radius: 50%; }
.chrome .home-bar {
  position: absolute;
  left: 50%;
  bottom: 8px;
  translate: -50% 0;
  width: 140px;
  height: 5px;
  border-radius: 3px;
  background: currentColor;
}
.screen[data-kind="tablet"] .chrome .home-bar { width: 320px; }
.screen[data-cutout="punch"] .chrome .home-bar { width: 108px; height: 4px; bottom: 10px; }

/* resize handles: drag any frame and every frame follows */
.resize {
  position: absolute;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.15s;
  touch-action: none;
}
.frame:hover .resize, .viewport.resizing .resize { opacity: 1; }
.resize::after { content: ""; position: absolute; inset: 0; margin: auto; border-radius: 99px; background: var(--muted); }
.resize:hover::after, .viewport.resizing .resize::after { background: var(--accent); }
.resize.e {
  left: 100%;
  top: 0;
  bottom: 0;
  width: calc(24px / var(--z, 1));
  cursor: ew-resize;
}
.resize.e::after { width: calc(5px / var(--z, 1)); height: calc(56px / var(--z, 1)); }
.resize.s {
  top: 100%;
  left: 0;
  right: 0;
  height: calc(24px / var(--z, 1));
  cursor: ns-resize;
}
.resize.s::after { height: calc(5px / var(--z, 1)); width: calc(56px / var(--z, 1)); }
.resize.se {
  left: 100%;
  top: 100%;
  width: calc(24px / var(--z, 1));
  height: calc(24px / var(--z, 1));
  cursor: nwse-resize;
}
.resize.se::after { width: calc(9px / var(--z, 1)); height: calc(9px / var(--z, 1)); }
.frame.dressed .resize.e { margin-left: 16px; }
.frame.dressed .resize.s { margin-top: 16px; }
.frame.dressed .resize.se { margin: 16px 0 0 16px; }
.full-height .resize.s, .full-height .resize.se { display: none; }
.viewport.resizing, .viewport.resizing * { cursor: grabbing; }
.viewport.resizing iframe { pointer-events: none; }

.screen .scheme {
  position: absolute;
  top: 100%;
  left: 0;
  padding-top: calc(6px / var(--z, 1));
  font: calc(11px / var(--z, 1)) var(--mono);
  color: var(--muted);
}

/* working state: shimmer over the frame (or an empty skeleton) */
.working-overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: flex-end;
  padding: 24px;
  background: linear-gradient(100deg, transparent 20%, light-dark(#ffffff99, #ffffff14) 50%, transparent 80%) 0 0 / 300% 100%,
    light-dark(#f2f2f0cc, #1c1c1ecc);
  animation: shimmer 1.8s linear infinite;
  pointer-events: none;
}
.frame.working .working-overlay { display: flex; }
.working-overlay .skeleton {
  position: absolute;
  inset: 48px 48px auto 48px;
  display: grid;
  gap: 18px;
}
.frame.has-content .working-overlay .skeleton { display: none; }
.skeleton i {
  display: block;
  height: 14px;
  border-radius: 7px;
  background: light-dark(#00000012, #ffffff12);
}
.skeleton i:nth-child(1) { width: 40%; height: 28px; }
.skeleton i:nth-child(2) { width: 75%; }
.skeleton i:nth-child(3) { width: 60%; }
.skeleton i:nth-child(4) { width: 100%; height: 160px; border-radius: 12px; }
.skeleton i:nth-child(5) { width: 50%; }
.working-overlay .task {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 100%;
  padding: 10px 14px;
  border-radius: 999px;
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  font-size: calc(13px / var(--z, 1));
  transform-origin: 0 100%;
}
.pulse {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.2s ease-in-out infinite;
  flex: none;
}
@keyframes shimmer { to { background-position: -300% 0, 0 0; } }
@keyframes pulse { 50% { opacity: 0.3; transform: scale(0.7); } }
@media (prefers-reduced-motion: reduce) {
  .working-overlay, .pulse { animation: none; }
}

/* ---------- empty state ---------- */
.empty {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}
.empty-card {
  max-width: 460px;
  padding: 28px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  pointer-events: auto;
}
.empty-card h2 { margin: 0 0 6px; font-size: 17px; letter-spacing: -0.01em; }
.empty-card p { margin: 0 0 14px; color: var(--muted); }

/* ---------- popover / connect ---------- */
.pop {
  position: fixed;
  top: 56px;
  right: 12px;
  z-index: 20;
  width: min(420px, calc(100vw - 24px));
  padding: 16px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.pop[hidden] { display: none; }
.pop h3 { margin: 0 0 4px; font-size: 13px; }
.pop p { margin: 0 0 12px; color: var(--muted); }
.field { display: flex; gap: 6px; margin-bottom: 14px; }
.field input {
  flex: 1;
  min-width: 0;
  height: 30px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel-2);
  font: 12px var(--mono);
}
.field .btn { border: 1px solid var(--line); }
.prompt {
  font: 12px/1.5 var(--mono);
  padding: 10px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  white-space: pre-wrap;
  margin: 0 0 8px;
}

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--dock) + 20px);
  z-index: 30;
  transform: translate(-50%, 12px);
  opacity: 0;
  max-width: calc(100vw - 32px);
  padding: 9px 14px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  transition: opacity 0.15s, transform 0.15s;
  pointer-events: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
body.picking .toast { bottom: calc(var(--dock) + 72px); }

/* shown while selecting elements */
.pick-bar {
  position: fixed;
  left: 50%;
  bottom: calc(var(--dock) + 20px);
  z-index: 29;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100vw - 32px);
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
  white-space: nowrap;
  overflow: hidden;
  pointer-events: none;
}
.pick-bar[hidden] { display: none; }
.pick-bar b { color: var(--accent); font-weight: 600; }
.pick-bar .btn { pointer-events: auto; color: var(--text); }
.pick-bar .actions { display: flex; gap: 2px; margin-left: auto; }
.pick-bar kbd {
  font: 11px var(--mono);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  margin-right: 4px;
}
@media (max-width: 640px) { .pick-bar span:nth-of-type(n+3) { display: none; } }
.toast code { font: 12px var(--mono); opacity: 0.75; }

.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); margin: 0 6px; }
.status-dot.live { background: #22a06b; }

/* ---------- home ---------- */
.home {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 48px 16px;
  background-image: radial-gradient(var(--dot) 1px, transparent 1px);
  background-size: 24px 24px;
}
.home-card {
  width: min(520px, 100%);
  padding: 32px;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.home-card h1 { margin: 0 0 6px; font-size: 26px; letter-spacing: -0.02em; }
.home-card h1 span { font-family: var(--mono); font-weight: 400; font-size: 15px; color: var(--accent); margin-left: 6px; }
.home-card > p { margin: 0 0 22px; color: var(--muted); font-size: 14px; }
.home-card form { display: flex; gap: 8px; margin-bottom: 24px; }
.home-card form input {
  flex: 1;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
}
.home-card form .btn { height: 36px; padding: 0 16px; }
.home-card h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.recent { list-style: none; margin: 0 0 24px; padding: 0; }
.recent a {
  display: flex;
  justify-content: space-between;
  padding: 8px 10px;
  margin: 0 -10px;
  border-radius: 8px;
  text-decoration: none;
}
.recent a:hover { background: var(--panel-2); }
.recent time { color: var(--muted); font-size: 12px; }

@media (max-width: 1180px) {
  .hide-lg, .bar kbd { display: none !important; }
}
@media (max-width: 1100px) {
  .hide-md { display: none !important; }
}
@media (min-width: 821px) {
  .phone-only { display: none !important; }
}

/* ---------- phones: identity on top, tools in a dock at the thumb, view settings in a sheet ---------- */
@media (max-width: 820px) {
  :root { --dock: calc(64px + env(safe-area-inset-bottom)); }
  .bar {
    top: calc(8px + env(safe-area-inset-top));
    left: calc(8px + env(safe-area-inset-left));
    right: calc(8px + env(safe-area-inset-right));
  }
  .bar, .tools, .view-sheet, .pick-bar { touch-action: none; }
  .bar .btn { height: 36px; }
  .file-name { width: auto; }

  .tools {
    position: fixed;
    left: 50%;
    bottom: calc(10px + env(safe-area-inset-bottom));
    translate: -50% 0;
    max-width: calc(100vw - 16px);
  }
  .tools .btn { height: 40px; padding: 0 11px; }
  .stepper { display: flex; align-items: center; }
  .stepper .btn { padding: 0 9px; font-size: 17px; }
  .stepper .count { font-size: 13px; min-width: 4.5ch; justify-content: center; padding: 0 2px; font-variant-numeric: tabular-nums; }
  .tools .btn:disabled { opacity: 0.35; }
  .tools .btn[aria-expanded="true"] { background: var(--accent-soft); color: var(--accent); }

  .view-sheet {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    position: fixed;
    left: 8px;
    right: 8px;
    bottom: calc(var(--dock) + 8px);
    max-width: 420px;
    margin: 0 auto;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: var(--panel);
    box-shadow: 0 6px 24px rgb(0 0 0 / 0.25);
  }
  body:not(.view-open) .view-sheet { display: none; }
  .view-sheet .group { padding: 0; border: 0; box-shadow: none; background: none; }
  .view-sheet .view { flex: 1 1 100%; flex-wrap: wrap; gap: 6px; }
  .view-sheet .view .sep { display: none; }
  .view-sheet .view [data-theme-mode] { flex: 1; justify-content: center; background: var(--panel-2); }
  .view-sheet .view [data-theme-mode][aria-pressed="true"] { background: var(--accent-soft); }
  .view-sheet #deviceSel { flex: 1 1 100%; border: 1px solid var(--line); }
  .view-sheet #chromeBtn, .view-sheet #fullBtn { flex: 1; justify-content: center; border: 1px solid var(--line); }
  .view-sheet .group:last-child { margin-left: auto; }

  .pop { top: calc(56px + env(safe-area-inset-top)); left: 8px; right: 8px; width: auto; max-height: calc(100dvh - var(--dock) - 72px); overflow: auto; touch-action: pan-y; }

  .pick-bar {
    left: 8px;
    right: 8px;
    bottom: calc(var(--dock) + 8px);
    transform: none;
    max-width: 420px;
    margin: 0 auto;
    gap: 8px;
    padding: 4px 4px 4px 14px;
    border-radius: 14px;
    min-height: 48px;
  }
  .pick-bar span:nth-of-type(n+3) { display: none; }
  .pick-bar .btn { height: 38px; padding: 0 10px; }
  .pick-bar .btn[aria-pressed="true"] { color: var(--accent); }
  body.picking .toast { bottom: calc(var(--dock) + 68px); }
}

/* ---------- touch: no hover, so show what hover would on the selected frame ---------- */
@media (pointer: coarse) {
  /* Move: frames don't take touches, so one finger pans and two pinch anywhere */
  body:not(.picking) .screen iframe { pointer-events: none; }
  .frame:not(.selected) .resize { display: none; }
  .frame.selected .resize { opacity: 1; }
  .frame-label kbd { display: none !important; }
}
@container (max-width: 24em) {
  @media (hover: none) {
    .frame.selected .frame-label .copy { display: inline; flex: none; }
    .frame.selected .frame-label .meta { display: none; }
  }
}
