:root {
  --bg: #15171a;
  --panel: rgba(24, 26, 29, 0.78);
  --panel-solid: #1c1f22;
  --line: rgba(255, 255, 255, 0.08);
  --line-2: rgba(255, 255, 255, 0.14);
  --text: #ece7de;
  --muted: #a39d93;
  --faint: #6f6a63;
  --accent: #f0b35a;
  --accent-ink: #2a1c06;
  --radius: 14px;
  --gutter: 16px;
  --col: 304px;
  --bar: 44px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body { font: 14px/1.4 Inter, system-ui, sans-serif; -webkit-font-smoothing: antialiased; }
canvas#c { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; outline: none; }
button { font: inherit; color: inherit; }
svg { stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; fill: none; }

.panel {
  background: var(--panel);
  backdrop-filter: blur(14px) saturate(1.2);
  -webkit-backdrop-filter: blur(14px) saturate(1.2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.28);
}

/* columnes: a l'esquerra la maqueta, a la dreta els trens; la barra de vistes queda a sota, sense tocar-les */
.dock { position: fixed; top: var(--gutter); bottom: calc(var(--gutter) + var(--bar) + 10px); width: var(--col); display: flex; flex-direction: column; gap: 10px; pointer-events: none; z-index: 2; }
.dock > * { pointer-events: auto; }
.dock-l { left: var(--gutter); }
.dock-r { right: var(--gutter); }

/* capçalera */
.brand { display: flex; align-items: center; gap: 12px; padding: 10px 10px 10px 12px; flex: none; width: max-content; max-width: calc(100vw - var(--col) - 3 * var(--gutter)); }
.logo { width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; flex: none; }
.logo svg { width: 22px; height: 22px; stroke-width: 2.4; }
.title { min-width: 0; flex: 1; }
.title h1 { margin: 0; font-size: 16px; font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title p { margin: 1px 0 0; color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.btn { display: inline-flex; align-items: center; gap: 7px; height: 34px; padding: 0 12px; border-radius: 9px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.04); cursor: pointer; font-weight: 550; font-size: 13px; white-space: nowrap; flex: none; transition: background 0.15s, border-color 0.15s; }
.btn + .btn { margin-left: -6px; }
.btn.icon-only { padding: 0 9px; color: inherit; text-decoration: none; }
.btn:hover { background: rgba(255, 255, 255, 0.09); border-color: rgba(255, 255, 255, 0.22); }
.btn svg { width: 17px; height: 17px; }
.btn:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

/* panells laterals: capçalera fixa i cos amb scroll propi */
.side { display: flex; flex-direction: column; min-height: 0; flex: 0 1 auto; overflow: hidden; }
.side .head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 10px 10px 14px; min-height: 52px; flex: none; }
.side .body { padding: 2px 14px 14px; overflow-y: auto; overflow-x: hidden; min-height: 0; scrollbar-width: thin; }
.head h2 { margin: 0; font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); cursor: pointer; user-select: none; display: flex; align-items: center; gap: 8px; min-width: 0; }
.head h2 .count { font: 500 11px/1 "JetBrains Mono", ui-monospace, monospace; letter-spacing: 0; color: var(--text); background: rgba(255, 255, 255, 0.08); border-radius: 6px; padding: 3px 6px; }
.head h2 .count:empty { display: none; }
.head-actions { display: flex; gap: 6px; align-items: center; flex: none; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; font-size: 12.5px; font-weight: 550; padding: 4px 6px; border-radius: 6px; }
.link:hover { background: rgba(240, 179, 90, 0.1); }
.icon { width: 32px; height: 32px; display: grid; place-items: center; border-radius: 9px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.04); cursor: pointer; padding: 0; font-size: 18px; line-height: 1; flex: none; }
.icon:hover { background: rgba(255, 255, 255, 0.1); }
.icon svg { width: 16px; height: 16px; }
#play .i-play { display: none; }
#play.paused .i-play { display: block; fill: currentColor; }
#play.paused .i-pause { display: none; }
.close-mobile { display: none; }
.fold svg { transition: transform 0.18s; }
.side.folded { flex: none; }
.side.folded > .body { display: none !important; }
.side.folded .fold svg { transform: rotate(180deg); }
.group { margin: 6px 0 4px; font-size: 11.5px; font-weight: 600; color: var(--faint); letter-spacing: 0.04em; }
.group + .toggles { border-top: 0; padding-top: 0; }

