:root{
  --bg:#0b0d12; --bg2:#0f1218; --panel:#141821; --panel2:#171c26; --line:#232a37; --line2:#2e3747;
  --txt:#e9edf6; --mut:#8f9bb3; --mut2:#6b768c;
  --acc:#6d8bff; --acc2:#8f6bff; --ok:#35d39a; --warn:#ffb454; --err:#ff6b6b;
  --r:14px; --r-sm:10px;
  --shadow:0 18px 40px rgba(0,0,0,.45);
}
*{box-sizing:border-box}
[hidden]{display:none!important}
html,body{height:100%}
body{
  margin:0;background:radial-gradient(1200px 700px at 20% -10%,#161d2b 0%,var(--bg) 55%) fixed,var(--bg);
  color:var(--txt);font:15px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Inter,Arial,sans-serif;
  -webkit-font-smoothing:antialiased;overflow:hidden;
}
button,input,select,textarea{font:inherit;color:inherit}
button{cursor:pointer;border:0;background:none}
a{color:var(--acc);text-decoration:none}
h1,h2,h3{margin:0;font-weight:600;letter-spacing:-.01em}
svg{width:18px;height:18px;fill:none;stroke:currentColor;stroke-width:1.8;stroke-linecap:round;stroke-linejoin:round}

/* ---------------- topbar ---------------- */
.topbar{
  height:58px;display:flex;align-items:center;gap:16px;padding:0 16px;
  border-bottom:1px solid var(--line);background:rgba(13,16,23,.86);backdrop-filter:blur(14px);
  position:relative;z-index:20;
}
.brand{display:flex;align-items:center;gap:10px;color:var(--txt)}
.brand-mark{
  width:30px;height:30px;border-radius:9px;display:grid;place-items:center;font-weight:800;font-size:16px;
  background:linear-gradient(140deg,var(--acc),var(--acc2));box-shadow:0 6px 18px rgba(109,139,255,.35);
}
.brand-text{display:flex;flex-direction:column;line-height:1.05;font-weight:600}
.brand-text small{color:var(--mut2);font-size:11px;font-weight:500;letter-spacing:.08em;text-transform:uppercase}
.doc-info{display:flex;align-items:center;gap:8px;color:var(--mut);font-size:13px;margin-left:8px;
  padding:5px 12px;border:1px solid var(--line);border-radius:999px;background:var(--panel)}
.doc-info .dot{width:6px;height:6px;border-radius:50%;background:var(--mut2)}
.doc-info.live .dot{background:var(--ok);box-shadow:0 0 0 3px rgba(53,211,154,.15)}
.top-actions{margin-left:auto;display:flex;align-items:center;gap:8px}
.history-group{display:flex;gap:4px;padding:3px;border:1px solid var(--line);border-radius:11px;background:var(--panel)}

.primary,.ghost{
  display:inline-flex;align-items:center;justify-content:center;gap:7px;
  height:36px;padding:0 14px;border-radius:10px;font-weight:600;font-size:14px;
  transition:transform .12s ease,background .15s,border-color .15s,opacity .15s;
}
.primary{background:linear-gradient(140deg,var(--acc),var(--acc2));color:#fff;box-shadow:0 8px 20px rgba(109,139,255,.28)}
.primary:hover{transform:translateY(-1px)}
.primary:disabled,.ghost:disabled{opacity:.42;cursor:default;transform:none;box-shadow:none}
.ghost{background:var(--panel);border:1px solid var(--line);color:var(--txt)}
.ghost:hover:not(:disabled){background:var(--panel2);border-color:var(--line2)}
.ghost.icon{padding:0;width:34px}
.primary.icon{width:40px;padding:0;height:40px}

/* ---------------- layout ---------------- */
.shell{
  display:grid;grid-template-columns:74px minmax(0,1fr) 360px;gap:0;
  height:calc(100dvh - 58px);
}
.rail{
  border-right:1px solid var(--line);background:var(--bg2);padding:10px 8px;
  display:flex;flex-direction:column;gap:4px;overflow-y:auto;
}
.rail-btn{
  display:flex;flex-direction:column;align-items:center;gap:5px;padding:9px 4px;border-radius:12px;
  color:var(--mut);font-size:10.5px;font-weight:600;letter-spacing:.01em;transition:.15s;
}
.rail-btn svg{width:20px;height:20px}
.rail-btn:hover{background:var(--panel);color:var(--txt)}
.rail-btn.active{background:linear-gradient(160deg,rgba(109,139,255,.22),rgba(143,107,255,.12));color:#cfd9ff;
  box-shadow:inset 0 0 0 1px rgba(109,139,255,.35)}

.stage{position:relative;display:flex;flex-direction:column;min-width:0;background:
  linear-gradient(0deg,rgba(255,255,255,.02),rgba(255,255,255,.02)),
  repeating-conic-gradient(#12161e 0% 25%,#0e1219 0% 50%) 50%/24px 24px;}
.canvas-wrap{flex:1;position:relative;display:grid;place-items:center;overflow:auto;padding:26px}
.canvas-holder{position:relative;line-height:0;box-shadow:var(--shadow);border-radius:6px;overflow:hidden}
canvas{display:block;max-width:100%;height:auto;touch-action:none;background:#0a0c11}
.stage-bar{
  height:46px;border-top:1px solid var(--line);display:flex;align-items:center;gap:12px;padding:0 14px;
  background:rgba(15,18,25,.9);backdrop-filter:blur(10px);
}
.zoom{display:flex;align-items:center;gap:6px;font-size:13px;color:var(--mut)}
.zoom .ghost{height:28px;padding:0 9px;font-size:12.5px}
.zoom .ghost.icon{width:28px}
.stage-hint{margin-left:auto;color:var(--mut2);font-size:12.5px}

/* ---------------- empty state ---------------- */
.empty{display:grid;place-items:center;width:100%;height:100%;padding:20px}
.empty-card{max-width:470px;text-align:center;padding:34px 30px;border:1px dashed var(--line2);border-radius:22px;
  background:rgba(20,24,33,.72);backdrop-filter:blur(8px)}
.empty-icon{width:56px;height:56px;border-radius:16px;margin:0 auto 16px;display:grid;place-items:center;
  background:linear-gradient(150deg,rgba(109,139,255,.25),rgba(143,107,255,.15));color:#c8d3ff}
.empty-icon svg{width:26px;height:26px}
.empty-card h1{font-size:21px;margin-bottom:8px}
.empty-card p{color:var(--mut);font-size:14px;margin:0 0 18px}
.empty-actions{display:flex;gap:10px;justify-content:center;flex-wrap:wrap}
.empty-note{font-size:12.5px;color:var(--mut2);margin-top:16px!important}

/* ---------------- crop layer ---------------- */
.crop-layer{position:absolute;inset:0;cursor:crosshair;background:rgba(0,0,0,.45)}
.crop-box{position:absolute;border:1px solid #fff;box-shadow:0 0 0 9999px rgba(0,0,0,.35);pointer-events:none}
.crop-box .h{position:absolute;width:13px;height:13px;background:#fff;border-radius:3px;box-shadow:0 1px 4px rgba(0,0,0,.5);pointer-events:auto}
.crop-box .h.tl{left:-6px;top:-6px;cursor:nwse-resize}
.crop-box .h.tr{right:-6px;top:-6px;cursor:nesw-resize}
.crop-box .h.bl{left:-6px;bottom:-6px;cursor:nesw-resize}
.crop-box .h.br{right:-6px;bottom:-6px;cursor:nwse-resize}

/* ---------------- busy ---------------- */
.busy{position:absolute;inset:0;display:grid;place-items:center;gap:12px;background:rgba(9,11,16,.72);
  backdrop-filter:blur(3px);color:var(--txt);font-size:14px;z-index:5;grid-auto-flow:row}
.spinner{width:34px;height:34px;border-radius:50%;border:2.5px solid rgba(255,255,255,.15);border-top-color:var(--acc);
  animation:spin .8s linear infinite}
@keyframes spin{to{transform:rotate(360deg)}}

/* ---------------- side panel ---------------- */
.side{display:flex;flex-direction:column;border-left:1px solid var(--line);background:var(--panel);min-width:0}
.side-tabs{display:flex;gap:4px;padding:8px;border-bottom:1px solid var(--line)}
.side-tab{flex:1;height:34px;border-radius:9px;color:var(--mut);font-size:13.5px;font-weight:600}
.side-tab.active{background:var(--panel2);color:var(--txt);box-shadow:inset 0 0 0 1px var(--line2)}
.panel{flex:1;min-height:0;overflow-y:auto;padding:14px;display:flex;flex-direction:column;gap:12px}
#panel-chat{overflow:hidden;padding-bottom:10px}
.tool-pane{display:flex;flex-direction:column;gap:10px}
.tool-pane h3{font-size:13px;text-transform:uppercase;letter-spacing:.06em;color:var(--mut2)}
.tool-pane h3.mt{margin-top:6px}
.hint{color:var(--mut2);font-size:12.5px;margin:0}
.mono{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px}
textarea,input[type=text],input[type=password],input[type=number],select{
  width:100%;background:var(--bg2);border:1px solid var(--line);border-radius:10px;padding:10px 12px;
  outline:none;transition:border-color .15s;
}
textarea{min-height:78px;resize:vertical}
textarea:focus,input:focus,select:focus{border-color:var(--acc)}
input[type=color]{width:100%;height:40px;background:var(--bg2);border:1px solid var(--line);border-radius:10px;padding:3px}
.field-row{display:grid;grid-template-columns:1fr 1fr;gap:10px}
.field-row label,.tool-pane>label{display:flex;flex-direction:column;gap:6px;font-size:12.5px;color:var(--mut)}
.check{display:flex;align-items:center;gap:8px;font-size:13px;color:var(--mut)}
.check input{accent-color:var(--acc)}
.row{display:flex;gap:8px;flex-wrap:wrap}
.row .primary,.row .ghost{flex:1;min-width:120px}
.btn-grid{display:grid;grid-template-columns:1fr 1fr;gap:8px}
.btn-grid .ghost{height:38px;font-size:13px}
.divider{height:1px;background:var(--line);margin:6px 0}
.chips{display:flex;flex-wrap:wrap;gap:6px}
.chip{
  height:30px;padding:0 11px;border-radius:999px;border:1px solid var(--line);background:var(--bg2);
  color:var(--mut);font-size:12.5px;font-weight:600;transition:.15s;
}
.chip:hover{color:var(--txt);border-color:var(--line2)}
.chip.active{background:rgba(109,139,255,.16);border-color:rgba(109,139,255,.5);color:#cdd7ff}
.slider{display:grid;grid-template-columns:86px 1fr 40px;align-items:center;gap:8px;font-size:12.5px;color:var(--mut)}
.slider output{text-align:right;color:var(--txt);font-variant-numeric:tabular-nums;font-size:12px}
input[type=range]{-webkit-appearance:none;height:4px;border-radius:99px;background:var(--line2);outline:none}
input[type=range]::-webkit-slider-thumb{-webkit-appearance:none;width:15px;height:15px;border-radius:50%;
  background:linear-gradient(140deg,var(--acc),var(--acc2));border:2px solid #0e1219;cursor:pointer}
input[type=range]::-moz-range-thumb{width:13px;height:13px;border-radius:50%;background:var(--acc);border:2px solid #0e1219}

.preset-grid{display:grid;grid-template-columns:repeat(2,1fr);gap:8px}
.preset{position:relative;border-radius:11px;overflow:hidden;border:1px solid var(--line);background:var(--bg2);
  padding:0;aspect-ratio:4/3;transition:.15s}
.preset canvas{width:100%;height:100%;object-fit:cover}
.preset span{position:absolute;left:0;right:0;bottom:0;padding:6px 8px;font-size:11.5px;font-weight:600;
  background:linear-gradient(0deg,rgba(0,0,0,.8),transparent);text-align:left}
.preset:hover{border-color:var(--acc);transform:translateY(-1px)}

/* ---------------- chat ---------------- */
.chat-log{flex:1;min-height:0;overflow-y:auto;display:flex;flex-direction:column;gap:10px;padding-right:2px}
.msg{display:flex;gap:8px;max-width:100%}
.msg .bubble{
  background:var(--panel2);border:1px solid var(--line);border-radius:14px 14px 14px 4px;
  padding:9px 12px;font-size:13.5px;line-height:1.45;white-space:pre-wrap;word-break:break-word;
}
.msg.user{justify-content:flex-end}
.msg.user .bubble{background:linear-gradient(140deg,rgba(109,139,255,.9),rgba(143,107,255,.85));
  border-color:transparent;color:#fff;border-radius:14px 14px 4px 14px}
.msg.bot .bubble{max-width:100%}
.msg .tool{
  display:inline-flex;align-items:center;gap:6px;margin-top:6px;padding:4px 9px;border-radius:8px;
  background:rgba(53,211,154,.12);border:1px solid rgba(53,211,154,.3);color:#8ff0c6;font-size:11.5px;font-weight:600;
}
.msg .tool.err{background:rgba(255,107,107,.1);border-color:rgba(255,107,107,.3);color:#ffb3b3}
.typing{display:inline-flex;gap:3px}
.typing i{width:5px;height:5px;border-radius:50%;background:var(--mut);animation:blink 1.2s infinite}
.typing i:nth-child(2){animation-delay:.2s}.typing i:nth-child(3){animation-delay:.4s}
@keyframes blink{0%,60%,100%{opacity:.25}30%{opacity:1}}
.chat-quick{display:flex;gap:6px;overflow-x:auto;padding-bottom:2px;scrollbar-width:none}
.chat-quick::-webkit-scrollbar{display:none}
.chat-input{display:flex;gap:8px;align-items:flex-end;border-top:1px solid var(--line);padding-top:10px}
.chat-input textarea{min-height:42px;max-height:140px;border-radius:12px;resize:none}

/* ---------------- modals ---------------- */
.modal{position:fixed;inset:0;background:rgba(6,8,12,.72);backdrop-filter:blur(6px);display:grid;place-items:center;
  z-index:50;padding:20px}
.modal[hidden]{display:none}
.modal-card{width:min(440px,100%);background:var(--panel);border:1px solid var(--line2);border-radius:18px;
  padding:22px;display:flex;flex-direction:column;gap:12px;box-shadow:var(--shadow)}
.modal-card h2{font-size:18px}
.modal-card p{margin:0;color:var(--mut);font-size:13.5px}

/* ---------------- toast ---------------- */
.toast{position:fixed;left:50%;bottom:26px;transform:translateX(-50%);background:#1b2130;border:1px solid var(--line2);
  color:var(--txt);padding:10px 16px;border-radius:12px;font-size:13.5px;z-index:80;box-shadow:var(--shadow);
  max-width:min(560px,90vw);text-align:center}
.toast.err{border-color:rgba(255,107,107,.5);background:#2a1618}

/* ---------------- responsive ---------------- */
@media (max-width:1100px){
  .shell{grid-template-columns:66px minmax(0,1fr) 330px}
}
@media (max-width:860px){
  .stage-hint{display:none}
  .topbar{overflow:hidden}
  .brand-text{display:none}
  .top-actions{gap:6px}
  .chip{height:34px;padding:0 13px}
  .zoom .ghost{height:32px}
  .zoom .ghost.icon{width:32px}
  .chat-input textarea{font-size:16px}
  body{overflow:auto}
  .shell{grid-template-columns:1fr;grid-template-rows:auto minmax(52vh,1fr) auto;height:auto;min-height:calc(100dvh - 58px)}
  .rail{flex-direction:row;border-right:0;border-bottom:1px solid var(--line);overflow-x:auto;padding:8px;gap:2px}
  .rail-btn{min-width:62px;flex:0 0 auto}
  .stage{order:1;min-height:46vh}
  .canvas-wrap{padding:14px}
  .side{order:2;border-left:0;border-top:1px solid var(--line);max-height:none}
  .panel{max-height:none;overflow:visible}
  .chat-log{max-height:44vh}
  .topbar{padding:0 10px;gap:8px}
  .doc-info{display:none}
  .brand-text small{display:none}
  .top-actions .primary span{display:none}
  .top-actions .primary{width:40px;padding:0}
  #keyLabel{display:none}
  #btnKey{padding:0 10px}
}

.color-row{display:flex;align-items:center;gap:8px}
.color-row code{font-size:12px;color:var(--mut);letter-spacing:.02em}
.seg-row{display:flex;gap:6px}
.seg-btn{flex:1;height:36px;border-radius:10px;border:1px solid var(--line);background:var(--bg2);
  color:var(--mut);font-size:15px;font-weight:600;transition:.15s}
.seg-btn:hover{color:var(--txt);border-color:var(--line2)}
.seg-btn.active{background:rgba(109,139,255,.16);border-color:rgba(109,139,255,.5);color:#cdd7ff}

.msg .bubble .bubble-text{margin-top:8px}
.msg .bubble strong{font-weight:700;color:#fff}
.msg .bubble code{background:rgba(255,255,255,.1);padding:1px 5px;border-radius:5px;font-size:12.5px}
