:root {
  --bg: #1b1f24;
  --panel: #242a31;
  --panel2: #2c333b;
  --border: #3a434d;
  --text: #e6e9ec;
  --dim: #9aa5b1;
  --accent: #3fb8ff;
  --accent2: #1f6fa0;
  --danger: #e5484d;
  --warn: #f5a524;
  --radius: 6px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }

button, select, input {
  font: inherit;
  color: inherit;
}
button {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button.active { background: var(--accent2); border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: #1d6f3f; border-color: #2fa35f; }
button.danger { background: #5a1f22; border-color: #8f2d31; }
button.small { padding: 2px 8px; font-size: 12px; }
select, input:not([type]), input[type=number] {
  background: #161a1f;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 4px 6px;
}
input.num { width: 72px; }

/* ------------------------------------------------------------------ top */
#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  height: 52px;
  padding: 0 12px;
  background: linear-gradient(#2a3038, #20252b);
  border-bottom: 1px solid var(--border);
}
#topbar .brand { display: flex; align-items: center; gap: 8px; min-width: 210px; }
#topbar .logo { font-size: 26px; }
#topbar .title { font-weight: 700; }
#topbar .subtitle { font-size: 11px; color: var(--dim); letter-spacing: 0.04em; text-transform: uppercase; }
#topbar .actions { display: flex; gap: 6px; align-items: center; flex-wrap: nowrap; overflow-x: auto; }
#topbar .sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
#topbar .right { margin-left: auto; display: flex; gap: 8px; }

.seg { display: inline-flex; }
.seg button { border-radius: 0; margin-left: -1px; }
.seg button:first-child { border-radius: var(--radius) 0 0 var(--radius); margin-left: 0; }
.seg button:last-child { border-radius: 0 var(--radius) var(--radius) 0; }

/* ----------------------------------------------------------------- main */
#main {
  display: grid;
  grid-template-columns: 290px 1fr 310px;
  height: calc(100% - 52px);
}
#left { display: flex; flex-direction: column; border-right: 1px solid var(--border); background: var(--panel); min-height: 0; }
#tabs { display: grid; grid-template-columns: repeat(4, 1fr); border-bottom: 1px solid var(--border); }
#tabs button { border: 0; border-radius: 0; padding: 10px 4px; background: transparent; border-bottom: 3px solid transparent; }
#tabs button.active { border-bottom-color: var(--accent); background: var(--panel2); }
.panel { overflow-y: auto; padding: 10px; min-height: 0; }
#right-panel { border-left: 1px solid var(--border); background: var(--panel); }

.panel h3 { margin: 4px 0 8px; font-size: 15px; display: flex; align-items: center; gap: 6px; }
.panel h3.brand { color: #ffcf5c; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; margin-top: 12px; }
.panel h4 { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--dim); }
.hint { color: var(--dim); font-size: 12px; line-height: 1.4; margin: 6px 0; }
.warn { color: var(--warn); font-size: 12px; }
.dim { color: var(--dim); font-size: 11px; }
.spacer { flex: 1; }
.card { background: var(--panel2); border: 1px solid var(--border); border-radius: 8px; padding: 10px; margin-bottom: 10px; }

.toolrow { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.toolrow button { font-size: 12px; }

.catalog { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.catalog .item {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 4px; text-align: center; min-height: 84px;
}
.catalog .item .icon { width: 64px; height: 40px; }
.catalog .item .ref { font-weight: 700; font-size: 13px; }
.catalog .item .nm { font-size: 11px; color: var(--dim); line-height: 1.2; }
.catalog .item.active .nm { color: var(--text); }
.swatch { width: 44px; height: 28px; border-radius: 3px; border: 4px solid; margin: 4px 0; }
.swatch.round { width: 30px; height: 30px; border-radius: 50%; }

.slider { display: grid; grid-template-columns: 70px 1fr 36px; align-items: center; gap: 6px; margin: 8px 0; }
.slider output { text-align: right; font-variant-numeric: tabular-nums; }
.swatches { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.swatches button { height: 34px; color: #111; font-size: 11px; font-weight: 600; text-shadow: 0 0 3px rgba(255,255,255,0.7); }
.swatches button.active { outline: 3px solid var(--accent); }
.stack { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.stack.row { flex-direction: row; flex-wrap: wrap; }

.consist { display: flex; flex-wrap: wrap; gap: 4px; min-height: 30px; }
.chip { font-size: 12px; padding: 3px 8px; }
.preset { text-align: left; display: flex; flex-direction: column; }
.stocklist { display: flex; flex-direction: column; gap: 5px; }
.stock { display: grid; grid-template-columns: 40px 1fr; grid-template-rows: auto auto auto; column-gap: 8px; text-align: left; padding: 6px; }
.stock .bar { grid-row: 1 / 4; border-radius: 3px; min-height: 36px; }
.stock .ref { font-weight: 700; font-size: 12px; }
.stock .nm { font-size: 12px; }

/* right panel */
#cab .train { border: 1px solid var(--border); border-radius: 6px; padding: 8px; margin-bottom: 8px; background: #20262c; cursor: pointer; }
#cab .train.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
#cab .row { display: flex; align-items: center; gap: 6px; }
#cab .row.buttons { margin: 6px 0 4px; }
#cab .name { flex: 1; min-width: 0; background: transparent; border-color: transparent; font-weight: 600; }
#cab .name:focus { border-color: var(--border); }
#cab .speed { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; min-width: 42px; text-align: right; color: #9ef0a8; }
#cab .unit { font-size: 11px; color: var(--dim); }
#cab .throttle { width: 100%; accent-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; cursor: pointer; }

.prop-title { display: flex; flex-direction: column; margin-bottom: 8px; }
.prop-title span { color: var(--dim); font-size: 12px; }
.field { display: flex; justify-content: space-between; align-items: center; gap: 6px; margin: 5px 0; font-size: 13px; }
.field > span:first-child { color: var(--dim); }
.field .seg button { font-size: 12px; padding: 3px 8px; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
table td, table th { padding: 3px 4px; border-bottom: 1px solid var(--border); text-align: left; }
table td.n { text-align: right; font-weight: 700; }
table.stats td:last-child { text-align: right; }

/* ----------------------------------------------------------------- views */
#views { position: relative; display: grid; min-width: 0; min-height: 0; }
#views.split { grid-template-columns: 1fr 1fr; }
#views.v2d { grid-template-columns: 1fr; }
#views.v2d #three-wrap { display: none; }
#views.v3d { grid-template-columns: 1fr; }
#views.v3d #plan-wrap { display: none; }
.view { position: relative; overflow: hidden; min-width: 0; min-height: 0; }
#plan-wrap { border-right: 1px solid var(--border); }
#plan { display: block; touch-action: none; }
#three-wrap canvas { display: block; }
.overlay { position: absolute; z-index: 2; display: flex; gap: 8px; align-items: center; }
.overlay.tl { top: 8px; left: 8px; background: rgba(20, 24, 30, 0.75); padding: 4px 8px; border-radius: 6px; }
.overlay.bl { bottom: 6px; left: 8px; font-size: 12px; color: var(--dim); background: rgba(20, 24, 30, 0.6); padding: 2px 8px; border-radius: 4px; }
#cammode.overlay { padding: 0; background: none; }

#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 50; pointer-events: none; }
.toast { background: rgba(20, 24, 30, 0.92); border: 1px solid var(--border); padding: 8px 14px; border-radius: 6px; animation: fade 4s forwards; }
@keyframes fade { 0%, 80% { opacity: 1; } 100% { opacity: 0; } }

/* help dialog */
dialog#help {
  width: min(900px, 94vw); height: min(86vh, 1000px); padding: 0;
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
}
dialog#help::backdrop { background: rgba(0, 0, 0, 0.55); }
.help-head { display: flex; justify-content: space-between; padding: 10px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--panel); }
.markdown { padding: 6px 24px 30px; line-height: 1.55; overflow-y: auto; height: calc(100% - 56px); }
.markdown h1 { font-size: 24px; }
.markdown h2 { font-size: 19px; margin-top: 28px; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.markdown code { background: #161a1f; padding: 1px 5px; border-radius: 4px; }
.markdown kbd { background: #161a1f; border: 1px solid var(--border); border-bottom-width: 2px; padding: 0 5px; border-radius: 4px; font-size: 12px; }
.markdown a { color: var(--accent); }
.markdown table td, .markdown table th { font-size: 13px; }

/* ----------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  #main { grid-template-columns: 250px 1fr 260px; }
  #topbar .brand { min-width: 0; }
  #topbar .subtitle { display: none; }
}
@media (max-width: 800px) {
  html, body { overflow: auto; }
  #topbar { height: auto; flex-wrap: wrap; padding: 6px 10px; gap: 8px; }
  #main { display: flex; flex-direction: column; height: auto; }
  #views { height: 70vh; order: -1; }
  #views.split { grid-template-columns: 1fr; grid-template-rows: 1fr 1fr; }
  #plan-wrap { border-right: 0; border-bottom: 1px solid var(--border); }
  #left, #right-panel { border: 0; }
  .panel { overflow: visible; }
}

/* layouts dialog */
dialog#layouts-dlg {
  width: min(760px, 94vw); padding: 0;
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 10px;
}
dialog#layouts-dlg::backdrop { background: rgba(0, 0, 0, 0.55); }
.layouts-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 10px; padding: 14px; }
.layout-card { display: flex; flex-direction: column; gap: 6px; text-align: left; padding: 12px; min-height: 130px; }
.layout-card strong { font-size: 15px; }
.layout-card .desc { color: var(--dim); font-size: 12px; line-height: 1.4; }
.layout-card .lvl { color: #ffcf5c; font-size: 13px; }
.layout-card .lvl em { color: var(--dim); font-style: normal; font-size: 11px; margin-left: 4px; }
.layout-card .lvl em.size { float: right; color: var(--text); }
