/* Tokens. Light by default, dark by system setting or by the Appearance switch (data-theme). */
:root {
  --bg: #f5f5f7; --panel: #ffffff; --panel2: #ececf0; --border: rgba(0,0,0,.1);
  --text: #1d1d1f; --muted: #6e6e73; --accent: #0071e3; --danger: #d70015; --ok: #248a3d; --warn: #b25000;
  --glass: rgba(255,255,255,.8); --shadow: 0 10px 34px rgba(0,0,0,.14); --stage: #e8e8ed;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme=light]) {
    --bg: #000; --panel: #1c1c1e; --panel2: #2c2c2e; --border: rgba(255,255,255,.13);
    --text: #f5f5f7; --muted: #98989d; --accent: #0a84ff; --danger: #ff453a; --ok: #30d158; --warn: #ff9f0a;
    --glass: rgba(36,36,38,.8); --shadow: 0 10px 34px rgba(0,0,0,.6); --stage: #0b0b0c;
    color-scheme: dark;
  }
}
:root[data-theme=dark] {
  --bg: #000; --panel: #1c1c1e; --panel2: #2c2c2e; --border: rgba(255,255,255,.13);
  --text: #f5f5f7; --muted: #98989d; --accent: #0a84ff; --danger: #ff453a; --ok: #30d158; --warn: #ff9f0a;
  --glass: rgba(36,36,38,.8); --shadow: 0 10px 34px rgba(0,0,0,.6); --stage: #0b0b0c;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
body.studio { overflow: hidden; }
.hidden { display: none !important; }
a { color: var(--accent); text-decoration: none; } a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
button { background: var(--panel2); border: 0; border-radius: 9px; padding: 7px 14px; cursor: pointer; transition: filter .12s, background .12s; }
button:hover { filter: brightness(.96); } button:disabled { opacity: .5; cursor: default; }
button:focus-visible, input:focus-visible, textarea:focus-visible, a:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button.primary { background: var(--accent); color: #fff; font-weight: 600; }
button.danger { color: var(--danger); } button.danger.solid { background: var(--danger); color: #fff; font-weight: 600; }
button.small { padding: 4px 10px; font-size: 13px; }
button.wide { width: 100%; }
button.link { background: none; padding: 0; text-align: left; color: var(--accent); border-radius: 4px; }
button.link.danger { color: var(--danger); }
button.icon { width: 32px; height: 32px; padding: 0; border-radius: 50%; display: inline-grid; place-items: center; flex-shrink: 0; }
button.on { background: var(--accent); color: #fff; }
input, select, textarea { background: var(--panel); border: 1px solid var(--border); border-radius: 9px; padding: 7px 10px; }
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; margin: 0; }
.note, .muted { color: var(--muted); } .note { font-size: 13px; margin: 4px 0 0; }

/* login */
.login { position: fixed; inset: 0; display: grid; place-items: center; background: var(--bg); z-index: 50; padding: 16px; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 20px; padding: 30px; width: min(360px, 100%); display: flex; flex-direction: column; gap: 14px; box-shadow: var(--shadow); }
.login-card h1 { margin: 0 0 4px; font-size: 26px; letter-spacing: -.02em; }

/* frame */
#app { display: flex; height: 100%; }
#rail { width: 240px; flex-shrink: 0; background: var(--panel2); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 12px; gap: 10px; }
#chatList { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 2px; }
#chatList li { display: flex; gap: 10px; align-items: center; padding: 6px 8px; border-radius: 11px; cursor: pointer; }
#chatList li:hover { background: var(--border); } #chatList li.on { background: var(--panel); box-shadow: 0 1px 3px rgba(0,0,0,.08); }
#chatList img, .cover-empty { width: 38px; height: 38px; border-radius: 8px; object-fit: cover; flex-shrink: 0; background: var(--border); }
#chatList div { min-width: 0; } #chatList b { display: block; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#chatList span { font-size: 12px; color: var(--muted); }
.rail-foot { display: flex; flex-direction: column; gap: 7px; font-size: 13px; padding: 8px 4px 0; border-top: 1px solid var(--border); }
.rail-foot .who { display: flex; justify-content: space-between; color: var(--muted); padding-top: 4px; }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; background: var(--panel); overflow: hidden; }
#bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
#bar .title { flex: 1; min-width: 0; text-align: left; background: none; padding: 4px 6px; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#menuBtn { display: none; }
#status { min-width: 0; flex-shrink: 1; } #status span { overflow: hidden; text-overflow: ellipsis; }
#status .dot { flex-shrink: 0; }
.pill { display: inline-flex; align-items: center; gap: 7px; background: var(--panel2); border-radius: 99px; padding: 5px 12px; font-size: 12px; white-space: nowrap; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: var(--ok); } .dot.busy { background: var(--warn); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* viewer */
#viewer { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.stage { flex: 1; min-height: 0; position: relative; display: grid; place-items: center; padding: 16px; background: var(--stage); }
.frame { position: absolute; inset: 14px; --x: 50%; }
.frame img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; cursor: zoom-in; }
.frame.zoom { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.94); }
.frame.zoom img { cursor: zoom-out; }
.frame.cmp { cursor: ew-resize; touch-action: none; }
.frame.cmp img { cursor: ew-resize; }
.frame.cmp .before { clip-path: inset(0 calc(100% - var(--x)) 0 0); }
.frame.cmp .knob { position: absolute; top: 0; bottom: 0; left: var(--x); width: 2px; background: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.25); pointer-events: none; }
.frame .lab { position: absolute; bottom: 12px; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; border-radius: 6px; padding: 2px 8px; pointer-events: none; }
.frame .lab.l { left: 12px; } .frame .lab.r { right: 12px; }
.caption { padding: 10px 16px 0; text-align: center; } .caption p { margin: 0; white-space: pre-wrap; max-height: 3em; overflow: auto; }
.caption span { font-size: 12px; color: var(--muted); }
.tools { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; padding: 10px 16px; }
.tools button { font-size: 13px; padding: 6px 12px; }
.wait { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 6px; } .wait h3 { margin: 10px 0 0; font-size: 17px; } .wait p { margin: 0 0 8px; }
.ring { --p: 0; width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(var(--accent) calc(var(--p) * 1%), var(--border) 0); }
.ring::after { content: ""; width: 72%; height: 72%; border-radius: 50%; background: var(--panel2); }
.ring.big { width: 96px; height: 96px; } .ring.big::after { width: 86%; height: 86%; background: var(--stage); }
.ring.indet { animation: spin 1.1s linear infinite; background: conic-gradient(var(--accent) 22%, var(--border) 0); }
@keyframes spin { to { transform: rotate(1turn); } }
.empty { text-align: center; max-width: 760px; } .empty h2 { font-size: 26px; letter-spacing: -.02em; margin: 0 0 18px; }
.starts { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; margin-bottom: 16px; }
.start { background: var(--panel); border-radius: 16px; padding: 16px; text-align: left; display: flex; flex-direction: column; gap: 4px; box-shadow: 0 1px 3px rgba(0,0,0,.08); }
.start b { font-size: 15px; } .start span { color: var(--muted); font-size: 13px; } .start em { font-style: normal; font-size: 12px; color: var(--accent); margin-top: 6px; }

