/* ── Base ──────────────────────────────────────────────────────────────── */
* { box-sizing: border-box; }

html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
               "Helvetica Neue", Arial, sans-serif;
  color: #2a2a2a;
  background: #f5f5f5;
}

#app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
}

/* Wide-panel mode: tabs that benefit from the full window (Zones / Nodes).
   The sidebar takes the whole viewport and the map column collapses. */
#app.wide-panel { grid-template-columns: 1fr; }
#app.wide-panel #map-container { display: none; }
#app.wide-panel #sidebar {
  max-width: none;
  padding: 0 0 24px 0;
}
/* Lay diagram cards in a responsive grid so we use the extra space. */
#app.wide-panel #zones-content,
#app.wide-panel #nodes-content {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
  gap: 8px;
}
#app.wide-panel .zone-card { margin-bottom: 0; }
/* Constrain global controls and the nodes selector so they don't
   stretch ridiculously wide on a 1920-wide screen. */
#app.wide-panel #global-controls,
#app.wide-panel .tabpanel.active > .panel:first-child > .selection-row,
#app.wide-panel .tabpanel.active > .panel:first-child > .hint,
#app.wide-panel .tabpanel.active > .panel:first-child > h2 {
  max-width: 720px;
}
#app.wide-panel #zones-content,
#app.wide-panel #nodes-content { max-width: 1600px; }

/* ── Sidebar ───────────────────────────────────────────────────────────── */
#sidebar {
  background: #ffffff;
  border-right: 1px solid #e0e0e0;
  overflow-y: auto;
  padding: 0 0 24px 0;
  box-shadow: 2px 0 4px rgba(0,0,0,0.04);
}

#sidebar header {
  padding: 18px 20px 14px;
  background: linear-gradient(135deg, #1f3964 0%, #2e75b6 100%);
  color: white;
}
#sidebar header h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.3px;
}
#sidebar header .subtitle {
  font-size: 12px;
  opacity: 0.85;
  margin-top: 2px;
}

.panel {
  padding: 14px 20px;
  border-bottom: 1px solid #eeeeee;
}

/* ── Tabs ──────────────────────────────────────────────────────────────── */
#tabs {
  display: flex;
  border-bottom: 1px solid #ddd;
  background: #fafafa;
  position: sticky;
  top: 0;
  z-index: 5;
}
#tabs .tab {
  flex: 1;
  padding: 9px 4px;
  border: none;
  background: transparent;
  color: #777;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
#tabs .tab:hover { background: #f0f0f0; color: #444; }
#tabs .tab.active {
  color: #1f3964;
  border-bottom-color: #2e75b6;
  background: white;
}

.tabpanel { display: none; }
.tabpanel.active { display: block; }

.placeholder {
  font-size: 12px;
  color: #999;
  padding: 12px;
  border: 1px dashed #ddd;
  border-radius: 4px;
  background: #fafafa;
  text-align: center;
}

