:root {
  --bg: #15181d;
  --panel: #1d2128;
  --panel-2: #242933;
  --border: #323946;
  --text: #e7eaf0;
  --muted: #9aa3b2;
  --accent: #6aa7ff;
  --accent-strong: #3d86f5;
  --accent-text: #0b1526;
  --ok: #4cc38a;
  --warn: #f2b84b;
  --danger: #ff6b6b;
  --radius: 10px;
  --shadow: 0 6px 24px rgb(0 0 0 / 0.25);
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f5f8;
    --panel: #ffffff;
    --panel-2: #f0f2f6;
    --border: #d9dee7;
    --text: #1b2230;
    --muted: #5d6778;
    --accent: #2f6fe0;
    --accent-strong: #1f5bc4;
    --accent-text: #ffffff;
    --shadow: 0 6px 24px rgb(20 30 50 / 0.08);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
}
h1, h2, h3 { margin: 0 0 0.5em; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1rem; }
a { color: var(--accent); }
code, pre { font-family: var(--mono); font-size: 0.85em; }
[hidden] { display: none !important; }

.muted { color: var(--muted); }
.small { font-size: 0.85em; }
.break { word-break: break-all; }
.offscreen { position: fixed; left: -9999px; }

/* Buttons & forms */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4em;
  padding: 0.55em 1em;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { background: var(--accent-strong); }
.btn.ghost { background: transparent; }
.btn.small { padding: 0.35em 0.7em; font-size: 0.85em; }
.btn.block { width: 100%; }
.btn.danger { color: var(--danger); }
.btn:focus-visible, input:focus-visible, .tab:focus-visible, .fileitem:focus-visible, .navbtn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.field { display: flex; flex-direction: column; gap: 0.3em; margin-bottom: 0.9em; flex: 1; min-width: 200px; }
.field > span { font-size: 0.85em; color: var(--muted); }
input[type='text'], input[type='email'], input[type='password'], input[type='search'] {
  font: inherit;
  padding: 0.6em 0.75em;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  width: 100%;
}
.form-row { display: flex; gap: 1em; flex-wrap: wrap; }
.form-error { color: var(--danger); min-height: 1.2em; margin: 0 0 0.8em; font-size: 0.9em; }

/* Auth page */
.auth-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 0.6em; }
.brand-lg { margin-bottom: 1.4em; }
.brand-lg h1 { margin: 0; }
.brand-lg p { margin: 0.2em 0 0; }
.segmented { display: flex; background: var(--panel-2); border-radius: 9px; padding: 3px; margin-bottom: 1.2em; }
.segmented button {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 0.5em;
  border-radius: 7px;
  font: inherit;
  cursor: pointer;
}
.segmented button[aria-selected='true'] { background: var(--panel); color: var(--text); box-shadow: 0 1px 3px rgb(0 0 0 / 0.2); }
.auth-note { margin: 1.2em 0 0; }

/* App shell */
.topbar {
  display: flex;
  align-items: center;
  gap: 1.5em;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topnav { display: flex; gap: 0.25em; }
.navbtn {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  padding: 0.45em 0.9em;
  border-radius: 7px;
  cursor: pointer;
}
.navbtn[aria-current='page'] { background: var(--panel-2); color: var(--text); }
.account { margin-left: auto; display: flex; align-items: center; gap: 0.5em; }

.layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); min-height: calc(100vh - 53px); }
.sidebar { border-right: 1px solid var(--border); padding: 16px; background: var(--panel); }
.content { padding: 20px 24px 48px; min-width: 0; }

.dropzone {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 0.3em;
  padding: 22px 14px;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.dragging { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, transparent); }
.dropzone input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.dropzone:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.dz-icon { font-size: 1.8rem; color: var(--accent); line-height: 1; }
.sidebar-actions { display: flex; gap: 0.5em; margin: 12px 0; }

