:root {
  --bg: #f6f7f4;
  --surface: #ffffff;
  --text: #1b1c19;
  --muted: #5f6359;
  --line: #e2e4dc;
  --primary: #2e7d32;
  --on-primary: #ffffff;
  --tilling: #2e7d32;
  --moving: #1e6fd9;
  --idle: #e09b00;
  --offline: #8a8d86;
  --error: #c62828;
  --shadow: 0 1px 3px rgb(0 0 0 / .12), 0 4px 16px rgb(0 0 0 / .08);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121411;
    --surface: #1c1f1a;
    --text: #e3e4dd;
    --muted: #a3a79c;
    --line: #2f332c;
    --primary: #7cc47f;
    --on-primary: #0d2b0f;
    --tilling: #6cc070;
    --moving: #6aa8ff;
    --idle: #f2b632;
    --offline: #8a8d86;
    --error: #ff8a80;
    --shadow: 0 1px 3px rgb(0 0 0 / .5), 0 4px 16px rgb(0 0 0 / .4);
    color-scheme: dark;
  }
  .leaflet-tile-pane { filter: brightness(.75) contrast(1.1) saturate(.8); }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; font-weight: 600; }
h1 { font-size: 18px; }
h2 { font-size: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.pad { padding: 0 16px; }
.error { color: var(--error); min-height: 1.4em; margin: 0; font-size: 13px; }

.icon {
  font-family: "Material Symbols Outlined";
  font-weight: normal; font-style: normal;
  font-size: 22px; line-height: 1;
  display: inline-block; white-space: nowrap; direction: ltr;
  -webkit-font-smoothing: antialiased;
  user-select: none;
}

/* Buttons & fields */
.btn {
  border: 0; border-radius: 999px; padding: 10px 20px;
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--primary); color: var(--on-primary); }
.icon-btn {
  border: 0; background: transparent; color: inherit; cursor: pointer;
  width: 40px; height: 40px; border-radius: 50%;
  display: inline-grid; place-items: center; flex: none;
}
.icon-btn:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.icon-btn.filled { background: var(--primary); color: var(--on-primary); }
.field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.field input {
  font: inherit; font-size: 15px; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
}
.field input:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.field.inline { display: flex; align-items: center; gap: 6px; }
.field.inline input { padding: 6px 8px; min-width: 0; }
#width { width: 72px; }
.toggle {
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
  border: 1px solid var(--line); background: transparent; color: var(--muted);
  border-radius: 999px; padding: 4px 12px;
}
.toggle[aria-pressed="true"] { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.detail-controls { align-items: center; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 28px 24px; width: min(360px, 100%); display: grid; gap: 14px; text-align: center;
}
.login-card .field { text-align: left; }
.login-logo { font-size: 48px; color: var(--primary); justify-self: center; }

/* App layout */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 8px 8px 16px; padding-top: max(8px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line); z-index: 1000;
}
.topbar > .icon { color: var(--primary); }
.topbar h1 { flex: 1; }
.layout { flex: 1; position: relative; display: flex; min-height: 0; }
.map { flex: 1; min-height: 0; background: var(--line); }

/* Panel: side panel on desktop, bottom sheet on mobile */
.panel {
  width: 380px; flex: none; overflow-y: auto;
  background: var(--surface); border-left: 1px solid var(--line);
  padding-bottom: 16px;
}
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .panel {
    width: auto; max-height: 48vh; border-left: 0;
    border-radius: var(--radius) var(--radius) 0 0;
    box-shadow: var(--shadow); margin-top: -14px; z-index: 500; position: relative;
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .panel::before {
    content: ""; display: block; width: 36px; height: 4px; border-radius: 2px;
    background: var(--line); margin: 8px auto 0;
  }
}
.panel-head {
  display: flex; align-items: center; gap: 8px; padding: 12px 16px; flex-wrap: wrap;
  position: sticky; top: 0; background: var(--surface); z-index: 1;
}
.panel-head h2 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend { width: 100%; font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.legend .dot { margin-left: 6px; }
.legend .dot:first-child { margin-left: 0; }

.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.dot.tilling, .chip.tilling { background: var(--tilling); }
.dot.moving, .chip.moving { background: var(--moving); }
.dot.idle, .chip.idle { background: var(--idle); }
.dot.offline, .chip.offline { background: var(--offline); }
.chip {
  color: #fff; font-size: 12px; font-weight: 600; padding: 2px 10px; border-radius: 999px;
  text-transform: capitalize; flex: none;
}
.chip.idle { color: #2b1d00; }

/* Fleet list */
.fleet { list-style: none; margin: 0; padding: 0 8px; }
.fleet li {
  display: grid; grid-template-columns: 1fr auto; gap: 4px 8px;
  padding: 12px; border-radius: 12px; cursor: pointer;
}
.fleet li:hover, .fleet li:focus-visible { background: color-mix(in srgb, var(--text) 6%, transparent); outline: none; }
.fleet .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fleet .meta { grid-column: 1 / -1; display: flex; gap: 14px; flex-wrap: wrap; color: var(--muted); font-size: 13px; }
.fleet .meta span, .stats .stat { display: inline-flex; align-items: center; gap: 4px; }
.fleet .meta .icon { font-size: 18px; }

/* Detail */
.detail-controls { display: flex; gap: 12px; padding: 0 16px 8px; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); gap: 8px; padding: 8px 16px; }
.stats .stat {
  flex-direction: column; align-items: flex-start; gap: 2px;
  background: var(--bg); border-radius: 12px; padding: 10px 12px;
}
.stats .stat .icon { font-size: 18px; color: var(--muted); }
.stats .value { font-size: 20px; font-weight: 600; }
.stats .label { font-size: 12px; color: var(--muted); }
.replay { display: flex; align-items: center; gap: 10px; padding: 8px 16px; }
.replay input[type=range] { flex: 1; accent-color: var(--primary); }

/* Map markers */
.tractor-marker {
  width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; border: 2px solid #fff; box-shadow: 0 1px 4px rgb(0 0 0 / .4);
}
.tractor-marker .icon { font-size: 18px; }
.tractor-marker.tilling { background: var(--tilling); }
.tractor-marker.moving { background: var(--moving); }
.tractor-marker.idle { background: var(--idle); }
.tractor-marker.offline { background: var(--offline); }
.tractor-marker.replay { background: var(--text); color: var(--bg); }
.leaflet-tooltip.tractor-label { font-weight: 600; }
