:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --line: #e3e6ea;
  --stripe: #fafbfc;
  --accent: #2563eb;
  --critical: #dc2626;
  --float: #cbd5e1;
  --select: #dbeafe;
  --error: #b42318;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --ok: #15803d;
  --off: rgba(100, 116, 139, .12);
  --ghost: #94a3b8;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1218;
    --panel: #161a22;
    --text: #e6e9ef;
    --muted: #8b93a3;
    --line: #262c37;
    --stripe: #1a1f28;
    --accent: #60a5fa;
    --critical: #f87171;
    --float: #3b4555;
    --select: #1e3a5f;
    --error: #fda29b;
    --warn-bg: #3a2a0c;
    --warn-text: #fcd34d;
    --ok: #4ade80;
    --off: rgba(148, 163, 184, .10);
    --ghost: #64748b;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

body { display: flex; flex-direction: column; }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.brand { font-size: 15px; }

.badge {
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .03em;
  text-transform: uppercase;
}
.badge.demo { background: var(--warn-bg); color: var(--warn-text); }
.badge.project { background: var(--select); color: var(--accent); }
.badge.tutorial { background: var(--ok); color: var(--panel); }

.group {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding-left: 12px;
  border-left: 1px solid var(--line);
}

button, .button, select, input, textarea { font: inherit; color: inherit; }