/* history strip */
#strip { display: flex; gap: 14px; padding: 10px 16px 4px; overflow-x: auto; border-top: 1px solid var(--border); align-items: flex-start; scrollbar-width: thin; }
.turn { flex-shrink: 0; max-width: 340px; } .tiles { display: flex; gap: 5px; align-items: center; }
.turn .cap { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 3px; max-width: 100%; }
.tile { width: 52px; height: 52px; padding: 0; border-radius: 10px; overflow: hidden; display: grid; place-items: center; flex-shrink: 0; outline: 2px solid transparent; outline-offset: 1px; }
.tile img { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.on { outline-color: var(--accent); background: var(--panel2); color: inherit; } .tile.bad { color: var(--danger); font-weight: 700; }
.turn .from { width: 34px; height: 34px; border-radius: 8px; object-fit: cover; opacity: .55; } .arrow { color: var(--muted); font-size: 12px; }
img.broken { visibility: hidden; }

/* composer */
#composer { margin: 8px auto 14px; width: min(760px, calc(100% - 28px)); background: var(--glass); backdrop-filter: blur(22px); -webkit-backdrop-filter: blur(22px); border: 1px solid var(--border); border-radius: 22px; box-shadow: var(--shadow); padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.c-top { display: flex; gap: 8px; align-items: flex-end; }
#prompt { flex: 1; min-width: 0; border: 0; background: none; resize: none; font-size: 15px; padding: 9px 4px; max-height: 160px; }
#prompt:focus { outline: none; }
.add { width: 40px; height: 40px; border-radius: 12px; border: 1.5px dashed var(--border); color: var(--muted); font-size: 22px; flex-direction: row; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; }
.add:hover { border-color: var(--accent); color: var(--accent); filter: none; }
button.add { background: none; padding: 0; display: inline-flex; }
.inputs { display: flex; gap: 8px; align-items: flex-end; flex-shrink: 0; }
.slot { display: flex; flex-direction: column; align-items: center; gap: 2px; } .slot small { font-size: 10px; color: var(--muted); max-width: 64px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.input-chip { position: relative; flex-shrink: 0; } .input-chip img { width: 40px; height: 40px; border-radius: 10px; object-fit: cover; display: block; }
.input-chip button { position: absolute; top: -6px; right: -6px; width: 18px; height: 18px; padding: 0; border-radius: 50%; background: var(--text); color: var(--panel); font-size: 12px; line-height: 18px; }
.send { width: 36px; height: 36px; padding: 0; border-radius: 50%; background: var(--accent); color: #fff; font-size: 18px; font-weight: 700; flex-shrink: 0; margin-bottom: 2px; }
.c-bottom { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.chip { border-radius: 99px; padding: 5px 12px; font-size: 12px; display: inline-flex; gap: 6px; align-items: center; } .chip span { color: var(--muted); } .chip i { font-style: normal; color: var(--muted); }
.hint { margin-left: auto; font-size: 12px; color: var(--muted); }
.only-narrow { display: none; }
#drop { position: absolute; inset: 8px; z-index: 30; border: 2px dashed var(--accent); border-radius: 18px; background: var(--glass); backdrop-filter: blur(8px); display: grid; place-items: center; font-size: 20px; font-weight: 600; pointer-events: none; }

/* inspector */
#inspector { width: 296px; flex-shrink: 0; border-left: 1px solid var(--border); background: var(--panel); padding: 14px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.card { background: var(--panel2); border-radius: 16px; padding: 14px; }
.card h4, .sheet h4 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.card h3 { margin: 0 0 4px; font-size: 17px; letter-spacing: -.01em; } .card p { margin: 0 0 10px; font-size: 13px; }
.tag { font-style: normal; font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 6px; background: var(--accent); color: #fff; margin-left: 8px; vertical-align: middle; text-transform: uppercase; letter-spacing: .04em; }
.card dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0 0 12px; font-size: 12px; } .card dt { color: var(--muted); } .card dd { margin: 0; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; font-size: 12px; color: var(--muted); } .field > span b { color: var(--text); }
.field p { margin: 0; font-size: 12px; } .field input[type=range] { accent-color: var(--accent); padding: 0; border: 0; background: none; }
.seg { display: inline-flex; background: var(--border); border-radius: 10px; padding: 2px; gap: 2px; } .seg.wrap { flex-wrap: wrap; }
.seg button { background: none; padding: 5px 10px; font-size: 12px; border-radius: 8px; color: var(--muted); }
.seg button.on { background: var(--panel); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.14); }
.stepper { display: inline-flex; align-items: center; gap: 12px; } .stepper button { width: 30px; height: 30px; padding: 0; border-radius: 50%; background: var(--panel); font-size: 16px; } .stepper b { color: var(--text); font-size: 15px; min-width: 1ch; text-align: center; }
details summary { cursor: pointer; font-size: 12px; color: var(--accent); margin-bottom: 10px; }

/* sheets, dialogs, toasts */
dialog.sheet { border: 0; border-radius: 22px; padding: 24px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); width: min(420px, calc(100vw - 24px)); max-height: calc(100vh - 24px); overflow-y: auto; }
dialog.sheet.wide { width: min(860px, calc(100vw - 24px)); }
dialog::backdrop { background: rgba(0,0,0,.4); backdrop-filter: blur(3px); }
.sheet h2 { margin: 0 0 6px; font-size: 21px; letter-spacing: -.02em; } .sheet h4 { margin: 20px 0 8px; } .sheet p { margin: 0 0 12px; }
.sheet form { display: flex; flex-direction: column; gap: 10px; } .dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.sheet .x, #inspector .x { position: sticky; float: right; top: 0; width: 30px; height: 30px; padding: 0; border-radius: 50%; font-size: 17px; }
.mgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px; }
.mcard { background: var(--panel2); border-radius: 16px; padding: 14px; text-align: left; outline: 2px solid transparent; display: flex; flex-direction: column; gap: 4px; }
.mcard.on { outline-color: var(--accent); background: var(--panel2); color: inherit; } .mcard b { font-size: 15px; } .mcard p { margin: 0 0 6px; font-size: 12px; color: var(--muted); flex: 1; }
.mcard b em { font-style: normal; font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 6px; background: var(--accent); color: #fff; margin-left: 6px; vertical-align: middle; text-transform: uppercase; letter-spacing: .04em; }
.sheet details.more { margin-top: 10px; } .sheet details.more summary { font-size: 13px; margin-bottom: 10px; }
.mcard div { display: flex; justify-content: space-between; font-size: 12px; } .mcard div span:last-child { color: var(--muted); }
.flow { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; }
.flow div { background: var(--panel2); border-radius: 14px; padding: 12px; font-size: 12px; color: var(--muted); } .flow b { display: block; color: var(--text); font-size: 13px; margin-bottom: 3px; }
.scroll-x { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; } th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: top; } th { color: var(--muted); font-weight: 600; font-size: 12px; }
#toasts { position: fixed; left: 50%; bottom: 120px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; align-items: center; z-index: 60; pointer-events: none; }
.toast { background: var(--text); color: var(--panel); border-radius: 99px; padding: 8px 16px; font-size: 13px; box-shadow: var(--shadow); transition: opacity .3s, transform .3s; max-width: 90vw; }
.toast.bad { background: var(--danger); color: #fff; } .toast.out { opacity: 0; transform: translateY(8px); }
#railScrim { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 34; }

@media (max-width: 1100px) {
  #inspector { position: fixed; left: 0; right: 0; bottom: 0; width: auto; max-height: 80vh; z-index: 35; border-left: 0; border-radius: 22px 22px 0 0; box-shadow: var(--shadow); transform: translateY(105%); transition: transform .25s; padding-bottom: max(14px, env(safe-area-inset-bottom)); }
  #inspector.open { transform: none; }
  .only-narrow { display: inline-flex; }
}
@media (max-width: 800px) {
  #rail { position: fixed; inset: 0 auto 0 0; z-index: 35; width: min(82vw, 300px); transform: translateX(-102%); transition: transform .22s; }
  #rail.open { transform: none; }
  #menuBtn { display: inline-grid; }
  .stage { padding: 8px; } .hint { width: 100%; margin: 0; }
  .c-top { flex-wrap: wrap; } .inputs.multi { width: 100%; }
  #composer { margin-bottom: max(10px, env(safe-area-inset-bottom)); }
}
/* fingers: every control at least 44 px tall */
@media (pointer: coarse) {
  button, .chip, .seg button, .tools button { min-height: 44px; }
  button.icon, .send, .add, .stepper button, .sheet .x, #inspector .x { width: 44px; height: 44px; min-height: 44px; }
  button.link { min-height: 36px; } .input-chip img { width: 44px; height: 44px; }
  .input-chip button { width: 24px; height: 24px; min-height: 24px; line-height: 24px; top: -8px; right: -8px; }
  .tile { min-height: 52px; } #chatList li { padding: 9px 8px; }
  #strip { scrollbar-width: none; } #strip::-webkit-scrollbar { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
