*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0d0d0d;
  --surface:  #161616;
  --surface2: #1e1e1e;
  --border:   #252525;
  --text:     #eef1f5;
  --muted:    #8b95a3;
  --accent:   #4ea1ff;
  --moving:   #4ea1ff;
  --anchored: #ffb12e;
  --moored:   #34d17e;
  --red:      #ff5d5d;
  --font: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

html, body { height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 12px; }

/* ── Header ─────────────────────────────────────────── */
header {
  display: flex; align-items: center; gap: 14px;
  padding: 0 14px; height: 36px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  font-size: 11px; letter-spacing: 0.08em;
  position: relative; z-index: 1000;
}
.logo { font-weight: 700; color: var(--accent); letter-spacing: 0.22em; font-size: 12px; }
#refresh-status { color: var(--muted); font-size: 10px; }
#last-updated { margin-left: auto; color: var(--muted); font-size: 10px; }

/* ── Layout ─────────────────────────────────────────── */
main {
  display: grid;
  grid-template-columns: 1fr 360px;
  height: calc(100vh - 36px);
}

/* ── Map ────────────────────────────────────────────── */
#map { width: 100%; height: 100%; }

/* Light basemap — no darkening filter */
.leaflet-container { background: #aad3df; } /* ocean blue, shown while tiles load */
.leaflet-popup-content-wrapper {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 2px; box-shadow: none;
  font-family: var(--font); font-size: 11px;
}
.leaflet-popup-tip { background: var(--surface); }
.leaflet-popup-content { margin: 10px 14px; line-height: 1.7; }
.leaflet-popup-content b { color: var(--accent); }
.gmaps-link {
  display: inline-block; margin-top: 6px;
  color: var(--accent); text-decoration: none; font-weight: 600;
}
.gmaps-link:hover { text-decoration: underline; }
.leaflet-control-attribution { font-size: 9px; opacity: 0.4; }

/* Ship name label */
.ship-label {
  color: #ffffff; font-family: var(--font); font-size: 11px; font-weight: 600;
  background: rgba(0,0,0,0.70); padding: 1px 5px; border-radius: 3px;
  white-space: nowrap; pointer-events: none;
}

/* ── Sidebar ─────────────────────────────────────────── */
#sidebar {
  display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--border);
  overflow-y: auto; overflow-x: hidden;
}

.section-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 12px;
  background: var(--surface2); border-bottom: 1px solid var(--border);
  font-size: 10px; letter-spacing: 0.16em; color: var(--muted);
  text-transform: uppercase; flex-shrink: 0;
}

/* Track controls */
#track-controls { border-bottom: 1px solid var(--border); flex-shrink: 0; }
#track-controls .section-header span {
  color: var(--accent); font-size: 12px; text-transform: none;
  letter-spacing: 0; font-weight: 600;
}
#clear-track {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 13px; padding: 0 4px; line-height: 1;
}
#clear-track:hover { color: var(--text); }
.track-range-row {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; flex-shrink: 0;
}
.track-range-row label { color: var(--muted); font-size: 10px; white-space: nowrap; }
#track-range {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); padding: 4px 6px;
  font-family: var(--font); font-size: 11px; flex: 1; cursor: pointer;
}
.hidden { display: none !important; }

/* ── Incident banner ─────────────────────────────────── */
/* Shown by app.js when the whole fleet's freshest fix goes stale (a feed-wide
   outage), hidden again automatically once positions resume. */
.incident-banner {
  background: var(--anchored); color: #1a1300;
  padding: 7px 14px; text-align: center;
  font-size: 11px; font-weight: 700; letter-spacing: 0.04em;
  border-bottom: 1px solid #00000033;
  position: relative; z-index: 1001;
}

