/* ============================================================
   TrackFleet — Stylesheet
   Mobile-first, CSS custom properties, no framework dependency
   ============================================================ */

/* ---------- Reset & Variables ---------- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary:       #1a73e8;
  --primary-dark:  #1558b0;
  --primary-light: #e8f0fe;
  --danger:        #d93025;
  --danger-light:  #fce8e6;
  --success:       #188038;
  --success-light: #e6f4ea;
  --warning:       #f9ab00;
  --warning-light: #fef7e0;
  --gray-50:       #f8f9fa;
  --gray-100:      #f1f3f4;
  --gray-200:      #e8eaed;
  --gray-400:      #9aa0a6;
  --gray-600:      #5f6368;
  --gray-800:      #3c4043;
  --gray-900:      #202124;
  --white:         #ffffff;
  --shadow-sm:     0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.08);
  --shadow-md:     0 4px 12px rgba(0,0,0,.15);
  --shadow-lg:     0 8px 24px rgba(0,0,0,.18);
  --radius-sm:     6px;
  --radius-md:     10px;
  --radius-lg:     16px;
  --radius-xl:     24px;
  --font:          'Segoe UI', system-ui, -apple-system, sans-serif;
  --nav-h-mobile:  56px;
  --bottom-nav-h:  64px;
  --desktop-nav-h: 60px;
}

html { font-size: 16px; -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font);
  color: var(--gray-900);
  background: var(--gray-50);
  min-height: 100dvh;
  line-height: 1.5;
}

/* ---------- Typography ---------- */
h1, h2, h3, h4 { font-weight: 600; line-height: 1.2; }
p { color: var(--gray-800); }
.text-muted  { color: var(--gray-400); font-size: .875rem; }
.text-mono   { font-family: 'Courier New', monospace; font-size: .85rem; }
.view-title  { font-size: 1.25rem; font-weight: 700; color: var(--gray-900); margin-bottom: 1rem; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .5rem 1.25rem;
  border: none; border-radius: var(--radius-sm);
  font-family: var(--font); font-size: .9375rem; font-weight: 500; cursor: pointer;
  transition: background .15s, opacity .15s, transform .1s;
  white-space: nowrap; text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: default; }