button, .button {
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  cursor: pointer;
}
button:hover, .button:hover { border-color: var(--accent); }
button:disabled { opacity: .5; cursor: progress; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.link { border: 0; background: none; color: var(--accent); padding: 0 4px; }
button.danger { color: var(--critical); border-color: var(--critical); background: var(--panel); }
button.danger:hover { background: var(--critical); color: var(--panel); }
button.learn { border-color: var(--ok); color: var(--ok); font-weight: 600; }
button.icon-btn { padding: 2px 9px; font-size: 15px; line-height: 1.2; }
button.icon { padding: 0 6px; border: 0; background: none; color: var(--muted); font-size: 15px; line-height: 1; }
button.icon:hover { color: var(--critical); }
.muted { color: var(--muted); }
.spacer-flex { flex: 1; }

input, select, textarea {
  padding: 3px 6px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  min-width: 0;
}

.stats {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  padding: 6px 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
}
.stats b { color: var(--text); font-variant-numeric: tabular-nums; }
.stats b.bad { color: var(--critical); }
.stats b.good { color: var(--ok); }

.layout { flex: 1; display: flex; min-height: 0; }

.center { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.workspace { position: relative; flex: 1; min-height: 160px; background: var(--panel); }

#canvas { position: absolute; inset: 0; pointer-events: none; }

.viewport { position: absolute; inset: 0; overflow: auto; outline: none; }
.spacer { width: 1px; height: 1px; }

.cell-editor {
  position: absolute;
  z-index: 2;
  height: 24px;
  padding: 0 4px;
  border: 2px solid var(--accent);
  border-radius: 0;
  text-align: right;
}

.histo {
  height: 190px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-top: 1px solid var(--line);
  background: var(--panel);
}
.histo.collapsed { height: auto; }
.histo.collapsed canvas { display: none; }
.histo-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 14px;
  padding: 4px 12px;
  color: var(--muted);
}
.histo canvas { flex: 1; min-height: 0; width: 100%; }
.legend { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; }
.sw { display: inline-block; width: 10px; height: 10px; margin-left: 6px; border-radius: 2px; }
.sw-load { background: var(--accent); }
.sw-over { background: var(--critical); }
.sw-cap { height: 2px; background: var(--text); }
.sw-off { background: var(--off); border: 1px solid var(--line); }

.side {
  width: 340px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--line);
  background: var(--panel);
  min-height: 0;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: 6px 4px 0;
  border-bottom: 1px solid var(--line);
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
}
.tabs button {
  border: 0;
  border-bottom: 2px solid transparent;
  border-radius: 0;
  padding: 6px 6px;
  color: var(--muted);
  white-space: nowrap;
  background: none;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

.tab-panel { overflow-y: auto; padding: 4px 12px 16px; flex: 1; }

.side h2, .side h3 {
  margin: 14px 0 6px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
}
.side h3 { text-transform: none; letter-spacing: 0; font-size: 12px; }

.hint { color: var(--muted); margin: 8px 0; }

.form { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px; }
.form label { display: flex; flex-direction: column; gap: 2px; color: var(--muted); font-size: 12px; }
.form label input, .form label select, .form label textarea { color: var(--text); font-size: 13px; }
.form .span2, .form textarea { grid-column: span 2; }
.form label.inline { flex-direction: row; align-items: center; gap: 6px; }
.form label.inline input { flex: 1; min-width: 0; }
.form label.row { flex-direction: row; align-items: center; gap: 6px; }
.form button.primary { font-weight: 600; }
label.block { display: flex; flex-direction: column; gap: 2px; color: var(--muted); margin: 6px 0; }

.week { display: grid; grid-template-columns: 38px 1fr; gap: 4px 6px; align-items: center; }
.week span { color: var(--muted); font-size: 12px; }

.presets { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; color: var(--muted); margin-bottom: 6px; }
.presets button { padding: 2px 8px; font-size: 12px; }

.details dl { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.details ul { margin: 6px 0 0; padding-left: 16px; }
.details a, .plain a { color: var(--accent); cursor: pointer; }

.plain { list-style: none; margin: 0 0 6px; padding: 0; }
.plain li { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 2px 0; border-bottom: 1px solid var(--line); }
.plain li a { flex: 1; min-width: 0; }

.details h3 { margin: 10px 0 2px; }
.details dd.bad { color: var(--critical); }
.links { list-style: none; padding: 0 !important; }
.links li { display: flex; align-items: center; gap: 4px; padding: 2px 0; border-bottom: 1px solid var(--line); }
.links li a { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.links .role { width: 44px; flex-shrink: 0; color: var(--muted); font-size: 12px; }
select.mini, input.mini { padding: 1px 3px; font-size: 12px; }
input.mini.lag { width: 52px; text-align: right; }

.table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.table th, .table td { text-align: left; padding: 3px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--muted); font-weight: 600; font-size: 12px; }
.table td.num, .table th.num { text-align: right; }
.table tr.clickable { cursor: pointer; }
.table tr.clickable:hover td { background: var(--stripe); }
.table tr.current td { background: var(--select); }
.table .bad { color: var(--critical); font-weight: 600; }

.spark { display: inline-flex; align-items: flex-end; gap: 1px; height: 18px; vertical-align: middle; }
.spark i { display: block; width: 5px; background: var(--accent); border-radius: 1px 1px 0 0; }

.codeset { margin: 8px 0 12px; }
.codeset b { display: block; margin-bottom: 2px; }
.codeset .chip { display: inline-flex; align-items: center; gap: 2px; margin: 2px 4px 2px 0; padding: 1px 2px 1px 6px; border: 1px solid var(--line); border-radius: 4px; font-size: 12px; }
.codeset b button.link { font-weight: 400; font-size: 12px; color: var(--critical); }

.log { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 12px; }
.log .error { color: var(--error); }

dialog {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(15, 18, 24, .45); }

.guide { width: min(860px, calc(100vw - 32px)); height: min(720px, calc(100vh - 32px)); }
.guide[open] { display: grid; grid-template-columns: 200px 1fr; grid-template-rows: auto 1fr; }
.guide-close { grid-column: 1 / -1; display: flex; justify-content: flex-end; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.guide-nav { display: flex; flex-direction: column; gap: 6px; padding: 14px; border-right: 1px solid var(--line); overflow-y: auto; }
.guide-nav a { color: var(--accent); text-decoration: none; }
.guide-body { overflow-y: auto; padding: 4px 24px 24px; line-height: 1.55; font-size: 14px; }
.guide-body h1 { font-size: 22px; margin: 16px 0 8px; }
.guide-body h2 { font-size: 17px; margin: 30px 0 8px; padding-top: 14px; border-top: 1px solid var(--line); }
.guide-body h3 { font-size: 14px; margin: 16px 0 4px; }
.guide-body code, .coach code { padding: 0 4px; border-radius: 4px; background: var(--stripe); border: 1px solid var(--line); font-size: 12px; }
.guide-body .example { padding: 8px 12px; border-left: 3px solid var(--accent); background: var(--stripe); }
.guide-body .table { font-size: 13px; }
.choices { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 6px; }
.choices button { padding: 8px 14px; }
.link-types { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.link-types figure { margin: 0; padding: 8px; border: 1px solid var(--line); border-radius: 8px; }
.link-types figcaption { font-size: 13px; }
.lt { width: 100%; max-width: 260px; display: block; }
.lt rect.a { fill: var(--accent); }
.lt rect.b { fill: var(--ok); }
.lt path { fill: none; stroke: var(--text); stroke-width: 1.4; }
.lt path.head { fill: var(--text); stroke: none; }
.lt text { fill: var(--muted); font-size: 10px; }
.glossary dt { font-weight: 600; margin-top: 10px; }
.glossary dd { margin: 2px 0 0; color: var(--text); }

.small-dialog { width: min(380px, calc(100vw - 32px)); }
.small-dialog .form { padding: 16px; }
.small-dialog h2 { margin: 0 0 4px; font-size: 16px; }

/* ---- toolbar menus */
.toolbar { gap: 8px 10px; }
.toolbar .group { padding-left: 10px; }
.tight-group { gap: 2px; }
details.menu { position: relative; }
details.menu > summary { list-style: none; cursor: pointer; user-select: none; }
details.menu > summary::-webkit-details-marker { display: none; }
details.menu > summary.button::after { content: " ▾"; color: var(--muted); font-size: 11px; }
details.menu[open] > summary.button { border-color: var(--accent); background: var(--select); }
.menu-panel {
  position: absolute; z-index: 40; top: calc(100% + 6px); left: 0;
  display: flex; flex-direction: column; gap: 2px; min-width: 240px; padding: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, .2);
}
.menu-right .menu-panel { left: auto; right: 0; }
.menu-panel hr { width: 100%; border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.menu-item {
  display: flex; justify-content: space-between; align-items: baseline; gap: 12px;
  width: 100%; padding: 6px 10px; border: 0; border-radius: 6px; background: none;
  text-align: left; cursor: pointer; white-space: nowrap;
}
.menu-item:hover { background: var(--select); }
.menu-item small { color: var(--muted); font-size: 11px; }
.learn-item { color: var(--ok); font-weight: 600; }
.menu-label { padding: 4px 10px 0; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
.menu-note { margin: 4px 10px 6px; max-width: 280px; color: var(--muted); white-space: normal; }
.menu-row { display: flex; gap: 6px; padding: 2px 6px 4px; }
.menu-row select { flex: 1; }
.menu-field { display: flex; flex-direction: column; gap: 2px; padding: 4px 10px; color: var(--muted); font-size: 12px; }
.menu-field input, .menu-field select { color: var(--text); font-size: 13px; }
.menu-check { display: flex; align-items: center; gap: 8px; padding: 5px 10px; border-radius: 6px; cursor: pointer; white-space: nowrap; }
.menu-check:hover { background: var(--select); }
.col-list { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; padding: 2px 10px 6px; }
.col-list label { display: flex; align-items: center; gap: 6px; white-space: nowrap; font-size: 12px; }
.mode-menu > summary.badge { cursor: pointer; }
.mode-menu > summary.badge::after { content: " ▾"; }
.zoom-group input[type=range] { width: 120px; vertical-align: middle; }

/* ---- side panel sections */
details.section { border-bottom: 1px solid var(--line); }
details.section > summary {
  display: flex; align-items: center; gap: 6px; padding: 10px 0 8px;
  list-style: none; cursor: pointer; user-select: none;
  font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted);
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::before { content: "▸"; display: inline-block; width: 10px; transition: transform .15s; }
details.section[open] > summary::before { transform: rotate(90deg); }
details.section > summary:hover { color: var(--text); }
details.section[open] { padding-bottom: 10px; }
details.section .count { margin-left: auto; font-weight: 500; text-transform: none; letter-spacing: 0; color: var(--muted); }
#links-list { max-height: 240px; overflow-y: auto; margin: 0; }

/* ---- collapsible side panel */
.side { transition: width .15s ease; }
.side-toggle { margin-right: 4px; font-size: 16px; }
.side.collapsed { width: 36px; }
.side.collapsed .tabs { flex-direction: column; border-bottom: 0; padding: 6px 2px; }
.side.collapsed .tabs button:not(.side-toggle), .side.collapsed .tab-panel { display: none !important; }

/* ---- histogram resize handle */
.histo-resize { height: 5px; margin-top: -3px; cursor: ns-resize; flex-shrink: 0; }
.histo-resize:hover { background: var(--accent); }

/* ---- leveling progress */
.progress {
  position: absolute; z-index: 3; left: 50%; top: 48px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .18);
}
.progress .bar { width: 160px; height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; }
.progress .bar i { display: block; height: 100%; width: 0; background: var(--accent); }

/* ---- toast */
.toast {
  position: fixed; z-index: 30; left: 50%; bottom: 20px; transform: translateX(-50%);
  display: flex; align-items: center; gap: 12px; max-width: min(640px, calc(100vw - 32px));
  padding: 8px 10px 8px 14px; border-radius: 8px;
  background: var(--text); color: var(--panel);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .25);
}
.toast.error { background: var(--critical); color: #fff; }
.toast button { background: transparent; color: inherit; border-color: currentColor; font-weight: 600; }

/* ---- tutorial coach */
.coach {
  position: fixed; z-index: 25; left: 16px; bottom: 16px;
  width: min(420px, calc(100vw - 32px)); max-height: min(70vh, 640px);
  display: flex; flex-direction: column;
  background: var(--panel); border: 1px solid var(--line); border-top: 4px solid var(--ok); border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, .28);
}
.coach header, .coach footer { display: flex; align-items: center; gap: 6px; padding: 8px 12px; }
.coach footer { border-top: 1px solid var(--line); }
.coach h2 { margin: 0; padding: 0 12px 4px; font-size: 16px; }
.coach-body { overflow-y: auto; padding: 0 12px 8px; font-size: 13.5px; line-height: 1.5; }
.coach-body p { margin: 6px 0; }
.coach-after { margin-top: 8px; padding: 4px 10px; border-left: 3px solid var(--ok); background: var(--stripe); }
#coach-status { font-size: 12px; color: var(--muted); }
#coach-status.ok { color: var(--ok); font-weight: 600; }
.coach.min .coach-body, .coach.min h2 { display: none; }
.coach-buttons { display: flex; flex-wrap: wrap; gap: 6px; }
.tight { margin: 4px 0; padding-left: 20px; }
.tight li { margin: 2px 0; }
.mini-table { border-collapse: collapse; margin: 6px 0; font-size: 12.5px; }
.mini-table td { padding: 2px 6px 2px 0; vertical-align: top; border-bottom: 1px solid var(--line); }
.coach-target { outline: 3px solid var(--ok) !important; outline-offset: 2px; border-radius: 6px; animation: coach-pulse 1.6s ease-in-out infinite; }
@keyframes coach-pulse { 50% { outline-color: transparent; } }
@media (prefers-reduced-motion: reduce) { .coach-target { animation: none; } }

@media (max-width: 900px) {
  .coach { left: 8px; right: 8px; bottom: 8px; width: auto; }
  .layout { flex-direction: column; overflow-y: auto; }
  .center { min-height: 70vh; }
  .side { width: auto; border-left: 0; border-top: 1px solid var(--line); }
  .guide[open] { grid-template-columns: 1fr; }
  .guide-nav { flex-direction: row; flex-wrap: wrap; border-right: 0; border-bottom: 1px solid var(--line); }
}
