: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; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.project-file { display: inline-flex; align-items: center; }

.projectbar {
  display: flex; align-items: center; gap: 9px; padding: 8px 18px;
  border-bottom: 1px solid var(--line); background: #0d1326; font-size: 13px; color: var(--muted);
}
.projectbar input {
  width: min(320px, 55vw); padding: 5px 7px; background: transparent; color: var(--ink);
  border: 1px solid transparent; border-radius: 6px; font: inherit; font-weight: 600;
}
.projectbar input:hover { border-color: var(--line); }
.projectbar input:focus { border-color: var(--accent); outline: none; background: var(--panel); }
.projectbar span { font-size: 12px; }

.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; }

/* Collapsible tool panels (single-screen layout) */
.panel { margin: 12px 18px 0; border: 1px solid var(--line); border-radius: 12px; background: var(--panel); overflow: hidden; }
.panel > summary { list-style: none; cursor: pointer; user-select: none; padding: 11px 16px; font-size: 14px; font-weight: 600; color: var(--ink); display: flex; align-items: center; gap: 9px; }
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary::before { content: '\25B8'; font-size: 11px; color: var(--muted); transition: transform .15s; }
.panel[open] > summary::before { transform: rotate(90deg); }
.panel > summary:hover { background: rgba(255,255,255,0.02); }
.panel > .toolbar { border-top: 1px solid var(--line); border-bottom: none; }
.panel > .results { margin: 0; border: 0; border-top: 1px solid var(--line); border-radius: 0; }

/* 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); }

/* Custom file picker (hides the browser-native, locale-dependent input button) */
.filepick { display: inline-flex; align-items: center; gap: 6px; }
.filepick .filebtn { cursor: pointer; }
.filepick .filename { font-size: 12px; color: var(--muted); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filepick .filename.set { color: var(--ink); }

/* 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-top: 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; }
.riet-warn { margin: 0 14px 10px; padding: 9px 12px; border-radius: 8px; font-size: 13px; line-height: 1.5;
  color: #f0b429; background: rgba(240, 180, 41, .08); border: 1px solid rgba(240, 180, 41, .4); }
.riet-warn.degenerate { color: #e0563b; background: rgba(224, 86, 59, .1); border-color: rgba(224, 86, 59, .5); }
.riet-warn[hidden] { display: none; }

/* Footer: copyright / license / GSAS-II attribution / support */
.appfoot { margin: 22px 18px 26px; padding: 14px 16px 0; border-top: 1px solid var(--line);
  color: var(--muted); font-size: 10.5px; line-height: 1.5; text-align: center; }
.appfoot .foot-line { margin: 3px 0; }
.appfoot a { color: var(--accent); text-decoration: none; }
.appfoot a:hover { text-decoration: underline; }

/* 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;
}

@media (max-width: 760px) {
  .topbar { align-items: flex-start; gap: 10px; }
  .brand small { display: none; }
  .actions { gap: 6px; }
  .actions .btn, .actions .project-file { padding: 7px 9px; font-size: 13px; }
  .projectbar { padding-left: 12px; padding-right: 12px; }
}