.layer-list, .train-list { list-style: none; margin: 0 0 10px; padding: 0; display: grid; gap: 2px; }
.layer { display: grid; grid-template-columns: 22px 1fr auto; align-items: center; gap: 8px; padding: 6px 6px 6px 4px; border-radius: 9px; cursor: pointer; user-select: none; transition: background 0.12s; }
.layer:hover { background: rgba(255, 255, 255, 0.05); }
.layer .eye { width: 22px; height: 22px; border-radius: 6px; display: grid; place-items: center; border: 1.5px solid var(--sw); background: var(--sw); transition: background 0.12s, opacity 0.12s; }
.layer .eye svg { width: 13px; height: 13px; stroke: #111; stroke-width: 3; }
.layer.off .eye { background: transparent; }
.layer.off .eye svg { opacity: 0; }
.layer.off .name { color: var(--faint); }
.layer .name { font-weight: 520; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer .z { color: var(--muted); font: 500 11.5px/1 "JetBrains Mono", ui-monospace, monospace; white-space: nowrap; }
.layer .solo { opacity: 0; font-size: 11.5px; color: var(--accent); background: none; border: 0; cursor: pointer; padding: 2px 4px; }
.layer:hover .solo { opacity: 1; }

.field { margin: 10px 0 12px; }
.field-row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 6px; }
.field label { font-size: 13px; color: var(--text); }
.field output { font: 500 12px "JetBrains Mono", ui-monospace, monospace; color: var(--accent); }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; cursor: pointer; }
input[type=range]::-webkit-slider-runnable-track { height: 4px; border-radius: 4px; background: linear-gradient(90deg, var(--accent) var(--p, 0%), rgba(255, 255, 255, 0.14) var(--p, 0%)); }
input[type=range]::-moz-range-track { height: 4px; border-radius: 4px; background: rgba(255, 255, 255, 0.14); }
input[type=range]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--accent); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: #fff; margin-top: -6px; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5); }
input[type=range]::-moz-range-thumb { width: 16px; height: 16px; border: 0; border-radius: 50%; background: #fff; }
input[type=range]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.toggles { display: grid; gap: 2px; margin-bottom: 8px; }
.toggles.first { margin-bottom: 2px; }
.tg { display: flex; align-items: center; gap: 10px; padding: 5px 2px; cursor: pointer; font-size: 13px; user-select: none; min-width: 0; }
.tg.sub { padding-left: 22px; }
.tg.dim { opacity: 0.45; cursor: default; }
.tg input { position: absolute; opacity: 0; pointer-events: none; }
.tg span { width: 30px; height: 18px; border-radius: 10px; background: rgba(255, 255, 255, 0.14); position: relative; flex: none; transition: background 0.15s; }
.tg span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform 0.18s cubic-bezier(.3, .7, .4, 1); }
.tg input:checked + span { background: var(--accent); }
.tg input:indeterminate + span { background: rgba(240, 179, 90, 0.5); }
.tg input:indeterminate + span::after { transform: translateX(6px); }
.tg input:checked + span::after { transform: translateX(12px); }
.tg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.train { display: grid; grid-template-columns: 4px minmax(0, 1fr) auto; grid-template-areas: 'sw nm tg' 'sw ds act'; column-gap: 10px; row-gap: 6px; align-items: center; padding: 8px 4px 8px 6px; border-radius: 10px; transition: opacity 0.15s; }
.train:hover { background: rgba(255, 255, 255, 0.04); }
.train .sw { grid-area: sw; width: 4px; align-self: stretch; border-radius: 2px; }
.train .nm { grid-area: nm; min-width: 0; font-weight: 600; font-size: 13.5px; line-height: 1.3; overflow-wrap: anywhere; }
.train .solo-tg { grid-area: tg; justify-self: end; padding: 0; }
.train .ds { grid-area: ds; min-width: 0; color: var(--muted); font-size: 12px; line-height: 1.3; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.train .act { grid-area: act; display: flex; gap: 3px; }
.train .act button { width: 28px; height: 28px; padding: 0; display: grid; place-items: center; border-radius: 8px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.04); cursor: pointer; transition: background 0.15s, color 0.15s; }
.train .act button svg { width: 15px; height: 15px; }
.train .act button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.1); }
.train .act button:disabled { opacity: 0.3; cursor: default; }
.train .act button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.train .act button.honk { animation: honk 0.5s ease-out; }
@keyframes honk { 0% { background: var(--accent); color: var(--accent-ink); } 100% { background: rgba(255, 255, 255, 0.04); } }
.train.off .nm, .train.off .ds, .train.off .sw { opacity: 0.45; }
.seg3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
.seg3 button { height: 32px; padding: 0 4px; border-radius: 8px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.04); cursor: pointer; font-size: 12px; font-weight: 550; white-space: nowrap; transition: background 0.15s, border-color 0.15s; }
.seg3 button:hover { background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.22); }
.seg3 button:active { transform: translateY(1px); }
.note { margin: 0; font-size: 12.5px; color: #ffcf8a; text-wrap: balance; }
.hint { margin: 0; font-size: 12px; color: var(--faint); text-wrap: balance; }
#mute .i-off { display: none; }
#mute.muted .i-off { display: block; }
#mute.muted .i-on { display: none; }
.empty { color: var(--muted); font-size: 13px; margin: 2px 0 0; text-wrap: balance; }

/* barra de vistes */
.views { position: fixed; z-index: 3; bottom: var(--gutter); left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 4px; padding: 5px; max-width: calc(100vw - 2 * var(--gutter)); }
.vrow { display: flex; gap: 4px; }
.views button { height: 34px; padding: 0 14px; border-radius: 9px; border: 0; background: transparent; cursor: pointer; font-weight: 550; font-size: 13px; color: var(--muted); white-space: nowrap; transition: background 0.15s, color 0.15s; }
.views button:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }
.views button.on { background: rgba(255, 255, 255, 0.12); color: var(--text); }
.mobile-only { display: none; }

