:root {
  /* Mari dark base */
  --bg: #050507;
  --card-bg: #121215;
  --card-2: #18181c;
  --card-stroke: rgba(255, 255, 255, 0.13);
  --separator: rgba(255, 255, 255, 0.10);
  --text-primary: #ffffff;
  --text-secondary: rgba(255, 255, 255, 0.62);
  --text-tertiary: rgba(255, 255, 255, 0.50);
  --button-fill: rgba(255, 255, 255, 0.06);

  /* Surfact brand */
  --surfact: #1FA377;       /* primary green */
  --surfact-bright: #35FAB8;/* ocean bright  */
  --surfact-deep: #08543C;
  --amber: #f3c766;         /* stop / stationary */
  --purple: #9b7be0;        /* incident / alert */
  --purple-deep: #633493;

  --tint: var(--surfact);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font-family: "Saira", -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", system-ui, sans-serif;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  width: 100%;
  max-width: 480px;
  padding: max(20px, env(safe-area-inset-top)) 16px calc(40px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  position: relative;
}

.app::before {
  content: "";
  position: fixed;
  inset: -10vh;
  background: radial-gradient(circle at center, var(--tint), transparent 65%);
  opacity: 0.12;
  filter: blur(60px);
  z-index: -1;
  pointer-events: none;
  transition: opacity 0.5s ease, background 0.5s ease;
}
.app[data-state="idle"]::before { opacity: 0; }

/* ---------- HEADER ---------- */
.header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 2px;
}
.brand { display: flex; align-items: center; gap: 11px; }
.brand-mark {
  width: 34px; height: 34px;
  border-radius: 10px;
  display: grid; place-items: center;
  color: #04130d;
  background: linear-gradient(150deg, var(--surfact-bright), var(--surfact));
  box-shadow: 0 0 16px rgba(31,163,119,0.35);
}
.brand-text h1 {
  font-size: 19px;
  font-weight: 700;
  letter-spacing: 4px;
  margin: 0;
  line-height: 1;
}
.brand-text p {
  font-size: 8.5px;
  font-weight: 600;
  letter-spacing: 2.5px;
  color: var(--text-tertiary);
  margin: 4px 0 0;
}
.setup-btn {
  width: 38px; height: 38px;
  border-radius: 11px;
  background: var(--button-fill);
  border: 0.5px solid var(--card-stroke);
  color: var(--text-secondary);
  cursor: pointer;
  display: grid; place-items: center;
  transition: background 0.15s ease, color 0.15s ease;
}
.setup-btn:active { background: rgba(255,255,255,0.12); color: #fff; }

/* ---------- DIAL ---------- */
.dial-wrap { width: 100%; display: flex; justify-content: center; }
.dial { position: relative; width: min(74vw, 280px); aspect-ratio: 1/1; }
.ring { width: 100%; height: 100%; transform: rotate(-90deg); display: block; }
.ring .track { fill: none; stroke: var(--card-stroke); stroke-width: 0.6; }
.ring .progress {
  fill: none; stroke: var(--tint); stroke-width: 1;
  stroke-linecap: round;
  stroke-dasharray: 289.03; stroke-dashoffset: 289.03;
  transition: stroke-dashoffset 0.5s linear, stroke 0.5s ease;
}
.dial-content {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  gap: 14px; padding: 26px; text-align: center;
}
.phase-label {
  font-size: 12px; font-weight: 600; letter-spacing: 4px;
  color: var(--tint); opacity: 0.9;
  transition: color 0.4s ease;
}
.dial-wrap[data-phase="idle"] .phase-label { color: var(--text-tertiary); }
.time-display {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 46px; font-weight: 200; letter-spacing: 1px;
  font-variant-numeric: tabular-nums; line-height: 1;
}
.dial-wrap[data-phase="idle"] .time-display { color: var(--text-tertiary); }

.gps-chip {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 10.5px; font-weight: 600; letter-spacing: 0.5px;
  color: var(--text-secondary);
  padding: 4px 10px; border-radius: 999px;
  background: var(--button-fill); border: 0.5px solid var(--separator);
  font-variant-numeric: tabular-nums;
}
.gps-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-tertiary); }
.gps-chip[data-fix="good"]  .gps-dot { background: var(--surfact-bright); box-shadow: 0 0 7px var(--surfact-bright); }
.gps-chip[data-fix="ok"]    .gps-dot { background: var(--amber); }
.gps-chip[data-fix="poor"]  .gps-dot { background: var(--purple); }
.gps-chip[data-fix="error"] .gps-dot { background: #ff6b6b; }
.gps-chip[data-fix="searching"] .gps-dot { background: var(--amber); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.25} }