.btn-primary         { background: var(--primary); color: #fff; }
.btn-primary:hover   { background: var(--primary-dark); }
.btn-danger          { background: var(--danger); color: #fff; }
.btn-danger:hover    { background: #b52b20; }
.btn-danger-outline  { border: 1.5px solid var(--danger); color: var(--danger); background: transparent; }
.btn-outline         { border: 1.5px solid var(--gray-200); color: var(--gray-800); background: #fff; }
.btn-outline:hover   { background: var(--gray-100); }
.btn-ghost           { background: transparent; color: var(--gray-800); }
.btn-ghost:hover     { background: var(--gray-100); }
.btn-warning         { background: var(--warning); color: #000; }
.btn-icon            { background: transparent; border: none; font-size: 1rem; padding: .25rem .5rem; cursor: pointer; color: var(--gray-600); }
.btn-sm              { padding: .3rem .8rem; font-size: .8125rem; }
.btn-lg              { padding: .75rem 1.5rem; font-size: 1rem; }
.btn-block           { width: 100%; display: flex; }
.mt-sm               { margin-top: .5rem; }

/* ---------- Inputs ---------- */
.input {
  width: 100%; padding: .625rem .875rem;
  border: 1.5px solid var(--gray-200); border-radius: var(--radius-sm);
  font-family: var(--font); font-size: .9375rem; color: var(--gray-900);
  background: #fff; outline: none; transition: border-color .15s;
  -webkit-appearance: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(26,115,232,.15); }
.input[readonly] { background: var(--gray-100); color: var(--gray-600); }
.input-password-wrap { position: relative; }
.input-password-wrap .input { padding-right: 2.5rem; }
.toggle-password {
  position: absolute; right: .5rem; top: 50%; transform: translateY(-50%);
  background: none; border: none; cursor: pointer; font-size: 1rem; color: var(--gray-400);
}
.input-row { display: flex; gap: .5rem; }
.input-row .input { flex: 1; }

/* ---------- Forms ---------- */
.form-group { display: flex; flex-direction: column; gap: .375rem; }
.form-group label { font-size: .875rem; font-weight: 500; color: var(--gray-800); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.form-error { color: var(--danger); font-size: .85rem; min-height: 1rem; }
.hidden { display: none !important; }

/* ---------- Cards ---------- */
.card {
  background: #fff; border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm); padding: 1rem;
  margin-bottom: .75rem;
}
.card-title { font-size: 1rem; font-weight: 600; margin-bottom: .75rem; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block; padding: .2rem .6rem;
  border-radius: 9999px; font-size: .75rem; font-weight: 600;
  background: var(--gray-100); color: var(--gray-800);
}
.badge-success  { background: var(--success-light); color: var(--success); }
.badge-danger   { background: var(--danger-light);  color: var(--danger); }
.badge-primary  { background: var(--primary-light); color: var(--primary); }

/* ---------- Layout: Auth pages ---------- */
.auth-body {
  background: linear-gradient(135deg, #1a73e8 0%, #0d47a1 100%);
  min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}

.auth-container { width: 100%; max-width: 420px; }

.auth-card {
  background: #fff; border-radius: var(--radius-xl);
  box-shadow: var(--shadow-lg); padding: 2rem 1.75rem;
  overflow: hidden;
}

.auth-panel { display: none; }
.auth-panel.active { display: flex; flex-direction: column; gap: 1rem; }

.auth-logo   { font-size: 2.5rem; text-align: center; }
.auth-title  { font-size: 1.625rem; text-align: center; }
.auth-subtitle { font-size: .9rem; color: var(--gray-600); text-align: center; }
.auth-form { display: flex; flex-direction: column; gap: .875rem; }

.link-btn {
  background: none; border: none; cursor: pointer;
  color: var(--primary); font-size: .875rem; text-decoration: underline;
  align-self: center;
}
.back-btn {
  background: none; border: none; cursor: pointer;
  color: var(--primary); font-size: .875rem; align-self: flex-start;
}
.code-input {
  text-align: center; letter-spacing: .5rem; font-size: 1.5rem; font-weight: 700;
}

/* ---------- Layout: App shell ---------- */
.app-body {
  background: var(--gray-100);
  padding-top: var(--nav-h-mobile);
  padding-bottom: var(--bottom-nav-h);
}

.app-main {
  min-height: calc(100dvh - var(--nav-h-mobile) - var(--bottom-nav-h));
  overflow-y: auto;
}

/* ---------- Mobile header ---------- */
.mobile-header {
  position: fixed; top: 0; left: 0; right: 0; height: var(--nav-h-mobile);
  background: #fff; box-shadow: var(--shadow-sm); z-index: 100;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1rem;
}
.mobile-brand { font-size: 1.125rem; font-weight: 700; color: var(--primary); }
.nav-avatar-sm { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; }

/* ---------- Mobile bottom nav ---------- */
.mobile-bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0; height: var(--bottom-nav-h);
  background: #fff; box-shadow: 0 -1px 3px rgba(0,0,0,.1); z-index: 100;
  display: flex; align-items: stretch;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.mobile-nav-item {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; background: none; border: none; cursor: pointer;
  color: var(--gray-400); font-size: .6875rem; font-weight: 500;
  border-top: 2px solid transparent; transition: color .15s, border-color .15s;
  -webkit-tap-highlight-color: transparent; padding: 4px 2px;
}
.mobile-nav-item.active { color: var(--primary); border-top-color: var(--primary); }
.mobile-nav-icon { font-size: 1.25rem; line-height: 1; }
.mobile-nav-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 56px; }

/* ---------- Desktop header ---------- */
.desktop-header {
  position: fixed; top: 0; left: 0; right: 0; height: var(--desktop-nav-h);
  background: #fff; box-shadow: var(--shadow-sm); z-index: 100;
  display: none; align-items: center; padding: 0 1.5rem; gap: 1rem;
}

.nav-brand { display: flex; align-items: center; gap: .5rem; flex-shrink: 0; }
.nav-logo  { font-size: 1.5rem; }
.nav-title { font-size: 1.125rem; font-weight: 700; color: var(--primary); }

.nav-items { display: flex; align-items: center; gap: .25rem; flex: 1; }

.nav-item {
  display: flex; align-items: center; gap: .375rem;
  padding: .4rem .875rem; border-radius: var(--radius-sm);
  background: none; border: none; cursor: pointer; font-size: .875rem;
  color: var(--gray-800); font-weight: 500; transition: background .15s, color .15s;
  -webkit-tap-highlight-color: transparent;
}
.nav-item:hover   { background: var(--gray-100); }
.nav-item.active  { background: var(--primary-light); color: var(--primary); }
.nav-icon { font-size: 1rem; }

.nav-right { display: flex; align-items: center; gap: .75rem; margin-left: auto; }
.nav-profile-btn {
  display: flex; align-items: center; gap: .5rem;
  background: none; border: none; cursor: pointer; font-size: .875rem;
  font-weight: 500; color: var(--gray-800); padding: .25rem .5rem;
  border-radius: var(--radius-sm); transition: background .15s;
}
.nav-profile-btn:hover { background: var(--gray-100); }
.nav-avatar { width: 32px; height: 32px; border-radius: 50%; object-fit: cover; }

/* Desktop breakpoint */
@media (min-width: 768px) {
  .desktop-header  { display: flex; }
  .mobile-header   { display: none; }
  .mobile-bottom-nav { display: none; }
  .app-body {
    padding-top: var(--desktop-nav-h);
    padding-bottom: 0;
  }
  .app-main {
    min-height: calc(100dvh - var(--desktop-nav-h));
    max-width: 1280px; margin: 0 auto; padding: 1.5rem;
  }
}

/* ---------- View content ---------- */
.view-content { padding: 1rem; max-width: 100vw; margin: 0 auto; }

.view-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 1rem;
}
.view-header .view-title { margin-bottom: 0; }

/* ---------- Loading & Errors ---------- */
.loading-spinner {
  display: flex; justify-content: center; align-items: center;
  min-height: 200px;
}
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--gray-200);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-state, .empty-state {
  text-align: center; padding: 3rem 1rem; color: var(--gray-400);
}

/* ---------- Home View ---------- */
.home-view { display: flex; flex-direction: column; gap: 1rem; align-items: stretch; }

.status-card {
  border-radius: var(--radius-lg); padding: 2rem 1.5rem;
  text-align: center; display: flex; flex-direction: column; align-items: center; gap: .5rem;
}
.status-working { background: linear-gradient(135deg, #e6f4ea, #d2f0da); }
.status-idle    { background: linear-gradient(135deg, #f8f9fa, #e8eaed); }

.status-icon  { font-size: 3rem; }
.status-label { font-size: 1.25rem; font-weight: 700; }
.status-sub   { font-size: .875rem; color: var(--gray-600); }

.stats-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: .75rem;
}
.stat-item {
  background: #fff; border-radius: var(--radius-md);
  padding: 1rem; text-align: center;
  box-shadow: var(--shadow-sm);
  display: flex; flex-direction: column; gap: .25rem;
}
.stat-label { font-size: .75rem; color: var(--gray-400); text-transform: uppercase; letter-spacing: .05em; }
.stat-value { font-size: 1.5rem; font-weight: 700; color: var(--primary); font-variant-numeric: tabular-nums; }

/* ---------- Admin Dashboard ---------- */
.dashboard-greeting {
  display: flex; align-items: center; gap: .625rem;
  margin-bottom: 1.25rem;
}
.dashboard-greeting h2 { font-size: 1.375rem; }
.dashboard-wave { font-size: 1.75rem; }

.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: .875rem;
}

.dashboard-card {
  background: #fff; border: none; border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm); padding: 1.5rem 1rem;
  display: flex; flex-direction: column; align-items: center; gap: .375rem;
  cursor: pointer; transition: box-shadow .15s, transform .1s;
  text-align: center;
}
.dashboard-card:hover  { box-shadow: var(--shadow-md); }
.dashboard-card:active { transform: scale(.97); }

.dashboard-card-icon  { font-size: 2rem; line-height: 1; }
.dashboard-card-value {
  font-size: 2.25rem; font-weight: 800;
  color: var(--primary); font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.dashboard-card-label {
  font-size: .8125rem; color: var(--gray-600); font-weight: 500;
}

@media (min-width: 480px) {
  .dashboard-grid { grid-template-columns: repeat(4, 1fr); }
}

/* ---------- Stats View ---------- */
.stats-periods { display: flex; flex-direction: column; gap: 1rem; }

.period-card {
  background: #fff; border-radius: var(--radius-md);
  box-shadow: var(--shadow-sm); overflow: hidden;
}
.period-title {
  padding: .75rem 1rem; font-size: .9375rem; font-weight: 700;
  background: var(--primary); color: #fff;
}
.period-stats {
  display: grid; grid-template-columns: repeat(2, 1fr);
  gap: 1px; background: var(--gray-200);
}
.period-stat {
  background: #fff; padding: .875rem 1rem;
  display: flex; flex-direction: column; gap: .2rem;
}
.period-stat.highlight { grid-column: span 2; background: var(--primary-light); }
.period-value { font-size: 1.25rem; font-weight: 700; color: var(--gray-900); }
.period-label { font-size: .75rem; color: var(--gray-400); }

/* ---------- Map View ---------- */
.map-view { display: flex; flex-direction: column; gap: .75rem; }
#leaflet-map, #history-map {
  height: calc(100dvh - var(--nav-h-mobile) - var(--bottom-nav-h) - 80px);
  min-height: 300px; border-radius: var(--radius-md); overflow: hidden;
  box-shadow: var(--shadow-sm);
}
@media (min-width: 768px) {
  #leaflet-map { height: 500px; }
}
.map-update-info { font-size: .8125rem; color: var(--gray-400); text-align: center; }

.map-vehicle-icon { background: none !important; border: none !important; }
.vehicle-dot {
  width: 20px; height: 20px;
  background: var(--primary); border: 3px solid #fff;
  border-radius: 50%; box-shadow: 0 2px 6px rgba(0,0,0,.3);
}

/* ---------- History View ---------- */
.history-filters { margin-bottom: 1rem; }
.filters-row {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: .75rem; margin-bottom: .75rem;
}
.filters-actions { display: flex; gap: .5rem; }

.history-list { display: flex; flex-direction: column; gap: .75rem; }
.history-item {}
.history-item-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: .5rem;
}
.history-status { font-weight: 600; font-size: .875rem; }
.history-plate  { font-family: monospace; background: var(--gray-100); padding: .15rem .5rem; border-radius: 4px; font-size: .8125rem; }
.history-meta   { display: flex; flex-direction: column; gap: .2rem; font-size: .8125rem; color: var(--gray-600); margin-bottom: .5rem; }
.history-events { display: flex; flex-direction: column; gap: .375rem; }

.event-item {
  display: flex; align-items: center; gap: .5rem;
  font-size: .8125rem; color: var(--gray-800);
}
.event-dot {
  width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0;
}
.event-start .event-dot  { background: var(--success); }
.event-end   .event-dot  { background: var(--danger); }

.pagination {
  display: flex; align-items: center; justify-content: center;
  gap: 1rem; margin-top: 1rem; padding: .5rem;
}
.pagination span { font-size: .875rem; color: var(--gray-600); }

/* ---------- Profile View ---------- */
.profile-avatar-section {
  display: flex; flex-direction: column; align-items: center; gap: .5rem;
  padding: 1.5rem 1rem;
}
.avatar-wrapper { position: relative; display: inline-block; }
.avatar-img {
  width: 88px; height: 88px; border-radius: 50%;
  object-fit: cover; border: 3px solid var(--gray-200);
  display: block;
}
.avatar-edit-btn {
  position: absolute; bottom: 0; right: 0;
  width: 28px; height: 28px;
  background: var(--primary); border-radius: 50%; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: .75rem; color: #fff;
}
.profile-name  { font-size: 1.125rem; font-weight: 700; }
.profile-email { font-size: .875rem; color: var(--gray-600); }

.session-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: .625rem 0; border-bottom: 1px solid var(--gray-100);
}
.session-item:last-child { border-bottom: none; }
.session-info { display: flex; flex-direction: column; gap: .15rem; }
.session-device { font-size: .875rem; font-weight: 500; }

/* ---------- Expenses View ---------- */
.expenses-list { display: flex; flex-direction: column; gap: .75rem; }
.expense-item {}
.expense-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: .5rem; }
.expense-amount { font-size: 1.25rem; font-weight: 700; color: var(--gray-900); }
.expense-plate  { font-family: monospace; font-size: .8rem; color: var(--gray-600); margin-left: .75rem; }
.expense-body   { display: flex; align-items: baseline; gap: .5rem; margin-bottom: .5rem; }
.expense-desc   { font-size: .8125rem; color: var(--gray-600); margin-top: .25rem; }
.expense-actions { display: flex; gap: .375rem; justify-content: flex-end; }

/* ---------- Admin Tables ---------- */
.search-bar { margin-bottom: .75rem; }

.admin-table-wrap { overflow-x: auto; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); }

.admin-table {
  width: 100%; border-collapse: collapse; background: #fff;
  font-size: .875rem;
}
.admin-table thead th {
  background: var(--gray-50); padding: .75rem 1rem;
  text-align: left; font-weight: 600; font-size: .8125rem;
  text-transform: uppercase; letter-spacing: .04em; color: var(--gray-600);
  border-bottom: 1px solid var(--gray-200); white-space: nowrap;
}
.admin-table tbody td {
  padding: .75rem 1rem; border-bottom: 1px solid var(--gray-100);
  vertical-align: middle;
}
.admin-table tbody tr:last-child td { border-bottom: none; }
.admin-table tbody tr:hover td { background: var(--gray-50); }

.actions-cell { display: flex; gap: .375rem; align-items: center; }

/* ---------- Admin list (non-table) ---------- */
.admin-list { background: #fff; border-radius: var(--radius-md); box-shadow: var(--shadow-sm); }
.admin-list-item {
  display: flex; align-items: center; gap: .75rem; padding: .75rem 1rem;
  border-bottom: 1px solid var(--gray-100);
}
.admin-list-item:last-child { border-bottom: none; }
.item-name { flex: 1; font-size: .9375rem; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 1000;
  display: flex; align-items: flex-end; justify-content: center;
  opacity: 0; transition: opacity .2s;
}
.modal-overlay.open { opacity: 1; }
.modal-overlay.open .modal-box { transform: translateY(0); }

.modal-box {
  background: #fff; border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  width: 100%; max-width: 540px; max-height: 92dvh;
  padding: 1.25rem 1.5rem 2rem;
  overflow-y: auto; display: flex; flex-direction: column; gap: .875rem;
  transform: translateY(30px); transition: transform .2s;
  position: relative;
}

@media (min-width: 640px) {
  .modal-overlay { align-items: center; }
  .modal-box {
    border-radius: var(--radius-xl);
    transform: scale(.95); transition: transform .2s, opacity .2s;
  }
  .modal-overlay.open .modal-box { transform: scale(1); }
}

.modal-close {
  position: absolute; top: 1rem; right: 1rem;
  font-size: 1rem; color: var(--gray-400);
}
.modal-form { display: flex; flex-direction: column; gap: .75rem; }
.modal-actions { display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; }
.modal-confirm-msg { font-size: 1rem; font-weight: 500; text-align: center; padding: .5rem 0; }

/* ---------- Toasts ---------- */
#toast-container {
  position: fixed; top: calc(var(--nav-h-mobile) + .75rem); right: .75rem;
  display: flex; flex-direction: column; gap: .5rem; z-index: 2000; pointer-events: none;
}
@media (min-width: 768px) {
  #toast-container { top: calc(var(--desktop-nav-h) + .75rem); }
}

.toast {
  background: var(--gray-900); color: #fff;
  padding: .625rem 1rem; border-radius: var(--radius-md);
  font-size: .875rem; font-weight: 500;
  box-shadow: var(--shadow-md); pointer-events: all;
  opacity: 0; transform: translateX(20px);
  transition: opacity .25s, transform .25s;
  max-width: 300px;
}
.toast.show { opacity: 1; transform: translateX(0); }
.toast-error   { background: var(--danger); }
.toast-success { background: var(--success); }
.toast-warning { background: var(--warning); color: #000; }

/* ---------- Autocomplete ---------- */
.autocomplete-wrap { position: relative; }

.autocomplete-dropdown {
  display: none;
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 200;
  background: #fff; border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-md);
  max-height: 220px; overflow-y: auto;
}
.autocomplete-dropdown.open { display: block; }

.ac-item {
  display: flex; flex-direction: column; gap: .1rem;
  padding: .625rem .875rem; cursor: pointer;
  transition: background .1s;
}
.ac-item:hover    { background: var(--primary-light); }
.ac-item + .ac-item { border-top: 1px solid var(--gray-100); }
.ac-empty  { color: var(--gray-400); cursor: default; font-size: .875rem; }
.ac-name   { font-size: .9375rem; font-weight: 500; color: var(--gray-900); }
.ac-sub    { font-size: .8rem; color: var(--gray-400); }

/* ---------- Misc helpers ---------- */
.vehicles-list { display: flex; flex-wrap: wrap; gap: .5rem; margin: .5rem 0; }
.vehicle-chip {
  display: flex; align-items: center; gap: .375rem;
  background: var(--gray-100); border-radius: var(--radius-sm);
  padding: .3rem .75rem; font-size: .875rem;
}

/* Responsive adjustments */
@media (max-width: 400px) {
  .filters-row { grid-template-columns: 1fr; }
  .form-row    { grid-template-columns: 1fr; }
  .period-stats { grid-template-columns: 1fr; }
  .period-stat.highlight { grid-column: span 1; }
}

/* Scrollbar styling (webkit) */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--gray-200); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gray-400); }