/* dades */
.stats { flex: none; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; margin: 0; }
.stat-grid div { min-width: 0; }
.stat-grid dt { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-grid dd { margin: 1px 0 0; color: var(--text); font: 500 14px "JetBrains Mono", ui-monospace, monospace; white-space: nowrap; }

.tip { position: fixed; z-index: 20; pointer-events: none; background: var(--panel-solid); border: 1px solid var(--line-2); border-radius: 10px; padding: 9px 11px; font-size: 12.5px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); max-width: 280px; }
.tip b { text-wrap: balance; display: block; font-size: 13px; font-weight: 600; margin-bottom: 3px; }
.tip .row { display: flex; justify-content: space-between; align-items: baseline; gap: 14px; color: var(--muted); }
.tip .row span { min-width: 0; }
.tip .row span:last-child { text-align: right; flex: none; max-width: 60%; }
.tip .row span:last-child { color: var(--text); font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 12px; }
.tip .sw { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; vertical-align: 1px; }

.cabbar { position: fixed; max-width: calc(100vw - 2 * var(--gutter)); top: var(--gutter); left: 50%; transform: translateX(-50%); display: flex; gap: 12px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 6px 6px 6px 14px; backdrop-filter: blur(12px); font-weight: 560; z-index: 5; }
.cabbar span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40vw; }
.cabbar button { white-space: nowrap; height: 30px; border-radius: 8px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.06); padding: 0 10px; cursor: pointer; font-size: 12.5px; }
kbd { font: 500 11px "JetBrains Mono", monospace; padding: 1px 5px; border-radius: 4px; border: 1px solid var(--line-2); color: var(--muted); margin-left: 4px; }
body.cab .dock { opacity: 0; pointer-events: none; }
body.cab .dock > * { pointer-events: none; }
.dock { transition: opacity 0.25s; }