/* ---------- CONTROLS ---------- */
.controls { display: flex; flex-direction: column; align-items: center; gap: 12px; margin-top: -4px; }
.play-btn {
  width: 76px; height: 76px; border-radius: 50%;
  background: #fff; color: #000; border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 22px rgba(255,255,255,0.16), 0 6px 10px rgba(0,0,0,0.5);
  transition: transform 0.18s cubic-bezier(0.34,1.56,0.64,1), background 0.3s ease;
}
.play-btn:active { transform: scale(0.94); }
.play-btn svg { width: 28px; height: 28px; }
.play-icon { transform: translateX(2px); }
/* state-driven icon swap (robust against UA/library display rules) */
.play-btn .play-icon { display: block; }
.play-btn .stop-icon { display: none; }
.app[data-state="recording"] .play-btn .play-icon { display: none; }
.app[data-state="recording"] .play-btn .stop-icon { display: block; }
.app[data-state="recording"] .play-btn {
  background: linear-gradient(150deg, var(--surfact-bright), var(--surfact));
  color: #04130d;
  box-shadow: 0 0 26px rgba(31,163,119,0.45), 0 6px 10px rgba(0,0,0,0.5);
}
.summary { display: flex; align-items: center; gap: 12px; font-size: 12.5px; color: var(--text-secondary); font-weight: 500; font-variant-numeric: tabular-nums; }
.vsep { width: 1px; height: 12px; background: var(--separator); display: inline-block; }