/* ── Fleet table ─────────────────────────────────────── */
#fleet-section { flex-shrink: 0; }
/* table-layout: fixed + percentage column widths keep the panel scroll-free. */
.table-wrap { overflow-x: hidden; }
#fleet-table { width: 100%; table-layout: fixed; border-collapse: collapse; }
#fleet-table th, #fleet-table td {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#fleet-table th {
  padding: 6px 5px; text-align: left; font-weight: 400;
  color: var(--muted); border-bottom: 1px solid var(--border);
  font-size: 10px; letter-spacing: 0.06em;
  cursor: pointer; user-select: none;
}
#fleet-table th:hover, #fleet-table th.sorted { color: var(--text); }
.sort-arrow { font-size: 8px; margin-left: 2px; color: var(--accent); }
#fleet-table th:nth-child(1) { width: auto; }  /* Ship — gets the remainder */
#fleet-table th:nth-child(2) { width: 12%; }   /* Spd */
#fleet-table th:nth-child(3) { width: 24%; }   /* Status */
#fleet-table th:nth-child(4) { width: 14%; }   /* Stat. */
#fleet-table th:nth-child(5) { width: 22%; }   /* Last Seen */
#fleet-table td { padding: 7px 6px; border-bottom: 1px solid var(--border); }
#fleet-table tbody tr { cursor: pointer; transition: background 0.1s; }
#fleet-table tbody tr:hover { background: var(--surface2); }
#fleet-table tbody tr.active { background: #132038; }

.spd { font-weight: 600; }
.spd-moving  { color: var(--moving); }
.spd-slow    { color: var(--anchored); }
.spd-stop    { color: var(--muted); }

.stat-h { color: var(--muted); font-size: 11px; }
.stat-h-long { color: var(--anchored); }
.stat-h-vlong { color: var(--red); }

