:root {
  --bg: #eef1f4; --surface: #ffffff; --surface-2: #f5f7f9; --line: #d9dee4; --text: #17202a; --muted: #5e6b78;
  --accent: #0b7a75; --accent-2: #0e9f98; --accent-ink: #ffffff; --danger: #c62828; --warn: #b77900;
  --shadow: 0 6px 24px rgba(16, 24, 40, .12); --radius: 8px; --top: 52px; --status: 26px; --tools: 64px; --panel: 330px;
  --font: "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141a; --surface: #171e26; --surface-2: #1d2630; --line: #2b3642; --text: #e6edf3; --muted: #93a1b0;
    --accent: #19b3aa; --accent-2: #2fd0c6; --accent-ink: #04201e; --danger: #ff6b6b; --warn: #f0b429;
    --shadow: 0 8px 28px rgba(0, 0, 0, .45); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141a; --surface: #171e26; --surface-2: #1d2630; --line: #2b3642; --text: #e6edf3; --muted: #93a1b0;
  --accent: #19b3aa; --accent-2: #2fd0c6; --accent-ink: #04201e; --danger: #ff6b6b; --warn: #f0b429;
  --shadow: 0 8px 28px rgba(0, 0, 0, .45); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.4 var(--font); overflow: hidden; overscroll-behavior: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
svg .fill { fill: currentColor; }
kbd { font: 11px/1 ui-monospace, Menlo, Consolas, monospace; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 2px 4px; margin-left: auto; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

#app { display: grid; grid-template-rows: var(--top) 1fr var(--status); height: 100%; height: 100dvh; }

/* ---------- top bar ---------- */
#top { display: flex; align-items: center; gap: 10px; padding: 0 10px; background: var(--surface); border-bottom: 1px solid var(--line); min-width: 0; position: relative; z-index: 20; }
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; letter-spacing: .2px; white-space: nowrap; }
.doc-name { min-width: 60px; width: 180px; border: 1px solid transparent; background: transparent; padding: 6px 8px; border-radius: 6px; font-weight: 600; }
.doc-name:hover, .doc-name:focus { border-color: var(--line); background: var(--surface-2); outline: none; }
.menu { display: flex; gap: 2px; }
.dd { position: relative; }
.dd > button { background: none; border: 0; padding: 8px 10px; border-radius: 6px; color: var(--text); }
.dd > button:hover, .dd.open > button { background: var(--surface-2); }
.dd-menu { display: none; position: absolute; top: calc(100% + 4px); left: 0; min-width: 260px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 6px; z-index: 50; }
.dd.open .dd-menu { display: block; }
.dd-menu button { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; background: none; border: 0; padding: 8px 10px; border-radius: 6px; min-height: 36px; }
.dd-menu button:hover, .dd-menu button:focus-visible { background: var(--surface-2); outline: none; }
.dd-menu hr { border: 0; border-top: 1px solid var(--line); margin: 6px 4px; }
.top-actions { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.icon-btn { display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: 8px; border: 1px solid transparent; background: none; color: var(--text); }
.icon-btn:hover { background: var(--surface-2); border-color: var(--line); }
.btn { display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface-2); color: var(--text); font-weight: 600; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: var(--accent-2); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
.erc-btn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.erc-btn.ok .dot { background: #22a05a; }
.erc-btn.bad .dot { background: var(--danger); }
.erc-btn.warn .dot { background: var(--warn); }
.erc-btn .count:empty { display: none; }
.erc-btn .count { font-size: 12px; background: var(--danger); color: #fff; border-radius: 10px; padding: 0 6px; }
.mobile-only { display: none !important; }

/* ---------- layout ---------- */
#main { display: grid; grid-template-columns: var(--tools) 1fr var(--panel); min-height: 0; }
#tools { display: flex; flex-direction: column; gap: 2px; padding: 6px; background: var(--surface); border-right: 1px solid var(--line); overflow-y: auto; }
#tools button { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 7px 2px; border: 1px solid transparent; border-radius: 8px; background: none; color: var(--text); font-size: 10.5px; line-height: 1.1; min-height: 52px; }
#tools button:hover { background: var(--surface-2); }
#tools button.active { background: color-mix(in srgb, var(--accent) 16%, transparent); border-color: color-mix(in srgb, var(--accent) 45%, transparent); color: var(--accent); }
#tools svg { width: 22px; height: 22px; }

#stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; cursor: default; }
#canvas.cross { cursor: crosshair; }
#canvas.grab { cursor: grab; }
#canvas.grabbing { cursor: grabbing; }
#canvas.move { cursor: move; }
.hint { position: absolute; left: 50%; top: 10px; transform: translateX(-50%); background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); padding: 6px 12px; border-radius: 20px; font-size: 12.5px; pointer-events: none; max-width: calc(100% - 24px); text-align: center; }
.zoom-ctl { position: absolute; right: 10px; bottom: 10px; display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); overflow: hidden; }
.zoom-ctl button { width: 40px; height: 40px; border: 0; background: none; font-size: 20px; display: grid; place-items: center; color: var(--text); }
.zoom-ctl button + button { border-top: 1px solid var(--line); }
.zoom-ctl button:hover { background: var(--surface-2); }

/* ---------- side panel ---------- */
#panel { display: flex; flex-direction: column; background: var(--surface); border-left: 1px solid var(--line); min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 6px; align-items: center; }
.tabs > button[data-tab] { flex: 1; background: none; border: 0; border-bottom: 2px solid transparent; padding: 12px 6px 10px; color: var(--muted); font-weight: 600; }
.tabs > button.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { flex: 1; overflow-y: auto; min-height: 0; padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.search input { width: 100%; height: 40px; border: 1px solid var(--line); border-radius: 8px; padding: 0 12px; background: var(--surface-2); }
.search input:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button { border: 1px solid var(--line); background: var(--surface-2); border-radius: 16px; padding: 4px 10px; font-size: 12px; min-height: 30px; }
.chips button:hover { border-color: var(--accent); }
.lib-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; min-height: 120px; border: 1px solid var(--line); border-radius: 8px; }
.lib-list li { padding: 8px 10px; border-bottom: 1px solid var(--line); cursor: pointer; display: grid; gap: 1px; }
.lib-list li:last-child { border-bottom: 0; }
.lib-list li:hover { background: var(--surface-2); }
.lib-list li.sel { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.lib-list .nm { font-weight: 600; overflow-wrap: anywhere; }
.lib-list .lb { font-size: 11.5px; color: var(--muted); }
.lib-list .ds { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-preview { border: 1px solid var(--line); border-radius: 8px; padding: 8px; display: grid; gap: 8px; background: var(--surface-2); }
.lib-preview canvas { width: 100%; height: 170px; background: var(--surface); border-radius: 6px; }
.pv-info { display: grid; gap: 2px; }
.pv-info p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); max-height: 54px; overflow: auto; }
.pv-actions { display: flex; gap: 8px; }
.pv-actions .btn { flex: 1; justify-content: center; }
.pv-actions select { height: 36px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); padding: 0 8px; }

.props { display: grid; gap: 10px; }
.props h3 { margin: 0; font-size: 15px; }
.props .sub { margin-top: -6px; font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.field { display: grid; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input[type=text], .field input:not([type]), .field select, .field textarea, .dlg-in { width: 100%; height: 38px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; background: var(--surface-2); }
.field textarea { height: 80px; padding: 8px 10px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .dlg-in:focus { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); border-color: var(--accent); }
.field .row { display: flex; gap: 6px; align-items: center; }
.field .row input { flex: 1; }
.eye { width: 38px; height: 38px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-2); display: grid; place-items: center; flex: none; }
.eye.off { opacity: .45; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.checks label { display: flex; align-items: center; gap: 6px; min-height: 30px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; }
.netinfo { font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; overflow-wrap: anywhere; }
.empty { padding: 20px 4px; text-align: center; }

.erc-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.erc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.erc-list li { border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: 6px; padding: 8px 10px; cursor: pointer; font-size: 13px; background: var(--surface-2); }
.erc-list li.error { border-left-color: var(--danger); }
.erc-list li.muted { cursor: default; border-left-color: var(--line); }
.erc-list li.okmsg { border-left-color: #22a05a; cursor: default; }
.erc-list li:hover:not(.muted):not(.okmsg) { border-color: var(--accent); }
.erc-list .code { font-size: 11px; color: var(--muted); display: block; }

/* ---------- status ---------- */
#status { display: flex; align-items: center; gap: 18px; padding: 0 12px; background: var(--surface); border-top: 1px solid var(--line); font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; }
#status .push { margin-left: auto; }
#stTool { color: var(--text); font-weight: 600; }

/* ---------- dialog / toast ---------- */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--text); box-shadow: var(--shadow); padding: 0; width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 40px); }
dialog::backdrop { background: rgba(10, 15, 20, .45); }
dialog form { padding: 18px; display: grid; gap: 12px; }
dialog h2 { margin: 0; font-size: 18px; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 4px 0 0; padding: 0; flex-wrap: wrap; }
#dlgBody { display: grid; gap: 10px; max-height: 60dvh; overflow-y: auto; }
#dlgBody p { margin: 0; }
.recent-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.recent-list li { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); border-radius: 8px; padding: 6px 6px 6px 12px; }
.recent-list li .meta { flex: 1; min-width: 0; }
.recent-list li .meta b { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.recent-list li .meta span { font-size: 12px; color: var(--muted); }
.recent-list .btn { height: 34px; }
.keys { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 13px; }
.keys kbd { margin: 0; justify-self: start; }
#toast { position: fixed; left: 50%; bottom: calc(var(--status) + 16px); transform: translateX(-50%) translateY(20px); opacity: 0; background: #17202a; color: #fff; padding: 10px 16px; border-radius: 10px; box-shadow: var(--shadow); transition: .2s; pointer-events: none; z-index: 100; max-width: calc(100vw - 24px); text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: var(--danger); }
.drop-hint { position: absolute; inset: 12px; border: 3px dashed var(--accent); border-radius: 16px; display: grid; place-items: center; background: color-mix(in srgb, var(--accent) 10%, transparent); font-size: 18px; font-weight: 700; color: var(--accent); pointer-events: none; }

/* ---------- tablet ---------- */
@media (max-width: 1100px) { :root { --panel: 290px; } .doc-name { width: 130px; } }

/* ---------- phone ---------- */
@media (max-width: 760px) {
  :root { --top: 52px; --status: 0px; }
  .mobile-only { display: inline-grid !important; }
  .float-actions.mobile-only { display: flex !important; }
  .float-actions.mobile-only[hidden] { display: none !important; }
  .brand span, .btn .lbl, .hide-sm { display: none; }
  .erc-btn .lbl { display: inline; }
  .top-actions .btn, .top-actions .icon-btn { height: 44px; min-width: 44px; }
  #toast { bottom: calc(76px + env(safe-area-inset-bottom)); }
  .doc-name { flex: 1; width: auto; min-width: 0; }
  .menu { display: none; position: absolute; top: var(--top); right: 6px; left: 6px; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); padding: 6px; max-height: calc(100dvh - var(--top) - 80px); overflow-y: auto; }
  .menu.open { display: flex; }
  .dd > button { width: 100%; text-align: left; padding: 12px; font-weight: 700; }
  .dd-menu { position: static; display: block; box-shadow: none; border: 0; padding: 0 0 6px 8px; min-width: 0; }
  .dd-menu kbd { display: none; }
  .dd-menu button { min-height: 44px; }
  #top .icon-btn[data-cmd] { display: none; }
  #status { display: none; }
  #main { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #tools { grid-row: 2; flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-top: 1px solid var(--line); padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); gap: 2px; scrollbar-width: none; }
  #tools::-webkit-scrollbar { display: none; }
  #tools button { min-width: 62px; min-height: 54px; flex: none; }
  #stage { grid-row: 1; }
  #panel { position: fixed; left: 0; right: 0; bottom: 0; height: min(72dvh, 560px); z-index: 30; border-left: 0; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0; box-shadow: 0 -10px 30px rgba(0,0,0,.25); transform: translateY(105%); transition: transform .22s ease; padding-bottom: env(safe-area-inset-bottom); }
  #panel.open { transform: none; }
  .panel-grip { display: flex !important; justify-content: center; padding: 8px 0 2px; }
  .panel-grip span { width: 40px; height: 4px; border-radius: 2px; background: var(--line); }
  .close-panel { width: 40px; height: 40px; }
  .float-actions { position: absolute; left: 8px; right: 8px; bottom: 8px; gap: 6px; justify-content: center; flex-wrap: wrap; }
  .float-actions button { min-height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); box-shadow: var(--shadow); font-weight: 600; }
  .float-actions button.danger { color: var(--danger); }
  .zoom-ctl { bottom: auto; top: 10px; right: 8px; }
  .zoom-ctl button { width: 44px; height: 44px; }
  .hint { top: auto; bottom: 64px; }
}
