:root {
  --bg: #0b1020;
  --panel: #11172c;
  --line: #26304f;
  --ink: #eef1f8;
  --muted: #9aa5c4;
  --accent: #6c8cff;
}
* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  display: flex;
  flex-direction: column;
}

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; }
.brand small { color: var(--muted); font-weight: 500; font-size: 12px; margin-left: 4px; }
.actions { display: flex; gap: 10px; }

.btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  padding: 8px 14px; border-radius: 10px; font-size: 14px; cursor: pointer;
  transition: border-color .15s, background .15s, opacity .15s;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .45; cursor: not-allowed; border-color: var(--line); }
.btn.primary { background: linear-gradient(90deg, #6c8cff, #8aa2ff); color: #0b1020; font-weight: 700; border: 0; }
.btn.ghost { background: transparent; }

/* Toolbar */
.toolbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 18px; border-bottom: 1px solid var(--line);
  font-size: 14px; color: var(--muted);
}
.toolbar label { display: flex; align-items: center; gap: 6px; }
.toolbar select, .toolbar input[type=number], .toolbar input[type=text] {
  background: var(--panel); border: 1px solid var(--line); color: var(--ink);
  border-radius: 8px; padding: 6px 8px;
}
.toolbar input[type=number] { width: 72px; }
.toolbar input[type=text] { width: 150px; }
.toolbar input[type=text]:focus { border-color: var(--accent); outline: none; }
.toolbar .chk { cursor: pointer; }
.toolbar2, .toolbar3 { padding-top: 8px; padding-bottom: 8px; }
.hint { color: var(--muted); }
.btn.active { background: linear-gradient(90deg, #6c8cff, #8aa2ff); color: #0b1020; font-weight: 700; border: 0; }
.spacer { flex: 1; }
.sep { width: 1px; height: 20px; align-self: center; background: var(--line); margin: 0 3px; }

/* Collapsible sections (progressive disclosure) */
details.collapse { border-bottom: 1px solid var(--line); }
details.collapse > summary { list-style: none; cursor: pointer; user-select: none; padding: 9px 18px; font-size: 13px; font-weight: 600; color: var(--muted); display: flex; align-items: center; gap: 7px; }
details.collapse > summary::-webkit-details-marker { display: none; }
details.collapse > summary::before { content: '\25B8'; font-size: 10px; transition: transform .15s; }
details.collapse[open] > summary::before { transform: rotate(90deg); }
details.collapse > summary:hover { color: var(--ink); }
details.collapse .toolbar { border-bottom: none; padding: 2px 18px 12px; }
details.collapse .riet-opts { border-bottom: none; padding: 2px 18px 12px; }

/* Tabs */
.tabs { display: flex; gap: 6px; padding: 10px 18px 0; border-bottom: 1px solid var(--line); }
.tab { display: flex; align-items: center; gap: 8px; background: transparent; border: 1px solid transparent; border-bottom: none; color: var(--muted); padding: 9px 18px; border-radius: 10px 10px 0 0; cursor: pointer; font-size: 14px; font-weight: 600; transition: color .15s, background .15s; }
.tab:hover { color: var(--ink); }
.tab.active { color: var(--ink); background: var(--panel); border-color: var(--line); margin-bottom: -1px; }
.tab .step { display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; border-radius: 50%; background: var(--line); color: var(--muted); font-size: 12px; font-weight: 700; transition: background .15s, color .15s; }
.tab.active .step { background: var(--accent); color: #0b1020; }
body[data-tab="view"] .only-analyze, body[data-tab="view"] .only-refine,
body[data-tab="analyze"] .only-view, body[data-tab="analyze"] .only-refine,
body[data-tab="refine"] .only-view, body[data-tab="refine"] .only-analyze { display: none !important; }

/* Dataset chips */
.chips { display: flex; gap: 8px; flex-wrap: wrap; padding: 10px 18px 0; }
.chip {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 999px; padding: 5px 6px 5px 10px; font-size: 13px;
}
.chip .swatch { width: 18px; height: 18px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: none; cursor: pointer; }
.chip .swatch::-webkit-color-swatch-wrapper { padding: 0; }
.chip .swatch::-webkit-color-swatch { border: none; border-radius: 3px; }
.chip .rename { background: transparent; border: 1px solid transparent; color: var(--ink); font-size: 13px; padding: 2px 4px; border-radius: 6px; }
.chip .rename:hover { border-color: var(--line); }
.chip .rename:focus { border-color: var(--accent); outline: none; background: #0d1430; }
.chip button { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 16px; line-height: 1; }
.chip button:hover { color: #fff; }

/* Plot stage */
.stage {
  position: relative; flex: 1; margin: 14px 18px 18px;
  border: 1px solid var(--line); border-radius: 14px; overflow: hidden; background: #fff;
  min-height: 440px;
}
.stage.drag { outline: 2px dashed var(--accent); outline-offset: -6px; }
.stage.labelmode { cursor: crosshair; }
#plot { width: 100%; height: 100%; min-height: 440px; }

.empty {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  background: #fff; z-index: 2; pointer-events: none;
}
.empty-inner { text-align: center; }
.empty .big { font-size: 22px; font-weight: 700; margin-bottom: 6px; color: #2a3350; }
.empty .muted { color: #7783a0; }
.empty .link {
  pointer-events: auto; border: 0; background: transparent; color: var(--accent);
  cursor: pointer; font-size: inherit; text-decoration: underline; padding: 0;
}

/* API status chip */
.apistatus { font-size: 12px; color: var(--muted); border: 1px solid var(--line); border-radius: 999px; padding: 5px 10px; align-self: center; }
.apistatus.on { color: #34e0c8; border-color: rgba(52, 224, 200, .4); }
.apistatus.off { color: #e0563b; border-color: rgba(224, 86, 59, .45); }

/* Peak-fit results table */
.results { margin: 0 18px 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
.results[hidden] { display: none; }
.results-head { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); font-weight: 600; }
.results-wrap { max-height: 260px; overflow: auto; }
#fittable { width: 100%; border-collapse: collapse; font-size: 13px; }
#fittable th, #fittable td { padding: 7px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
#fittable th { position: sticky; top: 0; background: #0f1530; color: var(--muted); z-index: 1; }
#fittable th.lft, #fittable td.lft { text-align: left; }
#fittable tbody tr:hover { background: #0d1430; }
#fittable .rm { border: 0; background: transparent; color: var(--muted); cursor: pointer; font-size: 15px; }
#fittable .rm:hover { color: #fff; }
#fittable input.hkl { width: 66px; background: var(--bg); border: 1px solid var(--line); color: var(--ink); border-radius: 6px; padding: 3px 6px; text-align: center; }
#fittable input.hkl:focus { border-color: var(--accent); outline: none; }
.results-head select { background: var(--bg); border: 1px solid var(--line); color: var(--ink); border-radius: 8px; padding: 5px 8px; margin-left: 6px; }
.cellresult { padding: 12px 14px; border-top: 1px solid var(--line); }
.cellresult[hidden] { display: none; }
.cellresult .big { font-size: 15px; font-weight: 700; color: #34e0c8; }
.cellresult .muted { color: var(--muted); font-size: 13px; margin-top: 3px; }
.cellresult table.obscalc { margin-top: 10px; border-collapse: collapse; font-size: 13px; }
.obscalc th, .obscalc td { padding: 4px 14px 4px 0; text-align: right; }
.obscalc tr.hd th { color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--line); }
.obscalc th:first-child, .obscalc td:first-child { text-align: left; padding-right: 22px; }
.obscalc tr.bad td { color: #e0563b; font-weight: 600; }

/* Rietveld panel */
.rietveld { margin: 0 18px 18px; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); }
.riet-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.riet-head label { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.riet-head input[type=file] { font-size: 12px; color: var(--muted); max-width: 190px; }
.pill { font-size: 12px; border: 1px solid var(--line); border-radius: 999px; padding: 3px 9px; color: var(--muted); }
.pill.on { color: #34e0c8; border-color: rgba(52, 224, 200, .4); }
.pill.off { color: #e0563b; border-color: rgba(224, 86, 59, .4); }
.riet-opts { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; padding: 8px 14px; border-bottom: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.riet-opts .chk { display: flex; align-items: center; gap: 5px; cursor: pointer; }
.riet-opts select { background: var(--bg); border: 1px solid var(--line); color: var(--ink); border-radius: 6px; padding: 2px 6px; margin-left: 4px; }
.rietinfo { padding: 10px 14px; font-size: 14px; color: var(--muted); }
.rietconsole { margin: 0 14px 10px; padding: 10px 12px; background: #0b1020; border: 1px solid var(--line); border-radius: 8px; font-family: ui-monospace, "Cascadia Code", Consolas, monospace; font-size: 12px; color: #9aa5c4; max-height: 190px; overflow: auto; white-space: pre; line-height: 1.55; }
.rietconsole[hidden] { display: none; }
#rietplot { width: 100%; height: 360px; background: #fff; border-radius: 0 0 12px 12px; }
#rietplot[hidden] { display: none; }

/* Toast */
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: #1b2340; border: 1px solid var(--line); color: var(--ink);
  padding: 10px 16px; border-radius: 10px; opacity: 0; transition: opacity .3s;
  pointer-events: none; font-size: 14px;
}