/* Nav status badge */
.badge {
  display: inline-block; padding: 1px 5px; font-size: 10px;
  letter-spacing: 0.04em; border-radius: 1px;
}
.badge-moving   { background: #0d2040; color: var(--moving); }
.badge-moored   { background: #0a2015; color: var(--moored); }
.badge-anchored { background: #2a1e00; color: var(--anchored); }
.badge-unknown  { background: #1a1a1a; color: var(--muted); }

/* Coverage staleness (last-seen age) — table cell + popup */
.seen-fresh  { color: var(--muted); }
.seen-stale  { color: var(--anchored); }
.seen-vstale { color: var(--red); }

/* ── Fleet roster management ─────────────────────────── */
#manage-toggle {
  background: none; border: 1px solid var(--border); color: var(--muted);
  cursor: pointer; font-family: var(--font); font-size: 9px;
  letter-spacing: 0.1em; padding: 2px 6px; border-radius: 2px; text-transform: uppercase;
}
#manage-toggle:hover { color: var(--text); border-color: var(--muted); }

#roster-panel { flex-shrink: 0; border-bottom: 1px solid var(--border); }
#roster-close {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 13px; padding: 0 4px; line-height: 1;
}
#roster-close:hover { color: var(--text); }

.roster-form { display: flex; gap: 6px; padding: 8px 12px; }
.roster-form input {
  background: var(--bg); color: var(--text); border: 1px solid var(--border);
  padding: 4px 6px; font-family: var(--font); font-size: 11px; min-width: 0;
}
#add-mmsi { flex: 0 0 96px; }
#add-name { flex: 1; }
.roster-form button {
  background: var(--accent); color: #0a0a0a; border: none; cursor: pointer;
  font-family: var(--font); font-size: 11px; font-weight: 600; padding: 4px 10px;
}
.roster-form button:hover { filter: brightness(1.1); }

.roster-error {
  color: var(--red); font-size: 10px; padding: 0 12px 6px;
}

.roster-list { padding: 0 0 6px; }
.roster-row {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 12px; border-top: 1px solid var(--border);
}
.roster-name { flex: 1; color: var(--text); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.roster-mmsi { color: var(--muted); font-size: 10px; }
.roster-del {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 12px; line-height: 1; padding: 0 2px;
}
.roster-del:hover { color: var(--red); }
.roster-empty { color: var(--muted); font-size: 11px; padding: 8px 12px; }

/* ── Stat cards ──────────────────────────────────────── */
#speed-section, #stationary-section { flex-shrink: 0; }

.stat-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 1px; background: var(--border); border-top: none;
}
.stat-card {
  background: var(--surface2); padding: 10px 12px;
  display: flex; flex-direction: column; gap: 3px;
  cursor: pointer; transition: background 0.1s;
}
.stat-card:hover { background: #1a1a1a; }
.stat-card.active { background: #132038; }
.stat-label {
  font-size: 10px; color: var(--muted); letter-spacing: 0.08em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.stat-value { font-size: 20px; font-weight: 700; line-height: 1.1; }
.stat-unit  { font-size: 10px; color: var(--muted); }

.val-moving   { color: var(--moving); }
.val-anchored { color: var(--anchored); }
.val-moored   { color: var(--moored); }
.val-stop     { color: var(--muted); }
.val-long     { color: var(--anchored); }
.val-vlong    { color: var(--red); }
.val-underway { color: var(--moored); font-size: 13px; }

/* ── Live logs panel ─────────────────────────────────── */
/* Button group on the right of a section header (Logs + Manage). */
.section-actions { display: flex; align-items: center; gap: 6px; }

#logs-toggle, #logs-pause, #logs-clear {
  background: none; border: 1px solid var(--border); color: var(--muted);
  cursor: pointer; font-family: var(--font); font-size: 9px;
  letter-spacing: 0.1em; padding: 2px 6px; border-radius: 2px; text-transform: uppercase;
}
#logs-toggle:hover, #logs-pause:hover, #logs-clear:hover { color: var(--text); border-color: var(--muted); }
#logs-close {
  background: none; border: none; color: var(--muted);
  cursor: pointer; font-size: 13px; padding: 0 4px; line-height: 1;
}
#logs-close:hover { color: var(--text); }

/* The panel takes the remaining sidebar height; its list scrolls internally. */
#logs-panel {
  flex: 1 1 auto; display: flex; flex-direction: column; min-height: 160px;
  border-bottom: 1px solid var(--border);
}
.logs-status { font-size: 9px; letter-spacing: 0.06em; text-transform: none; }
.logs-status-live          { color: var(--moored); }
.logs-status-connecting,
.logs-status-reconnecting  { color: var(--anchored); }
.logs-status-offline       { color: var(--red); }

.logs-filters {
  display: flex; flex-wrap: wrap; gap: 10px;
  padding: 6px 12px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.logs-filters label { display: flex; align-items: center; gap: 3px; color: var(--muted); font-size: 10px; cursor: pointer; }

.logs-list {
  flex: 1 1 auto; overflow-y: auto; min-height: 0;
  font-size: 10px; line-height: 1.5; padding: 4px 0;
}
.log-row {
  display: grid; grid-template-columns: 58px 42px 96px 1fr; gap: 6px;
  padding: 1px 12px; white-space: nowrap;
}
.log-row:hover { background: var(--surface2); }
.log-t      { color: var(--muted); }
.log-kind   { text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.log-ship   { overflow: hidden; text-overflow: ellipsis; }
.log-mmsi   { color: var(--muted); }
.log-detail { overflow: hidden; text-overflow: ellipsis; }

.log-rx     .log-kind   { color: var(--accent); }
.log-write              { color: var(--moored); }
.log-write  .log-kind   { color: var(--moored); }
.log-skip               { color: var(--muted); }
.log-influx             { color: var(--accent); }
.log-influx .log-kind   { color: var(--accent); }
.log-ws                 { color: var(--anchored); }
.log-ws     .log-kind   { color: var(--anchored); }

.logs-list.hide-rx     .log-rx,
.logs-list.hide-write  .log-write,
.logs-list.hide-skip   .log-skip,
.logs-list.hide-influx .log-influx,
.logs-list.hide-ws     .log-ws { display: none; }

/* ── Mobile view tabs ────────────────────────────────── */
/* Hidden on desktop; the side-by-side grid layout is used as-is there. */
.view-tabs { display: none; }

@media (max-width: 768px) {
  .view-tabs {
    display: flex; height: 40px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    position: relative; z-index: 1000;
  }
  .view-tabs button {
    flex: 1; background: none; border: none; cursor: pointer;
    color: var(--muted); font-family: var(--font); font-size: 12px;
    letter-spacing: 0.12em; text-transform: uppercase;
    border-bottom: 2px solid transparent;
  }
  .view-tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

  /* Stack into one full-width column; the tab bar adds 40px above main. */
  main { grid-template-columns: 1fr; height: calc(100vh - 36px - 40px); }

  #map, #sidebar { width: 100%; height: 100%; }
  #sidebar { border-left: none; }

  body.view-list #map     { display: none; }
  body.view-list #sidebar { display: flex; }
  body.view-map  #sidebar { display: none; }
  body.view-map  #map     { display: block; }
}
