:root {
  --ink: #17231f;
  --muted: #6f7c76;
  --line: #e2e8e4;
  --panel: #ffffff;
  --canvas: #f4f7f5;
  --green: #1e7254;
  --green-dark: #15533d;
  --green-soft: #e5f3ed;
  --orange: #e67e32;
  --orange-soft: #fff0e4;
  --blue: #3075bb;
  --red: #d8483f;
  --shadow: 0 12px 34px rgba(25, 43, 35, .10);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--ink); background: var(--canvas); }
button, input { font: inherit; }
button { color: inherit; }

.app-shell { height: 100%; min-height: 650px; display: flex; flex-direction: column; }
.topbar { height: 74px; flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between; padding: 0 24px; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 1001; }
.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark { width: 38px; height: 38px; display: grid; place-items: center; border-radius: 11px; background: var(--green); color: white; font-size: 13px; font-weight: 800; letter-spacing: -.04em; }
.brand h1 { margin: 0; font-size: 18px; line-height: 1.2; letter-spacing: -.02em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.simulation-actions { display: flex; align-items: center; gap: 10px; }
.time-control { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11px; font-weight: 700; }
.time-control select { padding: 8px 26px 8px 9px; border: 1px solid var(--line); border-radius: 9px; background: white; color: var(--ink); font-weight: 800; cursor: pointer; }
.sim-status { color: var(--green); font-size: 12px; font-weight: 700; margin-right: 4px; display: flex; align-items: center; gap: 7px; }
.sim-status span { width: 7px; height: 7px; background: var(--green); border-radius: 50%; box-shadow: 0 0 0 4px var(--green-soft); }
.sim-status.paused { color: var(--muted); }
.sim-status.paused span { background: var(--muted); box-shadow: 0 0 0 4px #edf0ee; }
.button { border: 1px solid var(--line); border-radius: 9px; padding: 9px 14px; background: white; cursor: pointer; font-size: 13px; font-weight: 700; transition: .18s ease; }
.button:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(25, 43, 35, .09); }
.button-primary { color: white; background: var(--green); border-color: var(--green); min-width: 78px; }
.button-primary:hover { background: var(--green-dark); }
.config-button { display: inline-flex; align-items: center; gap: 5px; }

.workspace { width: 100%; flex: 1; min-width: 0; min-height: 0; display: grid; grid-template-columns: 330px minmax(0, 1fr); }
.control-panel { min-width: 0; background: var(--panel); overflow-y: auto; z-index: 1000; box-shadow: 6px 0 24px rgba(20, 50, 38, .06); }
.panel-section { padding: 21px 22px; border-bottom: 1px solid var(--line); }
.section-heading { display: flex; align-items: flex-start; gap: 11px; margin-bottom: 17px; }
.section-heading.compact { margin-bottom: 14px; }
.step-number { flex: 0 0 auto; width: 25px; height: 25px; display: grid; place-items: center; border-radius: 50%; background: var(--green); color: #fff; font-size: 12px; font-weight: 800; }
.section-heading h2 { margin: 1px 0 3px; font-size: 14px; }
.section-heading p { margin: 0; font-size: 11px; color: var(--muted); line-height: 1.4; }

.route-points { position: relative; display: grid; gap: 10px; }
.point-field { width: 100%; position: relative; z-index: 1; display: flex; align-items: center; gap: 10px; padding: 10px; text-align: left; border: 1px solid var(--line); border-radius: 10px; background: white; cursor: pointer; }
.point-field.active { border-color: var(--green); box-shadow: 0 0 0 3px var(--green-soft); }
.point-icon { width: 28px; height: 28px; flex: 0 0 auto; display: grid; place-items: center; border-radius: 50%; color: white; font-size: 12px; font-weight: 800; }
.point-a { background: var(--green); }
.point-b { background: var(--orange); }
.point-field strong, .point-field small { display: block; }
.point-field strong { font-size: 12px; }
.point-field small { max-width: 210px; margin-top: 2px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 10px; }
.route-line { position: absolute; left: 23px; top: 37px; bottom: 37px; border-left: 2px dotted #c9d3ce; }
.waypoint-list { display: grid; gap: 8px; }
.waypoint-field { position: relative; z-index: 1; display: flex; align-items: center; gap: 10px; min-width: 0; padding: 8px 9px; border: 1px solid var(--line); border-radius: 10px; background: white; }
.waypoint-number { width: 28px; height: 28px; flex: 0 0 auto; display: grid; place-items: center; border-radius: 50%; background: var(--route-color, var(--green)); color: white; font-size: 10px; font-weight: 800; }
.waypoint-details { min-width: 0; flex: 1; }
.waypoint-details strong, .waypoint-details small { display: block; }
.waypoint-details strong { font-size: 11px; }
.waypoint-details small { margin-top: 2px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 9px; }
.waypoint-remove { width: 24px; height: 24px; flex: 0 0 auto; display: grid; place-items: center; border: 0; border-radius: 6px; background: transparent; color: var(--muted); cursor: pointer; }
.waypoint-remove:hover { color: var(--red); background: #fff0ef; }
.waypoint-add { width: 100%; margin-top: 10px; padding: 8px; border: 1px dashed #b8c8c0; border-radius: 9px; background: transparent; color: var(--green); font-size: 10px; font-weight: 700; cursor: pointer; }
.waypoint-add:hover, .waypoint-add.active { border-style: solid; border-color: var(--green); background: var(--green-soft); }
.route-actions { display: flex; align-items: center; gap: 8px; margin-top: 11px; }
.add-route { flex: 1; padding: 8px 10px; color: var(--green); border-color: #b8d4c8; background: var(--green-soft); }
.route-delete { padding: 6px 0; border: 0; background: transparent; color: var(--muted); font-size: 10px; cursor: pointer; }
.route-delete:hover { color: var(--red); }
.route-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.route-tab { display: flex; align-items: center; gap: 6px; padding: 6px 9px; border: 1px solid var(--line); border-radius: 999px; background: white; color: var(--muted); font-size: 10px; font-weight: 700; cursor: pointer; }
.route-tab.active { color: var(--ink); border-color: var(--route-color); box-shadow: 0 0 0 2px color-mix(in srgb, var(--route-color) 14%, transparent); }
.route-tab-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--route-color); }
.route-name-label { display: block; margin-top: 10px; color: var(--muted); font-size: 9px; font-weight: 700; }
.route-name-label input { width: 100%; margin-top: 5px; padding: 8px 9px; border: 1px solid var(--line); border-radius: 8px; background: white; color: var(--ink); font-size: 11px; }
.route-name-label input:focus { outline: 2px solid var(--green-soft); border-color: var(--green); }

.range-label { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; font-size: 12px; }
.range-label.spaced { margin-top: 18px; }
.range-label strong { color: var(--green); font-size: 12px; }
input[type="range"] { width: 100%; height: 4px; accent-color: var(--green); cursor: pointer; }
.range-scale { display: flex; justify-content: space-between; margin-top: 5px; color: var(--muted); font-size: 9px; }

.tool-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.map-tool { min-height: 66px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--line); border-radius: 10px; background: white; cursor: pointer; font-size: 11px; font-weight: 700; transition: .15s ease; }
.map-tool:hover { background: var(--canvas); border-color: #c7d2cc; }
.map-tool.active { background: var(--green-soft); border-color: var(--green); color: var(--green-dark); box-shadow: 0 0 0 2px var(--green-soft); }
.tool-symbol { height: 22px; display: grid; place-items: center; font-weight: 900; }
.slow-symbol { width: 28px; color: var(--orange); font-size: 25px; letter-spacing: -8px; transform: rotate(-12deg); }
.stop-symbol { width: 27px; height: 27px; border-radius: 7px; background: var(--red); color: white; font-size: 6px; border: 2px solid white; outline: 1px solid var(--red); }
.light-symbol { width: 14px; height: 26px; background: #35413c; border-radius: 4px; display: flex; flex-direction: column; gap: 2px; padding: 3px; }
.light-symbol i { width: 6px; height: 6px; border-radius: 50%; background: #66736e; }
.light-symbol i:nth-child(1) { background: #ef5a50; }
.yield-symbol { color: var(--red); font-size: 29px; line-height: 1; }
.tool-hint { margin: 11px 0 0; color: var(--muted); font-size: 9px; text-align: center; }
.device-controls { margin-top: 12px; padding: 11px; border-radius: 9px; background: var(--orange-soft); }
.device-controls label { display: flex; justify-content: space-between; margin: 4px 0; font-size: 10px; }
.device-controls input { accent-color: var(--orange); }
.stop-controls { background: #fff0ef; }
.stop-controls input { accent-color: var(--red); }
.light-controls { background: #eef2f0; }
.light-controls input { accent-color: #46534d; }
.light-controls label + input + label { margin-top: 10px; }

.map-region { position: relative; min-width: 0; min-height: 0; }
#map { position: absolute; inset: 0; background: #dce5df; }
.map-overlay { position: absolute; z-index: 500; background: rgba(255,255,255,.94); border: 1px solid rgba(215,224,219,.9); box-shadow: var(--shadow); backdrop-filter: blur(8px); }
.map-message { top: 16px; left: 50%; transform: translateX(-50%); padding: 9px 15px; border-radius: 999px; font-size: 11px; font-weight: 700; pointer-events: none; transition: opacity .2s; }
.map-message.success { color: var(--green); }
.legend { right: 16px; top: 16px; display: flex; gap: 12px; padding: 9px 12px; border-radius: 9px; font-size: 10px; }
.legend span { display: flex; align-items: center; gap: 5px; }
.legend-dot { width: 9px; height: 9px; display: inline-block; border-radius: 50%; }
.legend-dot.car { background: var(--green); }
.legend-dot.bus { width: 13px; border-radius: 3px; background: var(--blue); }
.legend-dot.slowed { background: var(--orange); }
.route-metrics { left: 16px; bottom: 24px; width: min(690px, calc(100% - 32px)); border-radius: 12px; overflow: hidden; }
.metrics-title { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px 7px; }
.metrics-title strong { font-size: 11px; }
.metrics-title span { color: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.metrics-title b { color: var(--ink); }
.metrics-head, .metrics-row { display: grid; grid-template-columns: minmax(82px, 1.25fr) .65fr .65fr .8fr .7fr .95fr; align-items: center; gap: 7px; padding: 6px 12px; }
.metrics-head { background: var(--canvas); color: var(--muted); font-size: 8px; text-transform: uppercase; letter-spacing: .04em; }
.metrics-head span:not(:first-child) { justify-self: stretch; text-align: center; }
.metrics-row { border-top: 1px solid var(--line); font-size: 10px; font-variant-numeric: tabular-nums; }
.metrics-row:first-child { border-top: 0; }
.metrics-route { display: flex; align-items: center; min-width: 0; gap: 7px; font-weight: 700; }
.metrics-route i { width: 8px; height: 8px; flex: 0 0 auto; border-radius: 50%; background: var(--route-color); }
.metrics-route span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metrics-value { justify-self: stretch; text-align: center; }

.leaflet-control-zoom { border: 0 !important; box-shadow: var(--shadow) !important; }
.leaflet-control-zoom a { border-color: var(--line) !important; color: var(--ink) !important; }
.leaflet-control-attribution { font-size: 9px !important; }
.leaflet-div-icon { background: transparent; border: 0; }
.endpoint-marker { width: 32px; height: 40px; display: flex; justify-content: center; align-items: flex-start; padding-top: 6px; color: white; font-size: 12px; font-weight: 800; filter: drop-shadow(0 3px 4px rgba(20,30,25,.25)); }
.endpoint-marker::before { content: ""; position: absolute; width: 29px; height: 29px; border-radius: 50% 50% 50% 8px; transform: rotate(-45deg); background: var(--route-color, var(--green)); border: 3px solid white; z-index: -1; }
.endpoint-marker.b { opacity: .9; }
.waypoint-marker { width: 25px; height: 25px; display: grid; place-items: center; border: 3px solid white; border-radius: 50%; background: var(--route-color, var(--green)); color: white; font-size: 9px; font-weight: 800; box-shadow: 0 3px 8px rgba(20,30,25,.28); }
.road-device { width: 28px; height: 28px; display: grid; place-items: center; border-radius: 8px; background: white; box-shadow: 0 3px 10px rgba(17,34,26,.25); font-size: 9px; font-weight: 900; cursor: pointer; }
.road-device.stop { color: white; background: var(--red); border-radius: 8px; font-size: 7px; border: 2px solid white; outline: 1px solid var(--red); }
.road-device.yield { color: var(--red); font-size: 25px; line-height: 1; }
.road-device.light { background: #35413c; border: 2px solid white; border-radius: 6px; }
.road-device.light::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #ef5a50; box-shadow: 0 8px #6c7873, 0 -8px #6c7873; }
.road-device.light.green::before { background: #6c7873; box-shadow: 0 8px #38a16e, 0 -8px #6c7873; }
.slow-zone-handle { width: 26px; height: 26px; display: grid; place-items: center; border: 3px solid white; border-radius: 50%; background: var(--orange); color: white; font-size: 17px; font-weight: 800; box-shadow: 0 3px 8px rgba(20,30,25,.25); cursor: grab; }
.slow-zone-handle:active { cursor: grabbing; }
.slow-resize-handle { width: 22px; height: 22px; display: grid; place-items: center; border: 2px solid var(--orange); border-radius: 50%; background: white; color: var(--orange); font-size: 12px; font-weight: 800; box-shadow: 0 3px 8px rgba(20,30,25,.2); cursor: ew-resize; }
.device-popup { min-width: 210px; color: var(--ink); }
.device-popup h3 { margin: 0 0 3px; font-size: 12px; }
.device-popup > p { margin: 0 0 9px; color: var(--muted); font-size: 9px; }
.device-scope-list { display: grid; gap: 6px; margin-bottom: 10px; }
.device-scope-option { display: flex; align-items: center; gap: 7px; font-size: 10px; cursor: pointer; }
.device-scope-option input { margin: 0; accent-color: var(--green); }
.device-scope-option i { width: 8px; height: 8px; border-radius: 50%; background: var(--route-color, var(--green)); }
.device-popup-timing { display: grid; gap: 9px; margin-top: 11px; padding-top: 10px; border-top: 1px solid var(--line); }
.device-popup-timing label { display: grid; grid-template-columns: 1fr auto; gap: 5px 10px; font-size: 10px; }
.device-popup-timing input { grid-column: 1 / -1; width: 100%; accent-color: #46534d; }
.device-popup-actions { display: flex; justify-content: flex-end; border-top: 1px solid var(--line); padding-top: 8px; }
.device-popup-delete { padding: 5px 8px; border: 0; background: transparent; color: var(--red); font-size: 10px; font-weight: 700; cursor: pointer; }
.vehicle-icon { position: relative; transform-origin: center; filter: drop-shadow(0 2px 2px rgba(18,30,24,.25)); }
.vehicle-body { width: 13px; height: 8px; background: var(--route-color, var(--green)); border: 2px solid white; border-radius: 5px; }
.vehicle-body.bus { width: 18px; height: 9px; border-radius: 3px; background: var(--blue); }
.vehicle-body.slowed { background: var(--orange); }

@media (max-width: 760px) {
  html, body { overflow-x: hidden; }
  .app-shell { min-height: 780px; height: auto; }
  .topbar { height: auto; min-height: 66px; padding: 12px 14px; }
  .brand p, .sim-status { display: none; }
  .workspace { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 520px; }
  .control-panel { width: 100%; overflow: visible; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .panel-section { min-width: 0; padding: 16px; }
  .panel-section:last-child { grid-column: 1 / -1; }
  .tool-grid { grid-template-columns: repeat(4, 1fr); }
  .map-tool, input[type="range"] { min-width: 0; }
  .route-metrics { right: 10px; left: 10px; bottom: 20px; width: auto; }
  .legend { display: none; }
  .config-button { padding-inline: 10px; }
  .config-action-text { display: none; }
}

@media (max-width: 500px) {
  .button-secondary { display: none; }
  .control-panel { display: block; }
  .workspace { grid-template-rows: auto 470px; }
  .tool-grid { grid-template-columns: repeat(2, 1fr); }
  .map-message { max-width: 75%; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}
