:root {
  --bg: #151515;
  --panel: #1c1c1c;
  --grid: #2a2a2a;
  --border: #383838;
  --text: #e6e6e6;
  --text-dim: #8c8c8c;
  --accent: #f5f5f5;
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --sector-1: #fa0303;
  --sector-3: #fdd601;
  --sector-4: #14c74f;
  --ptb-yellow: var(--sector-3);
  --ptb-black: #000;
  --sidebar-width: 60ch;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, 'Segoe UI', Roboto, sans-serif;
  min-height: 100vh;
  overflow: hidden;
}

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--sidebar-width);
  height: 100vh;
  min-height: 0;
}

.map-wrap {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
}

.map-frame {
  position: relative;
  width: min(100%, calc(100vh - 48px) * 778 / 943);
  max-height: calc(100vh - 48px);
  aspect-ratio: 778 / 943;
  flex: 0 1 auto;
}

svg#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.route-group .hitareas path {
  fill: transparent;
  stroke: transparent;
  stroke-width: 12;
  pointer-events: all;
  cursor: pointer;
}

.route-group .visible-layer {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s var(--ease);
}

.route-group.active .visible-layer {
  opacity: 1;
}

#Track > path {
  transition: opacity 0.35s var(--ease);
}

svg#map.dimmed #Track > path {
  opacity: 0.25;
}

aside#legend {
  min-width: 0;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
  border-left: 1px solid var(--border);
  padding: 18px 0;
  z-index: 5;
}

.legend-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
}

aside#legend h1 {
  font-family: 'Rajdhani', sans-serif;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 0.01em;
  margin: 0 0 18px;
  color: var(--accent);
}

.legend-title-sep {
  margin: 0 8px;
  color: var(--text-dim);
  font-weight: 400;
}

#legend-toggle {
  display: none;
  cursor: pointer;
}

ul#route-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  height: calc(100vh - 74px);
  scrollbar-gutter: stable;
}

.legend-item {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  transition:
    opacity 0.35s var(--ease),
    background-color 0.15s var(--ease);
}

.legend-item:hover,
.legend-item.active {
  background: var(--grid);
}

#legend.dimmed .legend-item:not(.active) {
  opacity: 0.35;
}

.legend-item-header {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  min-height: 36px;
  margin: 0;
  gap: 10px;
  padding: 10px;
  background: none;
  border: 0;
  color: inherit;
  text-align: left;
  cursor: pointer;
  font:
    600 15px 'Rajdhani',
    sans-serif;
  line-height: 1;
  letter-spacing: 0.01em;
}

.legend-item-header > * {
  align-self: center;
}

.legend-head .sub {
  margin: -14px 0 4px;
  color: var(--text-dim);
  font-family: 'Titillium Web', sans-serif;
  font-size: 10px;
  font-weight: 400;
  line-height: 1.3;
  letter-spacing: 0.02em;
}

.legend-head .sub + .sub {
  margin-top: 0;
}

.legend-label {
  min-width: 0;
  flex: 1 1 auto;
  line-height: 1.1;
}

/* Reverted to old swatch styling */
.legend-swatch {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.legend-swatch span {
  height: 14px;
  border-radius: 1px;
  width: 6px;
}

.total-flag {
  width: 10px;
  height: 10px;
  flex: 0 0 10px;
  border-radius: 50%;
  background: conic-gradient(var(--ptb-black) 25%, #fff 0 50%, var(--ptb-black) 0 75%, #fff 0);
  background-size: 4px 4px;
}

/* Reverted to old PTB badge styling */
.legend-badge {
  margin-left: auto;
  font-family: 'Titillium Web', sans-serif;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--ptb-yellow);
  border: 3px solid transparent;
  border-radius: 3px;
  padding: 2px 5px;
  background:
    linear-gradient(var(--panel), var(--panel)) padding-box,
    repeating-linear-gradient(45deg, var(--ptb-black) 0 5px, var(--ptb-yellow) 5px 10px) border-box;
}

.legend-details {
  display: grid;
  grid-template-rows: 0fr;
  padding: 0 10px;
  transition: grid-template-rows 0.25s var(--ease);
}

.legend-item.expanded .legend-details {
  grid-template-rows: 1fr;
}

.legend-details-inner {
  min-height: 0;
  overflow: hidden;
}

.legend-details-inner .p-times {
  margin: 0 0 8px;
}

.p-times {
  width: 100%;
  border-collapse: collapse;
  table-layout: auto;
  font-size: 10.5px;
  margin: 0 0 8px;
}

.p-times col.sector-col {
  width: 0;
}

.p-times th:first-child,
.p-times td.sector-label {
  width: 1%;
  white-space: nowrap;
}

.p-times th {
  color: var(--text-dim);
  font-family: 'Rajdhani', sans-serif;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-align: left;
  padding: 5px 6px;
  border-bottom: 1px solid var(--border);
}

.p-times td {
  padding: 5px 6px;
  border-bottom: 1px solid var(--grid);
  vertical-align: middle;
}

.p-times td.sector-label,
.p-times th:first-child {
  color: var(--text-dim);
  padding-right: 12px;
}

.p-times .sector-label-inner {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* Reverted to old circular sector dots */
.p-times .row-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.p-times tr.total td {
  font-weight: 700;
  color: var(--accent);
  border-bottom: 0;
}

.p-times .car-name {
  display: block;
  color: var(--text-dim);
  font-size: 9px;
  line-height: 1.1;
  margin-top: 1px;
}

.p-times .no-data {
  color: var(--text-dim);
}

.p-times tfoot td {
  border-bottom: 0;
  padding-top: 8px;
  vertical-align: middle;
}

.p-link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-dim);
  text-decoration: none;
  font:
    700 9px 'Titillium Web',
    sans-serif;
  line-height: 1;
  letter-spacing: 0.03em;
  text-transform: uppercase;
}

.p-link-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

@media (max-width: 1000px) {
  body {
    overflow: auto;
  }

  .layout {
    display: block;
    height: auto;
    min-height: 100vh;
  }

  .map-wrap {
    min-height: 100vh;
  }

  aside#legend {
    position: fixed;
    inset: 0 0 0 auto;
    width: min(92vw, 430px);
    height: 100vh;
    transform: translateX(100%);
    transition: transform 0.25s var(--ease);
  }

  aside#legend.open {
    transform: translateX(0);
  }

  #legend-toggle {
    display: block;
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 20;
    padding: 8px 11px;
    background: var(--bg);
    border: 0;
    border-radius: 0;
    color: var(--text);
  }
}
