:root {
  --accent: #1f6feb;
  --panel-bg: #ffffff;
  --panel-text: #1b1f24;
  --muted: color-mix(in srgb, var(--panel-text) 55%, transparent);
  --line: color-mix(in srgb, var(--panel-text) 14%, transparent);
  --soft: color-mix(in srgb, var(--accent) 12%, var(--panel-bg));
  --on-accent: #ffffff;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--panel-bg); color: var(--panel-text); }
#app { position: fixed; inset: 0; display: flex; }
#map { flex: 1; height: 100%; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 8px; padding: 8px 12px; background: var(--soft); color: var(--panel-text); }
button.primary { background: var(--accent); color: var(--on-accent); }
button.ghost { background: transparent; border: 1px solid var(--line); }
button:disabled { opacity: .45; cursor: default; }
input, select { font: inherit; color: var(--panel-text); background: var(--panel-bg); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }

/* Top bar */
#topbar { position: absolute; z-index: 1000; top: 10px; left: 10px; display: flex; gap: 12px; align-items: center;
  background: var(--panel-bg); color: var(--panel-text); padding: 8px 12px; border-radius: var(--radius); box-shadow: 0 2px 10px #0003; max-width: calc(100% - 440px); }
.brand { font-weight: 700; display: flex; align-items: center; gap: 6px; color: var(--accent); white-space: nowrap; }
.brand img { width: 22px; height: 22px; }
#status { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

#mapbuttons { position: absolute; z-index: 1000; left: 10px; bottom: 24px; display: flex; gap: 8px; }
.fab { background: var(--accent); color: var(--on-accent); padding: 12px 16px; border-radius: 999px; box-shadow: 0 2px 10px #0004; font-weight: 600; }
.fab.small { width: 46px; height: 46px; padding: 0; font-size: 22px; line-height: 1; }
.fab.on { outline: 3px solid color-mix(in srgb, var(--accent) 40%, white); }

#selectbar { position: absolute; z-index: 1001; left: 50%; transform: translateX(calc(-50% - 200px)); top: 64px; width: min(560px, calc(100% - 460px));
  background: var(--panel-bg); color: var(--panel-text); border: 2px solid var(--accent); border-radius: var(--radius); padding: 10px 12px; box-shadow: 0 2px 14px #0004; }
#selectbar[hidden], #reportbar[hidden] { display: none; }
#reportbar { position: absolute; z-index: 1001; left: 10px; bottom: 80px; width: min(420px, calc(100% - 440px));
  background: var(--panel-bg); color: var(--panel-text); border: 2px solid #e5484d; border-radius: var(--radius); padding: 10px 12px; box-shadow: 0 2px 14px #0004; display: flex; flex-direction: column; gap: 8px; }
#rNote { width: 100%; }
.fab.report { background: #e5484d; color: #fff; }
.report-icon { font-size: 20px; filter: drop-shadow(0 0 2px #fff); }
#selectmsg { margin-bottom: 8px; font-weight: 500; }

/* Side panel (desktop) */
#panel { width: 400px; height: 100%; display: flex; flex-direction: column; background: var(--panel-bg); color: var(--panel-text); border-left: 1px solid var(--line); z-index: 1002; }
#tabs { display: flex; border-bottom: 1px solid var(--line); }
#tabs button { flex: 1; background: transparent; border-radius: 0; padding: 10px 4px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; color: var(--muted); position: relative; border-bottom: 3px solid transparent; }
#tabs button span { font-size: 18px; }
#tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }
#alertBadge { position: absolute; top: 4px; right: 18%; background: #e5484d; color: #fff; font-size: 10px; border-radius: 999px; padding: 1px 5px; }
.tab { display: none; padding: 12px; overflow-y: auto; flex: 1; }
.tab.active { display: block; }
.tab h3 { margin: 18px 0 8px; font-size: 14px; color: var(--accent); text-transform: uppercase; letter-spacing: .04em; }
.tab h3:first-child { margin-top: 0; }
#trainSearch { width: 100%; margin-bottom: 8px; }
.list { display: flex; flex-direction: column; gap: 6px; }
.item { border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; cursor: pointer; }
.item:hover { background: var(--soft); }
.item .t { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.item .s { font-size: 12px; color: var(--muted); margin-top: 2px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; flex: none; }
.field { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin: 8px 0; font-size: 14px; }
.field input[type=color] { width: 44px; height: 32px; padding: 2px; }
.check { display: flex; gap: 8px; align-items: flex-start; margin: 8px 0; font-size: 14px; }
.check input { margin-top: 3px; accent-color: var(--accent); }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.swatches button { width: 34px; height: 34px; border-radius: 50%; padding: 0; border: 2px solid var(--line); }
.sec-edit { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.sec-edit input[type=text] { width: 100%; }

/* Train markers */
.train-icon { display: flex; align-items: center; justify-content: center; }
.train-icon .b { width: 26px; height: 26px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 1px 5px #0006; display: flex; align-items: center; justify-content: center; font-size: 13px; position: relative; }
.train-icon .arrow { position: absolute; top: -9px; left: 50%; margin-left: -5px; width: 0; height: 0; border-left: 5px solid transparent; border-right: 5px solid transparent; border-bottom: 8px solid; }
.train-icon.stopped .b { outline: 3px solid #e5484d; }
.train-label { background: transparent; border: 0; box-shadow: none; font-weight: 700; font-size: 11px; color: #111; text-shadow: 0 0 3px #fff, 0 0 3px #fff; }
.cam-icon { font-size: 15px; filter: drop-shadow(0 0 2px #fff); }
.leaflet-popup-content { font-size: 13px; line-height: 1.45; }
.leaflet-popup-content h4 { margin: 0 0 4px; font-size: 15px; }
.leaflet-popup-content table td:first-child { color: #666; padding-right: 8px; vertical-align: top; }

.xing { width: 16px; height: 16px; border-radius: 50%; background: #fff; border: 2px solid #6b7280; display: flex; align-items: center; justify-content: center; font-size: 10px; font-weight: 900; color: #6b7280; box-shadow: 0 0 3px #0006; line-height: 1; }
.xing.active { border-color: #e5484d; color: #e5484d; }
.xing.often { box-shadow: 0 0 0 3px #f59e0b, 0 0 3px #0006; }
.xing.blocked { background: #e5484d; color: #fff; border-color: #fff; }
.est { margin-top: 6px; padding: 6px 8px; border-radius: 8px; background: color-mix(in srgb, #f59e0b 14%, transparent); font-size: 12px; line-height: 1.4; }
.est b { display: block; }
.stop-meta { font-size: 12px; color: var(--muted); margin-top: 3px; line-height: 1.45; }
.tag { display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 999px; background: #f59e0b; color: #111; margin-left: 4px; }
.popup-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.popup-actions button { font-size: 12px; padding: 6px 8px; }

a.btn { display: inline-block; text-decoration: none; padding: 9px 14px; border-radius: 8px; font-weight: 600; }
a.btn.primary { background: var(--accent); color: var(--on-accent); }
#gateLink { flex: 1; min-width: 0; }

/* Owner / guest */
body.guest .owner-only, body.none .owner-only { display: none !important; }
body:not(.guest) .guest-only { display: none !important; }
#gate { position: fixed; inset: 0; z-index: 5000; background: var(--panel-bg); display: flex; align-items: center; justify-content: center; padding: 20px; }
#gate[hidden] { display: none; }
.gate-card { max-width: 380px; text-align: center; }
.gate-card h2 { color: var(--accent); margin: 10px 0 4px; }
.gate-card details { margin-top: 18px; text-align: left; border: 1px solid var(--line); border-radius: 10px; padding: 10px; }
.gate-card summary { cursor: pointer; font-weight: 600; }
.gate-card input { flex: 1; min-width: 0; }
#shareBox code { word-break: break-all; display: block; padding: 6px 8px; border-radius: 8px; background: var(--soft); margin-top: 4px; }
#ownerCodeBox b { font-size: 22px; letter-spacing: .12em; }

/* Legend box */
.legend { background: var(--panel-bg); color: var(--panel-text); border-radius: 10px; box-shadow: 0 2px 10px #0003; font-size: 12px; max-width: 250px; }
.legend-toggle { background: transparent; color: var(--accent); font-weight: 700; padding: 6px 10px; width: 100%; text-align: left; }
.legend-body { display: none; padding: 0 10px 8px; max-height: 50vh; overflow-y: auto; }
.legend.open .legend-body { display: block; }
.lg-head { font-weight: 700; color: var(--accent); margin: 8px 0 3px; text-transform: uppercase; font-size: 10px; letter-spacing: .05em; }
.lg-row { display: flex; align-items: center; gap: 8px; margin: 3px 0; line-height: 1.25; }
.lg-icon { width: 28px; flex: none; display: flex; justify-content: center; align-items: center; }
.lg-train { width: 18px; height: 18px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 2px #0008; display: inline-flex; align-items: center; justify-content: center; font-size: 9px; }
.lg-stopped { outline: 2px solid #e5484d; }
.lg-sym { font-size: 15px; }
.lg-home { width: 12px; height: 12px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 2px #0008; display: inline-block; }
.lg-ring { width: 18px; height: 18px; border-radius: 50%; border: 2px dashed; display: inline-block; }

#toasts { position: fixed; z-index: 3000; top: 10px; right: 410px; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--panel-bg); color: var(--panel-text); border-left: 5px solid var(--accent); padding: 10px 12px; border-radius: 10px; box-shadow: 0 3px 14px #0004; max-width: 320px; font-size: 14px; }
.toast b { display: block; }

/* Phone and narrow screens: map on top, simple bottom sheet with tabs */
@media (max-width: 760px) {
  #app { flex-direction: column; }
  #map { flex: 1; }
  #panel { width: 100%; height: auto; max-height: 46vh; border-left: 0; border-top: 1px solid var(--line); flex-direction: column-reverse; padding-bottom: env(safe-area-inset-bottom); }
  #panel.collapsed .tab { display: none; }
  #tabs { border-bottom: 0; border-top: 1px solid var(--line); }
  #tabs button { padding: 8px 2px; border-bottom: 0; border-top: 3px solid transparent; }
  #tabs button.active { border-top-color: var(--accent); }
  #topbar { max-width: calc(100% - 20px); right: 10px; padding: 6px 10px; }
  #topbar #status { flex: 1; text-align: right; }
  #mapbuttons { bottom: auto; top: 60px; left: 10px; flex-direction: column; align-items: flex-start; }
  .fab { padding: 10px 14px; font-size: 14px; }
  #selectbar { left: 10px; right: 10px; width: auto; transform: none; top: auto; bottom: calc(46vh + 10px); }
  #panel.collapsed ~ #selectbar, #app:has(#panel.collapsed) #selectbar { bottom: 80px; }
  #reportbar { left: 10px; right: 10px; width: auto; bottom: calc(46vh + 10px); }
  #app:has(#panel.collapsed) #reportbar { bottom: 80px; }
  #toasts { right: 10px; left: 10px; top: 56px; }
  .toast { max-width: none; }
  .leaflet-control-zoom { display: none; }
  .legend { max-width: 220px; }
  .legend-body { max-height: 30vh; }
}