.filelist { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.fileitem {
  width: 100%;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.fileitem:hover { background: var(--panel-2); }
.fileitem.active { background: var(--panel-2); border-color: var(--accent); }
.thumb { width: 48px; height: 48px; object-fit: cover; border-radius: 6px; flex: none; background: var(--panel-2); }
.thumb.placeholder { display: grid; place-items: center; color: var(--muted); }
.fileinfo { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.filename { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 0.9em; }
.filemeta { display: flex; flex-wrap: wrap; gap: 4px; }

.pill {
  display: inline-block;
  font-size: 0.72em;
  padding: 0.15em 0.55em;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--border);
}
.pill.ok { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, transparent); }
.pill.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); }
.pill.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.pill.sim { color: #fff; border: 0; }
.sim-0 { background: #7c5cff; }
.sim-1 { background: #e0567a; }
.sim-2 { background: #1fa38a; }
.sim-3 { background: #d98a1f; }
.sim-4 { background: #3a86d9; }
.sim-5 { background: #8a9a2b; }

/* Content */
.empty { max-width: 640px; margin: 8vh auto; text-align: center; }
.empty .steps { text-align: left; display: inline-block; margin: 1em auto; }
.empty .steps li { margin: 0.35em 0; }
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 1em;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hero { display: flex; gap: 20px; align-items: flex-start; margin-bottom: 18px; }
.hero-img {
  width: 340px;
  max-width: 45%;
  max-height: 260px;
  object-fit: contain;
  border-radius: var(--radius);
  background: var(--panel);
  border: 1px solid var(--border);
}
.hero-img.placeholder { height: 180px; display: grid; place-items: center; color: var(--muted); }
.hero-info { min-width: 0; flex: 1; }
.hero-title { word-break: break-word; }
.hero-info p { margin: 0.3em 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 0.15em 0.6em; font-size: 0.9em; }

.histogram { margin: 10px 0 0; max-width: 360px; }
.histogram svg { width: 100%; height: 80px; display: block; background: var(--panel); border-radius: 6px; border: 1px solid var(--border); }
.histogram path { mix-blend-mode: screen; opacity: 0.75; }
.h-r { fill: #ff4d4d; }
.h-g { fill: #3ddc84; }
.h-b { fill: #4d8dff; }
@media (prefers-color-scheme: light) { .histogram path { mix-blend-mode: multiply; opacity: 0.55; } }
.histogram figcaption { margin-top: 4px; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-weight: 500;
  padding: 0.6em 1em;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  white-space: nowrap;
}
.tab[aria-selected='true'] { color: var(--text); border-bottom-color: var(--accent); }
.dot { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-left: 6px; vertical-align: middle; }
.dot.ok { background: var(--ok); }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 14px; align-items: start; }
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0;
}
.card + .card { margin-top: 0; }
.kv { display: grid; grid-template-columns: minmax(110px, 40%) 1fr; gap: 6px 12px; margin: 0; }
.kv dt { color: var(--muted); font-size: 0.88em; }
.kv dd { margin: 0; word-break: break-word; }
.links { display: flex; gap: 1em; margin: 0.8em 0 0; }

.notice { border-radius: var(--radius); padding: 14px 16px; border: 1px solid var(--border); background: var(--panel); }
.notice.warn { border-color: color-mix(in srgb, var(--warn) 50%, var(--border)); }
.notice h3 { color: var(--warn); }

/* Preset */
.preset-view { display: flex; flex-direction: column; gap: 14px; }
.builder h3 { font-size: 1.1rem; }
.group-toggles { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 6px; margin: 10px 0; }
.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--panel-2);
  cursor: pointer;
  font-size: 0.92em;
}
.toggle.specific span:first-of-type { color: var(--muted); }
.toggle input { accent-color: var(--accent); width: 16px; height: 16px; }
.toggle .count { margin-left: auto; font-size: 0.8em; color: var(--muted); }
.preset-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.howto { margin-top: 12px; }
.howto summary { cursor: pointer; color: var(--accent); }
.howto li { margin: 0.4em 0; }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 14px; align-items: start; }
.group-section.excluded { opacity: 0.45; }
.settings { display: flex; flex-direction: column; gap: 6px; }
.setting {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: 'label value' 'bar bar';
  gap: 2px 10px;
  font-size: 0.9em;
}
.setting-label { grid-area: label; display: flex; align-items: center; gap: 6px; min-width: 0; }
.setting-value { grid-area: value; font-family: var(--mono); font-size: 0.9em; text-align: right; word-break: break-all; }
.bar { grid-area: bar; position: relative; height: 4px; background: var(--panel-2); border-radius: 2px; overflow: hidden; }
.bar-fill { position: absolute; top: 0; bottom: 0; background: var(--accent); border-radius: 2px; }
.bar-zero { position: absolute; top: -2px; bottom: -2px; width: 1px; background: var(--muted); }
.swatch { width: 11px; height: 11px; border-radius: 50%; flex: none; border: 1px solid rgb(0 0 0 / 0.2); }

.curve { margin: 0 0 12px; }
.curve svg { width: 100%; max-width: 240px; display: block; }
.curve .frame { fill: var(--panel-2); stroke: var(--border); }
.curve .grid { stroke: var(--border); stroke-width: 0.6; }
.curve .diag { stroke: var(--muted); stroke-dasharray: 3 3; stroke-width: 0.6; }
.curve polyline { stroke-width: 1.8; }
.c-l polyline { stroke: var(--text); }
.c-l circle { fill: var(--text); }
.c-r polyline { stroke: #ff5a5a; }
.c-r circle { fill: #ff5a5a; }
.c-g polyline { stroke: #3ddc84; }
.c-g circle { fill: #3ddc84; }
.c-b polyline { stroke: #4d8dff; }
.c-b circle { fill: #4d8dff; }

.complex { margin-top: 10px; }
.complex summary { cursor: pointer; font-size: 0.92em; }
.complex .meta-table { margin-top: 8px; }

/* Metadata tables */
.toolbar { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar h3 { margin: 0; }
.toolbar input[type='search'] { max-width: 360px; }
.meta-tables { display: flex; flex-direction: column; gap: 14px; }
.meta-table { width: 100%; border-collapse: collapse; font-size: 0.9em; table-layout: fixed; }
.meta-table th, .meta-table td { text-align: left; padding: 5px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.meta-table th { width: 38%; color: var(--muted); font-weight: 500; word-break: break-word; }
.meta-table td { word-break: break-word; white-space: pre-wrap; }
.meta-table.compact { font-size: 0.82em; }
.meta-table tr:last-child th, .meta-table tr:last-child td { border-bottom: 0; }

pre.code {
  margin: 0;
  padding: 12px;
  background: var(--bg);
  border-radius: 8px;
  border: 1px solid var(--border);
  max-height: 60vh;
  overflow: auto;
  white-space: pre;
}

/* Library */
.library { max-width: 1100px; margin: 0 auto; padding: 24px; }
.library-header { margin-bottom: 16px; }
.table-wrap { overflow-x: auto; padding: 0; }
.library-table { width: 100%; border-collapse: collapse; }
.library-table th, .library-table td { padding: 10px 14px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.library-table th { color: var(--muted); font-weight: 500; font-size: 0.85em; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

/* Dialog & toast */
dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--panel);
  color: var(--text);
  padding: 0;
  width: min(420px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
.dialog-body { padding: 22px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 100;
  max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { border-color: var(--danger); }

@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; gap: 0.6em; padding: 10px 16px; }
  .account { margin-left: 0; width: 100%; flex-wrap: wrap; }
  .layout { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; border-bottom: 1px solid var(--border); }
  .filelist { max-height: 240px; overflow-y: auto; }
  .content { padding: 16px; }
  .hero { flex-direction: column; }
  .hero-img { width: 100%; max-width: 100%; }
  .cards, .settings-grid { grid-template-columns: 1fr; }
  .library { padding: 16px; }
}