/* ---------- EVENT BUTTONS ---------- */
.event-bar { width: 100%; display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.event-btn {
  background: var(--card-bg); border: 0.5px solid var(--card-stroke);
  border-radius: 18px; padding: 14px 8px 12px;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  cursor: pointer; color: var(--text-primary);
  font-family: inherit; transition: transform 0.12s ease, border-color 0.2s ease, opacity 0.2s ease;
}
.event-btn:active:not(:disabled) { transform: scale(0.96); }
.event-btn:disabled { opacity: 0.35; cursor: default; }
.event-btn .ico {
  width: 34px; height: 34px; border-radius: 11px; display: grid; place-items: center;
  font-size: 15px; font-weight: 700; line-height: 1;
}
.event-btn .lbl { font-size: 13px; font-weight: 700; letter-spacing: 1.5px; }
.event-btn .sub { font-size: 9px; color: var(--text-tertiary); letter-spacing: 0.5px; }
.event-btn.stop .ico     { background: rgba(243,199,102,0.16); color: var(--amber); }
.event-btn.start .ico    { background: rgba(53,250,184,0.16); color: var(--surfact-bright); }
.event-btn.incident .ico { background: rgba(155,123,224,0.18); color: var(--purple); }
.event-btn.stop:active:not(:disabled)     { border-color: var(--amber); }
.event-btn.start:active:not(:disabled)    { border-color: var(--surfact-bright); }
.event-btn.incident:active:not(:disabled) { border-color: var(--purple); }

/* ---------- SEGMENTED ---------- */
.segmented {
  width: 100%; display: flex; gap: 4px; padding: 4px;
  background: var(--card-bg); border: 0.5px solid var(--card-stroke);
  border-radius: 13px;
}
.seg {
  flex: 1; padding: 9px 0; border: none; background: transparent; cursor: pointer;
  color: var(--text-secondary); font-family: inherit; font-size: 11px; font-weight: 700; letter-spacing: 2px;
  border-radius: 9px; transition: background 0.2s ease, color 0.2s ease;
}
.seg.active { background: var(--button-fill); color: #fff; }

/* ---------- VIEWS ---------- */
.view { width: 100%; }
.view:not(.active) { display: none; }
.view.active { display: block; }

/* timeline */
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.empty { font-size: 12.5px; color: var(--text-secondary); text-align: center; line-height: 1.6; padding: 22px 18px; margin: 0; }
.tl-item {
  display: grid; grid-template-columns: 14px 1fr; gap: 12px;
  padding: 12px 4px; position: relative;
}
.tl-item::before {
  content: ""; position: absolute; left: 6px; top: 26px; bottom: -12px; width: 1.5px; background: var(--separator);
}
.tl-item:last-child::before { display: none; }
.tl-node {
  width: 14px; height: 14px; border-radius: 50%; margin-top: 5px;
  border: 2.5px solid var(--bg);
}
.tl-item[data-type="stop"]     .tl-node { background: var(--amber); }
.tl-item[data-type="start"]    .tl-node { background: var(--surfact-bright); }
.tl-item[data-type="incident"] .tl-node { background: var(--purple); }
.tl-body { min-width: 0; }
.tl-top { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.tl-type { font-size: 12px; font-weight: 700; letter-spacing: 1.5px; }
.tl-item[data-type="stop"]     .tl-type { color: var(--amber); }
.tl-item[data-type="start"]    .tl-type { color: var(--surfact-bright); }
.tl-item[data-type="incident"] .tl-type { color: var(--purple); }
.tl-time { font-family: ui-monospace, Menlo, monospace; font-size: 12px; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.tl-elapsed { font-size: 10px; color: var(--text-tertiary); margin-left: auto; font-variant-numeric: tabular-nums; }
.tl-note { font-size: 13px; color: var(--text-primary); margin: 3px 0 0; line-height: 1.4; word-break: break-word; }
.tl-coords {
  font-family: ui-monospace, Menlo, monospace; font-size: 10.5px; color: var(--text-tertiary);
  margin-top: 4px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; cursor: pointer;
}
.tl-coords .acc { color: var(--text-tertiary); }
.tl-del {
  background: none; border: none; color: var(--text-tertiary); cursor: pointer;
  font-size: 18px; padding: 8px 10px; margin: -8px -6px -8px 0; line-height: 1; /* ~40px tap target */
}
.tl-del:active { color: #ff6b6b; }

/* map */
#map { width: 100%; height: 56vh; min-height: 320px; border-radius: 18px; overflow: hidden; border: 0.5px solid var(--card-stroke); background: #0c0c0f; }
.view-map { position: relative; }
.follow-btn {
  position: absolute; right: 14px; bottom: 14px; z-index: 500;
  width: 42px; height: 42px; border-radius: 12px;
  background: rgba(10,10,12,0.85); backdrop-filter: blur(8px);
  border: 0.5px solid var(--card-stroke); color: var(--text-secondary);
  font-size: 18px; cursor: pointer;
}
.follow-btn[aria-pressed="true"] { color: var(--surfact-bright); border-color: var(--surfact); }
.leaflet-container { background: #0c0c0f; font-family: inherit; }
.leaflet-control-attribution { background: rgba(0,0,0,0.5) !important; color: var(--text-tertiary) !important; }
.leaflet-control-attribution a { color: var(--text-secondary) !important; }

/* ---------- EXPORT ---------- */
.export-bar { width: 100%; display: flex; gap: 8px; }
.exp {
  flex: 1; padding: 12px 4px; border-radius: 13px;
  background: var(--card-bg); border: 0.5px solid var(--card-stroke);
  color: var(--text-primary); font-family: inherit; font-size: 12px; font-weight: 600; letter-spacing: 0.5px;
  cursor: pointer; transition: background 0.15s ease, border-color 0.15s ease;
}
.exp[data-export="gpx"] { flex: 1.6; background: linear-gradient(150deg, rgba(53,250,184,0.16), rgba(31,163,119,0.12)); border-color: rgba(31,163,119,0.5); color: var(--surfact-bright); }
.exp:active { background: rgba(255,255,255,0.1); }
.exp.danger { flex: 0.8; color: var(--text-tertiary); }
.exp.danger:active { color: #ff6b6b; border-color: #ff6b6b; }

.bg-warning {
  font-size: 11px; color: var(--text-secondary); text-align: center;
  max-width: 380px; line-height: 1.55; margin: 0; padding: 0 6px;
}

/* ---------- SHEETS ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,0.6); backdrop-filter: blur(4px);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%; max-width: 480px;
  background: var(--card-bg); border-top: 0.5px solid var(--card-stroke);
  border-radius: 22px 22px 0 0;
  padding: 18px 18px calc(22px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 12px;
  animation: sheetUp 0.25s cubic-bezier(0.2,0.8,0.2,1);
}
@keyframes sheetUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; }
.sheet-head strong { font-size: 16px; font-weight: 700; letter-spacing: 0.5px; }
.sheet-close { background: none; border: none; color: var(--surfact-bright); font-family: inherit; font-size: 14px; font-weight: 600; cursor: pointer; }
.sheet-note { font-size: 11.5px; color: var(--text-tertiary); margin: 0; line-height: 1.5; }
.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  background: var(--button-fill); border: 0.5px solid var(--card-stroke); color: var(--text-secondary);
  border-radius: 999px; padding: 7px 13px; font-size: 12px; font-family: inherit; cursor: pointer; font-weight: 500;
}
.chip:active, .chip.on { background: rgba(155,123,224,0.2); border-color: var(--purple); color: #fff; }
#incidentText, .field input {
  width: 100%; background: var(--card-2); border: 0.5px solid var(--card-stroke);
  border-radius: 13px; color: #fff; font-family: inherit; font-size: 15px; padding: 13px 14px;
  resize: vertical; -webkit-user-select: text; user-select: text;
}
#incidentText:focus, .field input:focus { outline: none; border-color: var(--surfact); }
.incident-loc { font-family: ui-monospace, Menlo, monospace; font-size: 11px; color: var(--text-tertiary); padding: 0 2px; }
.sheet-save {
  width: 100%; padding: 15px; border: none; border-radius: 14px;
  background: linear-gradient(150deg, var(--surfact-bright), var(--surfact)); color: #04130d;
  font-family: inherit; font-size: 15px; font-weight: 700; letter-spacing: 0.5px; cursor: pointer;
}
.sheet-save:active { filter: brightness(0.95); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field span { font-size: 11px; color: var(--text-secondary); font-weight: 600; letter-spacing: 0.5px; padding-left: 2px; }

/* ---------- TOAST ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: rgba(20,20,24,0.96); border: 0.5px solid var(--card-stroke);
  color: #fff; padding: 12px 18px; border-radius: 13px; font-size: 13px; z-index: 2000;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5); max-width: 88vw; text-align: center;
  animation: toastIn 0.2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* dial tints by state */
.app[data-state="recording"] { --tint: var(--surfact); }
.app[data-state="ended"]     { --tint: var(--purple); }
.dial-wrap[data-phase="recording"] { --tint: var(--surfact); }
.dial-wrap[data-phase="ended"]     { --tint: var(--purple); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01s !important; animation-duration: 0.01s !important; }
}
