:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --cheap: #16a34a;
  --near: #2563eb;
  --value: #9333ea;
  --warn: #b45309;
  --error: #dc2626;
  --radius: 12px;
}

* { box-sizing: border-box; }

/* The [hidden] attribute is only `display: none` in the UA stylesheet, so any
 * author rule that sets `display` (e.g. `.trip-active { display: flex }`) wins
 * and the element stays visible. Enforce it once, globally. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---- header ---- */

.bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 14px 10px;
  background: var(--ink); color: #fff;
  position: sticky; top: 0; z-index: 1000;
}
/* "Onde Atestar .pt" is too long for one line at a normal logo size, so it's
   stacked into two short lines instead — name on top, .pt underneath — which
   stays compact enough to sit on the left with the fuel selector on the right,
   on one row, at any screen width. */
.bar h1 {
  margin: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  white-space: nowrap;
}
.bar h1 .logo-name { font-size: 14px; font-weight: 700; letter-spacing: -0.01em; }
.bar h1 .tld { font-size: 12px; color: #94a3b8; font-weight: 500; }
.bar-controls { display: flex; gap: 8px; align-items: center; }

.bar select {
  font-size: 15px; padding: 8px 10px;
  border-radius: 9px; border: 1px solid #334155;
  background: #1e293b; color: #fff;
  max-width: 52vw;
}
.bar button {
  font-size: 17px; line-height: 1;
  width: 38px; height: 38px;
  border-radius: 9px; border: 1px solid #334155;
  background: #1e293b; color: #fff; cursor: pointer;
}
.bar button:disabled { opacity: 0.5; cursor: default; }

/* ---- status + manual ---- */

.status {
  padding: 9px 14px; font-size: 14px;
  color: var(--muted); background: var(--card);
  border-bottom: 1px solid var(--line);
}
.status-busy { color: var(--near); }
.status-warn { color: var(--warn); background: #fffbeb; }
.status-error { color: var(--error); background: #fef2f2; }

.manual {
  padding: 12px 14px; background: #fffbeb;
  border-bottom: 1px solid var(--line);
}
.manual label { font-size: 13px; font-weight: 600; display: block; margin-bottom: 6px; }
.manual-row { display: flex; gap: 8px; }
.manual input {
  flex: 1; min-width: 0;
  padding: 10px 12px; font-size: 16px;
  border: 1px solid #d6d3d1; border-radius: 9px; background: #fff;
}
.manual button {
  padding: 10px 16px; font-size: 15px; font-weight: 600;
  border: 0; border-radius: 9px; background: var(--ink); color: #fff; cursor: pointer;
}
.manual-hint { margin: 8px 0 0; font-size: 12.5px; color: var(--muted); }

/* ---- trip mode ---- */

.trip { border-bottom: 1px solid var(--line); background: var(--card); }

.trip-toggle {
  width: 100%; text-align: left;
  padding: 11px 14px; font-size: 14.5px; font-weight: 600;
  border: 0; background: none; color: var(--ink); cursor: pointer;
}
.trip-caret { color: var(--muted); font-size: 12px; }

.trip-form { padding: 0 14px 14px; }
.trip-form label { font-size: 13px; font-weight: 600; display: block; margin-bottom: 6px; }
.trip-row { display: flex; gap: 8px; }
.trip-row input {
  flex: 1; min-width: 0;
  padding: 10px 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 9px; background: #fff;
}
.trip-row button {
  padding: 10px 16px; font-size: 15px; font-weight: 600;
  border: 0; border-radius: 9px; background: var(--ink); color: #fff; cursor: pointer;
  white-space: nowrap;
}
.trip-hint { margin: 8px 0 0; font-size: 12.5px; color: var(--muted); }

.trip-active {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 11px 14px; background: #eff6ff;
  font-size: 14.5px; font-weight: 600;
}
.trip-active button {
  border: 0; background: none; color: var(--near);
  font-size: 13px; font-weight: 600; cursor: pointer; white-space: nowrap;
}

.row-along { font-size: 12px; color: var(--muted); margin-top: 3px; }
.on-route { color: var(--cheap); font-weight: 600; }

/* ---- layout ---- */

.layout { display: block; }
#map { height: 46vh; min-height: 260px; width: 100%; background: #dbeafe; z-index: 1; }

.panel { background: var(--card); border-top: 1px solid var(--line); }
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; flex-wrap: wrap;
  padding: 10px 14px; border-bottom: 1px solid var(--line);
  position: sticky; top: 0; background: var(--card); z-index: 2;
}
.legend { display: flex; gap: 6px; flex-wrap: wrap; }

.sortbar { display: flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.sort {
  padding: 7px 13px; font-size: 14px; border: 0;
  background: #fff; color: var(--muted); cursor: pointer;
}
.sort.active { background: var(--ink); color: #fff; font-weight: 600; }

/* ---- chips ---- */

.chip {
  display: inline-block; font-size: 11px; font-weight: 700;
  padding: 3px 7px; border-radius: 999px;
  color: #fff; white-space: nowrap;
}
.chip-cheap { background: var(--cheap); }
.chip-near  { background: var(--near); }

/* Legend chips are <button>s (list-row badges stay plain <span>s and are
   untouched) -- reset UA button chrome so they still look like pills, and
   give them the same tap feedback as a starred marker on the map. */
#legend .chip {
  border: 0; font: inherit; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
#legend .chip:hover:not(:disabled) { filter: brightness(1.12); }
#legend .chip:active:not(:disabled) { transform: scale(0.96); }
#legend .chip:disabled { cursor: default; opacity: 0.5; }
#legend .chip:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.chip-value { background: var(--value); }

/* ---- list ---- */

.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; gap: 12px; align-items: flex-start; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.row:active { background: #f8fafc; }
.row:focus-visible { outline: 2px solid var(--near); outline-offset: -2px; }
.row-hl { background: #f8fafc; box-shadow: inset 3px 0 0 var(--ink); }

/* The pinned recommendation is lifted out of sort order, so it needs to look
   deliberate — otherwise it reads as a broken sort sitting above a cheaper row. */
.row-pinned {
  background: #faf5ff;
  box-shadow: inset 4px 0 0 var(--value);
  border-bottom: 2px solid var(--line);
}

.row-main { min-width: 0; flex: 1; }
.row-name {
  font-weight: 600; font-size: 15px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.row-sub {
  font-size: 13px; color: var(--muted); margin-top: 2px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.row-badges { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.row-badges:empty { display: none; }

.row-right { text-align: right; flex-shrink: 0; }
.row-price { font-size: 17px; font-weight: 700; font-variant-numeric: tabular-nums; }
.row-dist { font-size: 13px; color: var(--muted); margin-top: 2px; }
.row-age { font-size: 11.5px; margin-top: 3px; }
.fresh { color: #94a3b8; }
.stale { color: var(--warn); font-weight: 600; }

.empty { padding: 26px 14px; text-align: center; color: var(--muted); }

/* ---- map pins ---- */

.pin {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%; border-radius: 50%;
  font-size: 13px; font-weight: 700; color: #fff;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  background: #94a3b8;
}
.pin-plain {
  background: #64748b;
  font-size: 10px;
  border-width: 1.5px;
  opacity: 0.82;
}
.pin-cheap { background: var(--cheap); }
.pin-near  { background: var(--near); }
.pin-value { background: var(--value); }

/* A station that is both cheapest and closest gets a split marker rather than
   two overlapping pins. */
.pin-cheap.pin-near  { background: linear-gradient(135deg, var(--cheap) 50%, var(--near) 50%); }
.pin-cheap.pin-value { background: linear-gradient(135deg, var(--cheap) 50%, var(--value) 50%); }
.pin-near.pin-value  { background: linear-gradient(135deg, var(--near) 50%, var(--value) 50%); }
.pin-cheap.pin-near.pin-value {
  background: conic-gradient(var(--cheap) 0 33%, var(--near) 0 66%, var(--value) 0);
}

/* ---- detail sheet ---- */

.scrim {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); z-index: 1500;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1600;
  background: var(--card);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -6px 28px rgba(0, 0, 0, 0.25);
  max-height: 82vh; overflow-y: auto;
}
.sheet-inner { padding: 20px 18px calc(20px + env(safe-area-inset-bottom)); position: relative; }
.sheet-close {
  position: absolute; top: 10px; right: 12px;
  width: 34px; height: 34px; font-size: 22px; line-height: 1;
  border: 0; border-radius: 50%; background: #f1f5f9; color: var(--muted); cursor: pointer;
}
.sheet h2 { margin: 0 40px 4px 0; font-size: 18px; }
.sheet-brand { margin: 0 0 10px; color: var(--muted); font-size: 14px; }
.sheet-price { margin: 0 0 10px; font-size: 21px; font-weight: 700; }
.sheet-addr { margin: 0 0 6px; font-size: 15px; }
.sheet-meta { margin: 0 0 16px; font-size: 13px; color: var(--muted); }

.sheet-actions { display: flex; gap: 10px; }
.sheet-actions a {
  flex: 1; text-align: center; padding: 13px 10px;
  border-radius: 10px; background: var(--ink); color: #fff;
  text-decoration: none; font-weight: 600; font-size: 15px;
}
.sheet-actions a:last-child { background: #475569; }

/* ---- footer ---- */

.foot {
  padding: 16px 14px calc(20px + env(safe-area-inset-bottom));
  font-size: 12px; color: var(--muted); text-align: center;
}
.foot a { color: var(--muted); }

/* ---- wide screens: map beside list ---- */

@media (min-width: 900px) {
  .layout {
    display: grid;
    grid-template-columns: 1fr 420px;
    height: calc(100vh - 108px);
  }
  #map { height: 100%; }
  .panel { overflow-y: auto; border-top: 0; border-left: 1px solid var(--line); }
}