/* ── FB tab tables ─────────────────────────────────────────────────────── */
#fb-dispatch-table {
  width: 100%;
  margin-top: 8px;
  border-collapse: collapse;
  font-size: 11px;
}
#fb-dispatch-table th, #fb-dispatch-table td {
  padding: 3px 6px;
  border-bottom: 1px solid #f4f4f4;
  text-align: left;
}
#fb-dispatch-table th {
  font-size: 10px; color: #666; text-transform: uppercase;
  letter-spacing: 0.4px; font-weight: 600;
}
#fb-dispatch-table td.num {
  text-align: right;
  font-family: "SF Mono", Menlo, Consolas, monospace;
}
#fb-dispatch-table td.dim { color: #999; font-size: 10px; }
.np-pos { color: #1c6e2c; font-weight: 600; }
.np-neg { color: #c0392b; font-weight: 600; }

/* Computed-CNEC summary card + zonal table */
.cnec-summary {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  gap: 6px;
  margin: 8px 0;
}
.cnec-summary > div {
  background: #f7fbff;
  border: 1px solid #e3edf6;
  border-radius: 4px;
  padding: 4px 6px;
}
.cnec-summary span {
  display: block; font-size: 9.5px; color: #666;
  text-transform: uppercase; letter-spacing: 0.4px;
}
.cnec-summary b {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px; color: #1f3964;
}
.cnec-summary .ram-ok       { background: #eaf4ec; border-color: #c8e0cd; }
.cnec-summary .ram-ok b     { color: #1c6e2c; }
.cnec-summary .ram-violated { background: #fdecea; border-color: #f3c2bd; }
.cnec-summary .ram-violated b { color: #c0392b; }

table.cnec-zonal {
  width: 100%; border-collapse: collapse; font-size: 11px;
}
table.cnec-zonal th {
  font-size: 10px; color: #666; text-transform: uppercase;
  letter-spacing: 0.4px; padding: 3px 4px;
  border-bottom: 1px solid #ddd; text-align: left;
}
table.cnec-zonal td {
  padding: 2px 4px;
  border-bottom: 1px solid #f4f4f4;
}
table.cnec-zonal td.num {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  text-align: right;
}
.zbar { height: 8px; border-radius: 2px; }
.zbar.pos { background: #ef8a62; }
.zbar.neg { background: #67a9cf; }

/* JAO CNEC dropdown + detail view */
#fb-jao-cnec {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 10.5px;
}
#fb-jao-detail { margin-top: 8px; }
.jao-meta {
  font-size: 11px;
  background: #fafafa;
  border: 1px solid #eee;
  border-radius: 4px;
  padding: 6px 8px;
  margin-bottom: 6px;
  line-height: 1.45;
}
.jao-meta > div { display: flex; gap: 6px; }
.jao-meta span {
  width: 84px;
  flex: 0 0 84px;
  color: #888;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
table.cnec-zonal tr.virtual-hub td:first-child {
  color: #888;
  font-style: italic;
}
.panel h2 {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.7px;
  color: #666;
  margin: 0 0 10px 0;
}

.hint {
  font-size: 12px;
  color: #777;
  margin: 8px 0 0 0;
  line-height: 1.4;
}

.dim { color: #999; font-size: 12px; }

/* ── Mode buttons ──────────────────────────────────────────────────────── */
.mode-buttons {
  display: flex;
  gap: 4px;
}
.mode-btn {
  flex: 1;
  padding: 7px 6px;
  border: 1px solid #ccc;
  background: #f8f8f8;
  color: #555;
  font-size: 12px;
  font-weight: 600;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s ease;
}
.mode-btn:hover { background: #eee; }
.mode-btn.active {
  background: #2e75b6;
  color: white;
  border-color: #2e75b6;
}

/* ── Selection rows ────────────────────────────────────────────────────── */
.selection-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 13px;
}
.selection-row label {
  width: 78px;
  color: #666;
  font-size: 12px;
}
.selection-row .selected-bus {
  flex: 1;
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  color: #1f3964;
  font-weight: 600;
  background: #f0f6fc;
  padding: 3px 6px;
  border-radius: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.small-btn {
  padding: 2px 8px;
  font-size: 11px;
  border: 1px solid #ccc;
  background: white;
  color: #888;
  border-radius: 3px;
  cursor: pointer;
}
.small-btn:hover { background: #eee; color: #333; }

.gsk-select {
  flex: 1;
  padding: 3px 6px;
  font-size: 12px;
  border: 1px solid #ccc;
  border-radius: 3px;
  background: white;
  color: #333;
  cursor: pointer;
}

#outage-list {
  list-style: none;
  padding: 0;
  margin: 8px 0 0 0;
  max-height: 140px;
  overflow-y: auto;
}
#outage-list li {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 11px;
  color: #c0392b;
  padding: 3px 6px;
  background: #fdecea;
  border-radius: 3px;
  margin-bottom: 3px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#outage-list .remove-btn {
  color: #888;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}
#outage-list .remove-btn:hover { color: #c0392b; }

.asset-list {
  list-style: none;
  padding: 0;
  margin: 8px 0 0 0;
  max-height: 260px;
  overflow-y: auto;
  border-top: 1px solid #eee;
}
.asset-list li {
  font-family: "SF Mono", Menlo, Consolas, monospace;
  font-size: 10.5px;
  padding: 4px 6px;
  border-bottom: 1px solid #f3f3f3;
  line-height: 1.3;
}
.asset-list li.matched { background: #eaf4ec; color: #1c6e2c; }
.asset-list li.unmatched { background: #fff7e8; color: #8a5a00; }
.asset-list .a-name { font-weight: 600; }
.asset-list .a-meta { color: #666; font-size: 10px; }
.asset-list .a-bt { display: inline-block; padding: 0 4px; border-radius: 2px;
  background: #ddd; color: #333; margin-left: 4px; font-size: 9.5px; }
.asset-list .a-bt.A53 { background: #d6e6f5; color: #1c4d80; }
.asset-list .a-bt.A54 { background: #f5d6d6; color: #802020; }

/* ── Radio group ───────────────────────────────────────────────────────── */
.radio-group { display: flex; flex-direction: column; gap: 4px; }
.radio-group label {
  font-size: 12px;
  color: #555;
  cursor: pointer;
}
.radio-group input { margin-right: 6px; }

/* ── Results table ─────────────────────────────────────────────────────── */
#result-table {
  width: 100%;
  margin-top: 8px;
  border-collapse: collapse;
  font-size: 11px;
}
#result-table th {
  text-align: left;
  padding: 4px 6px;
  border-bottom: 1px solid #ddd;
  color: #666;
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
#result-table td {
  padding: 3px 6px;
  border-bottom: 1px solid #f4f4f4;
  font-family: "SF Mono", Menlo, Consolas, monospace;
}
#result-table td.ptdf-val {
  text-align: right;
  font-weight: 600;
}
#result-table tbody tr:hover { background: #f7fbff; }

/* ── Zone / Node diagram cards ─────────────────────────────────────────── */
.zone-card {
  border: 1px solid #e6e6e6;
  border-radius: 4px;
  padding: 6px 8px;
  margin-bottom: 6px;
  background: #fff;
}
.zone-card-h {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 4px;
}
.zone-card-z {
  font-weight: 700; font-size: 12px; color: #1f3964;
  font-family: "SF Mono", Menlo, Consolas, monospace;
}
.zone-card-meta {
  font-size: 10.5px; color: #666;
  font-family: "SF Mono", Menlo, Consolas, monospace;
}
.zd-row {
  display: flex; align-items: center; gap: 6px;
  margin: 2px 0;
}
.zd-label {
  width: 44px; font-size: 9.5px; color: #888;
  text-transform: uppercase; letter-spacing: 0.4px;
  flex: 0 0 44px;
}
.zd-bar {
  height: 10px; display: flex; min-width: 1px;
  background: #f0f0f0; border-radius: 2px; overflow: hidden;
}
.zd-bar-load { background: #f3a683; }
.zd-seg { height: 100%; }
.zd-np-track {
  position: relative; height: 10px;
  background: #f0f0f0; border-radius: 2px;
  flex: 1;
}
.zd-np-zero {
  position: absolute; top: 0; bottom: 0;
  left: 50%; width: 1px; background: #aaa;
}
.zd-np-bar {
  position: absolute; top: 0; bottom: 0;
  border-radius: 2px;
}
.zd-np-bar.pos { background: #1c6e2c; }
.zd-np-bar.neg { background: #c0392b; }
.zd-legend {
  display: flex; flex-wrap: wrap; gap: 4px 10px;
  margin-top: 8px; padding-top: 6px;
  border-top: 1px solid #eee;
  font-size: 10px; color: #555;
}
.zd-leg-item { display: inline-flex; align-items: center; gap: 4px; }
.zd-leg-sw {
  display: inline-block; width: 10px; height: 10px;
  border-radius: 2px;
}

/* ── Map ───────────────────────────────────────────────────────────────── */
#map-container { position: relative; }
#map { width: 100%; height: 100%; }

/* Custom marker styles (added via JS) */
.bus-marker {
  border-radius: 50%;
  border: 2px solid white;
  box-shadow: 0 1px 3px rgba(0,0,0,0.4);
  cursor: pointer;
  transition: transform 0.1s ease;
}
.bus-marker:hover { transform: scale(1.25); }
.bus-marker.selected-inject {
  border-color: #27ae60;
  border-width: 3px;
}
.bus-marker.selected-withdraw {
  border-color: #8e44ad;
  border-width: 3px;
}
.bus-marker.slack {
  border-color: #f39c12;
}

.zone-label > div {
  font-size: 11px;
  font-weight: 700;
  color: #222;
  text-shadow: 0 0 3px white, 0 0 3px white, 0 0 3px white;
  white-space: nowrap;
}

/* ── Legend ────────────────────────────────────────────────────────────── */
#legend {
  position: absolute;
  bottom: 20px; right: 20px;
  z-index: 500;
}
#ptdf-legend {
  background: white;
  padding: 8px 12px;
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  font-size: 11px;
  min-width: 160px;
}
#ptdf-legend.hidden { display: none; }
.legend-title {
  font-weight: 600;
  margin-bottom: 4px;
  color: #444;
}
.legend-gradient {
  height: 10px;
  background: linear-gradient(to right,
    #2166ac 0%, #67a9cf 25%, #f7f7f7 50%, #ef8a62 75%, #b2182b 100%);
  border-radius: 2px;
  margin-bottom: 3px;
}
.legend-labels {
  display: flex;
  justify-content: space-between;
  color: #777;
  font-size: 10px;
}

/* ── Login overlay ─────────────────────────────────────────────────────────
   Full-viewport scrim. Sits above the app (z above leaflet's 400 / 1000)
   so the user can't click through it before authenticating. */
#login-overlay {
  position: fixed; inset: 0;
  background: rgba(20, 30, 50, 0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 10000;
  backdrop-filter: blur(2px);
}
#login-overlay.hidden { display: none; }
.login-card {
  background: #fff;
  padding: 28px 32px;
  border-radius: 8px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.25);
  width: 320px;
  display: flex; flex-direction: column; gap: 12px;
  font-family: inherit;
}
.login-card h2 { margin: 0; color: #1f3a68; font-size: 20px; }
.login-card .login-sub { margin: 0 0 4px; color: #555; font-size: 13px; }
.login-card label {
  display: flex; flex-direction: column; gap: 4px;
  font-size: 12px; color: #444;
}
.login-card input {
  padding: 8px 10px; font-size: 14px;
  border: 1px solid #ccd; border-radius: 4px;
}
.login-card input:focus { outline: 2px solid #2e75b6; outline-offset: -1px; }
.login-btn {
  margin-top: 4px;
  padding: 9px;
  background: #1f3a68; color: #fff;
  border: 0; border-radius: 4px;
  font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.login-btn:hover { background: #2e75b6; }
.login-btn[disabled] { opacity: 0.6; cursor: wait; }
.login-error {
  color: #b00020; font-size: 12px; min-height: 1em;
}
.login-hint {
  margin: 0; color: #888; font-size: 11px; line-height: 1.4;
}

/* ── Logout button in the sidebar header ───────────────────────────────── */
.logout-btn {
  margin-left: 8px;
  padding: 2px 8px;
  font-size: 11px;
  background: transparent;
  border: 1px solid #bbb;
  color: #666;
  border-radius: 3px;
  cursor: pointer;
  vertical-align: middle;
}
.logout-btn:hover { background: #f0f0f0; color: #1f3a68; border-color: #2e75b6; }
.logout-btn.hidden { display: none; }

