:root {
  --forest: #12372a;
  --forest-2: #1c4b3a;
  --lime: #c9f27a;
  --cream: #f6f4ed;
  --paper: #fffef9;
  --ink: #16201b;
  --muted: #728078;
  --line: #dde3dc;
  --danger: #c8483d;
  --shadow: 0 14px 35px rgba(18, 55, 42, .13);
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic UI", sans-serif; color: var(--ink); background: #dfe5df; }
button, input, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell { max-width: 560px; min-height: 100dvh; margin: auto; background: var(--cream); position: relative; overflow: hidden; box-shadow: 0 0 60px rgba(0,0,0,.12); }
.topbar { height: 74px; padding: 13px 18px; display: flex; justify-content: space-between; align-items: center; background: var(--paper); border-bottom: 1px solid var(--line); position: relative; z-index: 500; }
.brand { display: flex; align-items: center; gap: 11px; }
.brand-mark { width: 42px; height: 42px; border-radius: 13px; display: grid; place-items: center; background: var(--forest); color: var(--lime); font-weight: 900; font-size: 21px; transform: rotate(-3deg); }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: .06em; }
.brand p { font-size: 11px; color: var(--muted); margin: 3px 0 0; }
.icon-button { width: 40px; height: 40px; border: 0; border-radius: 12px; background: #eef1ec; color: var(--forest); font-weight: 900; letter-spacing: 2px; }

main { min-height: calc(100dvh - 146px); }
.view { display: none; padding-bottom: 90px; min-height: calc(100dvh - 74px); }
.view.active { display: block; }
.status-strip { height: 34px; background: var(--forest); color: white; display: flex; align-items: center; gap: 7px; padding: 0 15px; font-size: 11px; position: relative; z-index: 450; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #eebf4a; box-shadow: 0 0 0 4px rgba(238,191,74,.14); }
.status-dot.ready { background: var(--lime); box-shadow: 0 0 0 4px rgba(201,242,122,.14); }
.status-dot.recording { background: #ff6b5f; animation: pulse 1.5s infinite; }
.status-time { margin-left: auto; opacity: .75; }
@keyframes pulse { 50% { box-shadow: 0 0 0 7px rgba(255,107,95,.05); } }

#map { height: 39dvh; min-height: 285px; background: #e6e4d9; z-index: 1; }
.leaflet-control-attribution { font-size: 8px !important; }
.route-marker { background: var(--forest); border: 3px solid white; border-radius: 50%; box-shadow: 0 3px 10px rgba(0,0,0,.3); }
.event-map-marker { display: grid; place-items: center; width: 30px !important; height: 30px !important; background: var(--paper); border: 2px solid var(--forest); border-radius: 9px; font-size: 15px; box-shadow: 0 4px 12px rgba(0,0,0,.2); }
.map-actions { position: absolute; z-index: 400; top: calc(74px + 34px + 39dvh - 108px); right: 13px; display: grid; gap: 8px; }
.round-button { width: 42px; height: 42px; border-radius: 14px; border: 1px solid rgba(18,55,42,.1); background: rgba(255,254,249,.95); color: var(--forest); box-shadow: 0 5px 16px rgba(18,55,42,.15); font-size: 19px; font-weight: 800; }

.drive-panel { position: relative; z-index: 10; margin-top: -18px; border-radius: 22px 22px 0 0; background: var(--paper); padding: 20px 18px 24px; min-height: 260px; box-shadow: 0 -8px 26px rgba(18,55,42,.08); }
.recording-heading { display: flex; align-items: end; justify-content: space-between; }
.eyebrow { display: block; font-size: 10px; color: var(--muted); font-weight: 700; letter-spacing: .08em; text-transform: uppercase; margin-bottom: 4px; }
.recording-heading strong { font-size: 28px; letter-spacing: .03em; }
.recording-heading .distance strong { font-size: 36px; color: var(--forest); }
.recording-heading .distance span { font-weight: 700; color: var(--muted); }
.metric-row { display: grid; grid-template-columns: repeat(3,1fr); gap: 8px; margin: 15px 0; padding: 12px 0; border-block: 1px solid var(--line); }
.metric-row div { text-align: center; border-right: 1px solid var(--line); }
.metric-row div:last-child { border: 0; }
.metric-row span { display: block; color: var(--muted); font-size: 10px; margin-bottom: 4px; }
.metric-row strong { font-size: 12px; }
.record-button { width: 100%; height: 58px; border: 0; border-radius: 17px; background: var(--forest); color: white; font-weight: 800; display: flex; align-items: center; justify-content: center; gap: 10px; box-shadow: 0 8px 20px rgba(18,55,42,.22); }
.record-button.recording { background: #f2e6e2; color: var(--danger); box-shadow: none; }
.record-icon { width: 15px; height: 15px; background: #ff6b5f; border-radius: 50%; box-shadow: 0 0 0 5px rgba(255,107,95,.18); }
.recording .record-icon { border-radius: 3px; background: var(--danger); box-shadow: none; }
.event-controls { display: grid; grid-template-columns: repeat(4,1fr); gap: 8px; margin-top: 12px; }
.event-controls button { background: #eef2eb; border: 0; border-radius: 13px; padding: 9px 2px; font-size: 10px; font-weight: 700; color: var(--forest); }
.event-controls span { display: block; font-size: 19px; margin-bottom: 4px; }
.hidden { display: none !important; }

.bottom-nav { position: fixed; bottom: 0; left: 50%; transform: translateX(-50%); width: min(560px, 100%); height: calc(72px + env(safe-area-inset-bottom)); padding: 7px 20px env(safe-area-inset-bottom); background: rgba(255,254,249,.96); backdrop-filter: blur(16px); display: grid; grid-template-columns: repeat(3,1fr); border-top: 1px solid var(--line); z-index: 1000; }
.bottom-nav button { border: 0; background: none; color: #89938d; font-size: 10px; font-weight: 700; }
.bottom-nav span { display: block; font-size: 21px; margin-bottom: 3px; }
.bottom-nav button.active { color: var(--forest); }

#historyView, #statsView { padding: 25px 18px 100px; }
.section-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.section-title h2 { margin: 0; font-size: 27px; }
.text-button { color: var(--forest); border: 0; background: #e3e9e2; border-radius: 10px; padding: 8px 11px; font-size: 11px; font-weight: 700; }
.summary-card { border-radius: 21px; background: var(--forest); color: white; padding: 20px; margin-bottom: 18px; box-shadow: var(--shadow); }
.summary-card > span { font-size: 11px; opacity: .65; }
.summary-card > strong { display: block; font-size: 38px; color: var(--lime); margin: 5px 0 10px; }
.summary-card small { font-size: 14px; color: white; }
.summary-card div { display: flex; justify-content: space-between; border-top: 1px solid rgba(255,255,255,.14); padding-top: 10px; font-size: 11px; opacity: .8; }
.history-list { display: grid; gap: 10px; }
.history-item { border: 1px solid var(--line); background: var(--paper); border-radius: 17px; padding: 15px; display: grid; grid-template-columns: 46px 1fr auto; gap: 12px; align-items: center; text-align: left; width: 100%; color: var(--ink); }
.history-date { width: 46px; height: 50px; border-radius: 12px; background: #e9eee7; display: grid; place-items: center; align-content: center; color: var(--forest); line-height: 1; }
.history-date strong { font-size: 19px; }
.history-date small { font-size: 9px; margin-top: 4px; }
.history-main strong { display: block; font-size: 14px; margin-bottom: 5px; }
.history-main span { color: var(--muted); font-size: 10px; }
.history-distance { font-size: 15px; font-weight: 800; color: var(--forest); }
.empty-state { text-align: center; padding: 45px 20px; color: var(--muted); background: var(--paper); border: 1px dashed #cbd4cb; border-radius: 18px; }
.empty-state strong { display: block; color: var(--ink); margin-bottom: 7px; }

.period-tabs { background: #e4e9e2; border-radius: 13px; padding: 4px; display: grid; grid-template-columns: repeat(3,1fr); margin-bottom: 16px; }
.period-tabs button { border: 0; background: none; padding: 8px; border-radius: 10px; color: var(--muted); font-size: 11px; font-weight: 700; }
.period-tabs button.active { background: var(--paper); color: var(--forest); box-shadow: 0 2px 8px rgba(18,55,42,.08); }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stats-grid article, .chart-card { background: var(--paper); border: 1px solid var(--line); border-radius: 17px; padding: 16px; }
.stats-grid span { display: block; color: var(--muted); font-size: 10px; }
.stats-grid strong { font-size: 28px; color: var(--forest); margin-right: 4px; }
.stats-grid small { color: var(--muted); font-size: 10px; }
.chart-card { margin-top: 10px; }
.chart-card h3 { font-size: 13px; margin: 0 0 17px; }
.bar-chart { height: 130px; display: flex; align-items: flex-end; gap: 7px; border-bottom: 1px solid var(--line); }
.bar-column { flex: 1; display: flex; height: 100%; align-items: flex-end; position: relative; }
.bar { width: 100%; min-height: 3px; background: var(--forest); border-radius: 5px 5px 0 0; position: relative; }
.bar-column label { position: absolute; bottom: -19px; width: 100%; text-align: center; font-size: 8px; color: var(--muted); }
.bar-chart { margin-bottom: 18px; }
.event-breakdown { display: grid; gap: 11px; }
.breakdown-row { display: grid; grid-template-columns: 54px 1fr 28px; gap: 8px; align-items: center; font-size: 10px; }
.breakdown-track { height: 7px; border-radius: 10px; background: #e9eee7; overflow: hidden; }
.breakdown-fill { height: 100%; background: var(--lime); border-radius: 10px; }

dialog { width: min(520px, calc(100% - 24px)); border: 0; border-radius: 22px; padding: 20px; color: var(--ink); background: var(--paper); box-shadow: 0 25px 80px rgba(0,0,0,.28); }
dialog::backdrop { background: rgba(11,25,19,.55); backdrop-filter: blur(3px); }
.dialog-handle { width: 42px; height: 4px; border-radius: 10px; background: #d7ddd6; margin: -6px auto 18px; }
dialog h2 { margin: 0 0 18px; font-size: 21px; }
dialog label { display: block; font-size: 11px; font-weight: 700; margin: 14px 0; }
dialog textarea, dialog input[type="text"] { display: block; width: 100%; margin-top: 7px; border: 1px solid var(--line); background: #f5f6f2; padding: 12px; border-radius: 12px; outline: none; }
.toggle-label { display: flex; justify-content: space-between; align-items: center; }
.dialog-actions { display: grid; grid-template-columns: 1fr 1.5fr; gap: 9px; margin-top: 18px; }
.dialog-actions.stacked { grid-template-columns: 1fr; }
.primary-button, .secondary-button, .danger-button, .plain-button { border: 0; border-radius: 12px; padding: 12px; font-weight: 700; font-size: 12px; }
.primary-button { background: var(--forest); color: white; }
.secondary-button { background: #e9eee7; color: var(--forest); }
.danger-button { background: #f7e8e5; color: var(--danger); }
.plain-button { background: none; color: var(--muted); }
.full { width: 100%; margin-top: 8px; }
.trip-detail-hero { background: var(--forest); color: white; border-radius: 15px; padding: 16px; margin: 10px 0; }
.trip-detail-hero strong { color: var(--lime); font-size: 30px; }
.trip-detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 12px 0; }
.trip-detail-grid div { padding: 10px; background: #f0f3ee; border-radius: 10px; }
.trip-detail-grid span { display: block; color: var(--muted); font-size: 9px; margin-bottom: 3px; }
.trip-events { font-size: 11px; padding-left: 20px; }
.toast { position: fixed; z-index: 3000; left: 50%; bottom: 90px; transform: translate(-50%, 20px); background: var(--ink); color: white; border-radius: 12px; padding: 11px 16px; font-size: 11px; opacity: 0; pointer-events: none; transition: .25s; white-space: nowrap; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  .app-shell { min-height: 900px; margin-top: 20px; border-radius: 24px; }
  .bottom-nav { bottom: 20px; border-radius: 0 0 24px 24px; }
  #map { height: 350px; }
  .map-actions { top: 355px; }
}