/* mode editor */
body.edit .trains, body.edit .stats, body.edit .views .photo-btn { display: none; }
body.edit .tip { display: none !important; }
body.edit #editBtn { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.ed-busy { font-size: 12px; color: var(--accent); margin-right: 4px; }
.icon:disabled { opacity: 0.35; cursor: default; }
.ed-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 8px 0; }
.ed-row label { font-size: 13px; color: var(--muted); flex: none; }
.ed-row input, .ed-row select, .ed-card input[type=search], .ed-end input { font: inherit; font-size: 13px; color: var(--text); background: rgba(255, 255, 255, 0.05); border: 1px solid var(--line-2); border-radius: 8px; height: 32px; padding: 0 9px; min-width: 0; }
.ed-row input, .ed-row select { flex: 1; }
.ed-row select option { background: var(--panel-solid); }
.ed-row input:focus, .ed-row select:focus, .ed-card input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.ed-tools { margin: 4px 0 12px; }
.ed-tools button.on, .seg4 button.on, .seg2 button.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.seg4, .seg2 { display: grid; gap: 4px; margin-bottom: 4px; }
.seg4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.seg2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.seg4 button, .seg2 button { height: 32px; padding: 0 4px; border-radius: 8px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.04); cursor: pointer; font-size: 12px; font-weight: 550; white-space: nowrap; }
.seg4 button:hover, .seg2 button:hover { background: rgba(255, 255, 255, 0.1); }
.seg4 button.on:hover, .seg2 button.on:hover, .ed-tools button.on:hover { background: var(--accent); }
.ed-card { border: 1px solid var(--line); border-radius: 11px; padding: 10px; margin-bottom: 10px; background: rgba(255, 255, 255, 0.025); }
.ed-title { display: grid; gap: 2px; margin-bottom: 6px; }
.ed-title b { font-size: 13.5px; font-weight: 600; text-wrap: balance; }
.ed-title span { font-size: 12px; color: var(--muted); }
.ed-ends { display: grid; gap: 4px; margin: 8px 0; }
.ed-end { display: grid; grid-template-columns: 1fr 72px auto 46px; align-items: center; gap: 6px; font-size: 12.5px; }
.ed-end i { color: var(--accent); font-style: normal; font-size: 11.5px; }
.ed-end input { height: 28px; padding: 0 6px; font-family: "JetBrains Mono", ui-monospace, monospace; font-size: 12px; }
.ed-end em { font-style: normal; color: var(--muted); font-size: 12px; }
.ed-mini { height: 26px; border-radius: 7px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.04); cursor: pointer; font-size: 11.5px; color: var(--muted); padding: 0 6px; }
.ed-mini.on { color: var(--accent); border-color: rgba(240, 179, 90, 0.5); }
.ed-tg { padding-top: 6px; }
.ed-actions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 10px; }
.ed-actions button, .ed-save button { height: 32px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.05); cursor: pointer; font-size: 12.5px; font-weight: 550; white-space: nowrap; }
.ed-actions button:hover, .ed-save button:hover { background: rgba(255, 255, 255, 0.11); }
.ed-actions .danger { margin-left: auto; color: #ffb4a8; }
.ed-card input[type=search] { width: 100%; margin: 4px 0 6px; }
.ed-cat { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; max-height: 260px; overflow: auto; scrollbar-width: thin; }
.ed-cat button { width: 100%; text-align: left; display: grid; gap: 1px; padding: 6px 8px; border-radius: 8px; border: 0; background: transparent; cursor: pointer; }
.ed-cat button:hover { background: rgba(255, 255, 255, 0.07); }
.ed-cat b { font-size: 13px; font-weight: 550; }
.ed-cat span { font-size: 11.5px; color: var(--muted); }
.ed-help { margin: 4px 0 10px; line-height: 1.5; color: var(--muted); }
.ed-help kbd { margin: 0 2px 0 0; }
.ed-foot { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--line); padding-top: 10px; margin-top: 6px; }
.ed-state { font-size: 12px; color: var(--muted); }
.ed-state.dirty { color: var(--accent); }
.ed-save { display: grid; grid-template-columns: 1fr auto; gap: 6px; margin-top: 8px; }
.ed-save .primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.ed-save .primary:hover { background: #f5c271; }
.ed-save .primary kbd { color: var(--accent-ink); border-color: rgba(42, 28, 6, 0.3); }

/* mode fotografia */
.views .photo-btn { display: grid; place-items: center; padding: 0 10px; }
.photo-btn svg { width: 18px; height: 18px; }
body.photo .panel, body.photo .cabbar, body.photo .tip { opacity: 0 !important; pointer-events: none !important; }
.panel, .cabbar { transition: opacity 0.25s; }
.photobar { position: fixed; bottom: var(--gutter); left: 50%; transform: translateX(-50%); z-index: 8; display: flex; gap: 6px; padding: 5px; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; backdrop-filter: blur(12px); transition: opacity 0.4s; }
.photobar.idle { opacity: 0; pointer-events: none; }
.photobar button { white-space: nowrap; height: 32px; border-radius: 8px; border: 1px solid var(--line-2); background: rgba(255, 255, 255, 0.06); padding: 0 12px; cursor: pointer; font-size: 12.5px; font-weight: 550; }
.photobar button:hover { background: rgba(255, 255, 255, 0.12); }
.flash { position: fixed; inset: 0; z-index: 50; background: #fff; pointer-events: none; animation: flash 0.35s ease-out forwards; }
@keyframes flash { from { opacity: 0.7; } to { opacity: 0; } }

.drop { position: fixed; inset: 0; z-index: 40; background: rgba(12, 13, 15, 0.7); display: grid; place-items: center; backdrop-filter: blur(4px); }
.drop div { border: 2px dashed var(--accent); border-radius: 18px; padding: 40px 56px; font-size: 18px; }

.loading { position: fixed; inset: 0; z-index: 30; display: grid; place-content: center; justify-items: center; gap: 14px; background: radial-gradient(ellipse at center, #22252a, #121315); transition: opacity 0.4s; }
.loading.hide { opacity: 0; pointer-events: none; }
.loading p { text-wrap: balance; margin: 0; color: var(--muted); max-width: 420px; text-align: center; padding: 0 var(--gutter); }
.loading.error p { color: #ffb4a8; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.12); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
.loading.error .spinner { display: none; }
@keyframes spin { to { transform: rotate(1turn); } }

/* pantalla mitjana: columnes més estretes */
@media (max-width: 1100px) and (min-width: 861px) {
  :root { --col: 288px; }
}

/* mòbil: capçalera a dalt, panells com a fulls a sobre de la barra */
@media (max-width: 860px) {
  :root { --gutter: 10px; --sheet-bottom: calc(var(--gutter) + 96px + env(safe-area-inset-bottom)); }
  .dock { display: contents; }
  .brand { position: fixed; z-index: 3; top: calc(var(--gutter) + env(safe-area-inset-top)); left: var(--gutter); right: var(--gutter); width: auto; max-width: none; }
  .btn { padding: 0 9px; }
  .btn span { display: none; }
  .side { display: none; position: fixed; z-index: 6; left: var(--gutter); right: var(--gutter); bottom: var(--sheet-bottom); max-height: min(58vh, calc(100vh - var(--sheet-bottom) - 90px)); }
  .side.open { display: flex; }
  .side.folded > .body { display: block !important; }
  .side .fold { display: none; }
  .close-mobile { display: grid; }
  .stats { display: none !important; }
  .views { left: var(--gutter); right: var(--gutter); transform: none; bottom: calc(var(--gutter) + env(safe-area-inset-bottom)); }
  .vrow { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); }
  .vrow.mobile-only { display: grid; }
  .views .vrow:first-child { grid-template-columns: repeat(3, minmax(0, 1fr)) 44px; grid-auto-flow: row; }
  .views button { padding: 0 6px; font-size: 12.5px; }
  .photobar kbd { display: none; }
  body.edit .editor { display: flex; max-height: 46vh; }
  body.edit .views .vrow.mobile-only { display: none; }
  .ed-help kbd, .ed-save kbd { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
.editor .hint { color: var(--muted); }
