*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-secondary: #f2f2f7;
  --text: #1d1d1f;
  --text2: #86868b;
  --text3: #aeaeb2;
  --separator: rgba(60, 60, 67, 0.08);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 4px 24px rgba(0, 0, 0, 0.08), 0 12px 48px rgba(0, 0, 0, 0.06);
  --accent: #c8102e;
  --accent-bg: rgba(200, 16, 46, 0.08);
  --green: #34c759;
  --green-bg: rgba(52, 199, 89, 0.1);
  --orange: #ff9f0a;
  --orange-bg: rgba(255, 159, 10, 0.1);
  --blue: #007aff;
  --blue-bg: rgba(0, 122, 255, 0.08);
  --radius: 16px;
  --radius-sm: 12px;
  --radius-xs: 8px;
  --max-w: 960px;
  --header-h: 52px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --surface: #1c1c1e;
    --surface-secondary: #2c2c2e;
    --text: #f5f5f7;
    --text2: #98989d;
    --text3: #636366;
    --separator: rgba(255, 255, 255, 0.08);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.2), 0 4px 16px rgba(0, 0, 0, 0.16);
    --shadow-lg: 0 4px 24px rgba(0, 0, 0, 0.32);
  }
}

html {
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'SF Pro Text', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
}

/* ---- Login ---- */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.login-box {
  text-align: center;
  padding: 2rem;
  width: 100%;
  max-width: 340px;
}

.login-icon {
  width: 80px;
  height: 80px;
  border-radius: 22px;
  background: linear-gradient(135deg, #c8102e, #e8384f);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.5rem;
  font-size: 36px;
  box-shadow: 0 4px 20px rgba(200, 16, 46, 0.3);
}

.login-box h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 0.25rem;
}

.login-box .subtitle {
  color: var(--text2);
  font-size: 0.95rem;
  margin-bottom: 2rem;
}

.input-field {
  display: block;
  width: 100%;
  padding: 0.85rem 1rem;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--surface);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
  box-shadow: var(--shadow);
  outline: none;
  transition: box-shadow 0.2s;
}

.input-field:focus {
  box-shadow: 0 0 0 3px rgba(200, 16, 46, 0.2), var(--shadow);
}

.input-field::placeholder {
  color: var(--text3);
}

.btn-login {
  display: block;
  width: 100%;
  padding: 0.85rem;
  margin-top: 0.75rem;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.15s, opacity 0.15s;
}

.btn-login:active {
  transform: scale(0.98);
  opacity: 0.85;
}

/* ---- App Shell ---- */
.app {
  display: none;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(245, 245, 247, 0.72);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 0.5px solid var(--separator);
}

@media (prefers-color-scheme: dark) {
  .app-header {
    background: rgba(0, 0, 0, 0.72);
  }
}

.header-inner {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 0.75rem 1.25rem;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.header-top h1 {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.header-top h1 span {
  color: var(--accent);
}

.header-actions {
  display: flex;
  gap: 0.5rem;
}

.btn-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--surface-secondary);
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s, background 0.2s;
}

.btn-icon:active {
  transform: scale(0.92);
}

/* Segmented Control */
.segmented-control {
  display: flex;
  background: var(--surface-secondary);
  border-radius: var(--radius-xs);
  padding: 2px;
  position: relative;
}

.seg-btn {
  flex: 1;
  padding: 0.45rem 0.5rem;
  border: none;
  background: none;
  color: var(--text2);
  font-size: 0.8rem;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  border-radius: 6px;
  transition: color 0.2s;
  position: relative;
  z-index: 1;
  white-space: nowrap;
}

.seg-btn.active {
  color: var(--text);
  font-weight: 600;
  background: var(--surface);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06), 0 0.5px 1px rgba(0, 0, 0, 0.04);
}

/* Content */
.app-content {
  max-width: var(--max-w);
  margin: 0 auto;
  padding: 1rem 1.25rem 6rem;
}

.tab-content {
  display: none;
  animation: fadeIn 0.3s ease;
}

.tab-content.active {
  display: block;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---- Stat Cards ---- */
.stats-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem 1.1rem;
  box-shadow: var(--shadow);
  transition: transform 0.2s;
}

.stat-card:active {
  transform: scale(0.98);
}

.stat-label {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.35rem;
}

.stat-value {
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.1;
}

.stat-value small {
  font-size: 0.55em;
  font-weight: 500;
  color: var(--text2);
}

.stat-sub {
  font-size: 0.75rem;
  color: var(--text2);
  margin-top: 0.2rem;
}

.stat-card.accent {
  background: linear-gradient(135deg, #c8102e, #e8384f);
  color: #fff;
}

.stat-card.accent .stat-label {
  color: rgba(255, 255, 255, 0.7);
}

.stat-card.accent .stat-value small {
  color: rgba(255, 255, 255, 0.7);
}

.stat-card.accent .stat-sub {
  color: rgba(255, 255, 255, 0.7);
}

.stat-card.wide {
  grid-column: 1 / -1;
}

/* ---- Chart Cards ---- */
.chart-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.1rem;
  box-shadow: var(--shadow);
  margin-bottom: 0.75rem;
}

.chart-title {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 0.75rem;
}

.chart-card canvas {
  width: 100% !important;
  max-height: 200px;
}

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-bottom: 0.75rem;
}

/* ---- Section Titles ---- */
.section-title {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 1.25rem 0 0.75rem;
}

.section-subtitle {
  font-size: 0.8rem;
  color: var(--text2);
  margin: -0.5rem 0 0.75rem;
}

/* ---- Location List ---- */
.location-list {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.location-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1.1rem;
}

.location-item + .location-item {
  border-top: 0.5px solid var(--separator);
}

.location-rank {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--surface-secondary);
  color: var(--text2);
  font-size: 0.7rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 0.85rem;
  flex-shrink: 0;
}

.location-item:nth-child(1) .location-rank {
  background: var(--accent-bg);
  color: var(--accent);
}

.location-info {
  flex: 1;
  min-width: 0;
}

.location-name {
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.location-detail {
  font-size: 0.75rem;
  color: var(--text2);
}

.location-cost {
  text-align: right;
  flex-shrink: 0;
  margin-left: 0.75rem;
}

.location-cost .amount {
  font-size: 0.9rem;
  font-weight: 600;
}

.location-cost .liters {
  font-size: 0.75rem;
  color: var(--text2);
}

/* ---- Refuel List (Card-based) ---- */
.refuel-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.refuel-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.9rem 1.1rem;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.25rem;
  transition: transform 0.15s;
}

.refuel-card:active {
  transform: scale(0.99);
}

.refuel-top {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
}

.refuel-location {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.refuel-date {
  font-size: 0.75rem;
  color: var(--text2);
  flex-shrink: 0;
}

.refuel-meta {
  display: flex;
  gap: 1rem;
  font-size: 0.8rem;
  color: var(--text2);
  margin-top: 0.15rem;
}

.refuel-cost {
  font-size: 1.25rem;
  font-weight: 700;
  text-align: right;
  align-self: start;
  letter-spacing: -0.02em;
}

.refuel-consumption {
  text-align: right;
  font-size: 0.75rem;
  font-weight: 600;
  align-self: end;
}

.cons-good { color: var(--green); }
.cons-ok { color: var(--orange); }
.cons-bad { color: var(--accent); }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.5rem;
  border-radius: 20px;
  font-size: 0.7rem;
  font-weight: 600;
}

.badge-green {
  background: var(--green-bg);
  color: var(--green);
}

.badge-orange {
  background: var(--orange-bg);
  color: var(--orange);
}

.badge-red {
  background: var(--accent-bg);
  color: var(--accent);
}

/* ---- Cost Cards ---- */
.cost-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.cost-section {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  margin-bottom: 0.75rem;
}

.cost-section-header {
  padding: 0.85rem 1.1rem 0.5rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.cost-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.7rem 1.1rem;
}

.cost-row + .cost-row {
  border-top: 0.5px solid var(--separator);
}

.cost-row-type {
  font-size: 0.88rem;
}

.cost-row-date {
  font-size: 0.75rem;
  color: var(--text2);
}

.cost-row-amount {
  font-weight: 600;
  font-size: 0.88rem;
}

/* ---- Import ---- */
.import-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1.5rem;
  box-shadow: var(--shadow);
  text-align: center;
}

.import-card h3 {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 0.35rem;
}

.import-card p {
  font-size: 0.88rem;
  color: var(--text2);
  margin-bottom: 1.25rem;
}

.file-upload {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}

.file-upload input[type="file"] {
  font-family: inherit;
  font-size: 0.85rem;
  color: var(--text2);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.25rem;
  border-radius: var(--radius-xs);
  border: none;
  font-size: 0.88rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: transform 0.12s, opacity 0.15s;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-secondary {
  background: var(--surface-secondary);
  color: var(--text);
}

.import-result {
  margin-top: 1rem;
  font-size: 0.88rem;
  color: var(--green);
  font-weight: 500;
}

/* ---- Modal ---- */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 150;
  align-items: flex-end;
  justify-content: center;
  padding: 1rem;
}

.modal-overlay.active {
  display: flex;
}

.modal {
  background: var(--surface);
  border-radius: 20px;
  padding: 1.5rem;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

@keyframes slideUp {
  from { transform: translateY(100px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.modal h2 {
  font-size: 1.2rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 1.25rem;
}

.form-group {
  margin-bottom: 0.85rem;
}

.form-label {
  display: block;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 0.3rem;
}

.form-input {
  display: block;
  width: 100%;
  padding: 0.7rem 0.85rem;
  border-radius: var(--radius-xs);
  border: 0.5px solid var(--separator);
  background: var(--surface-secondary);
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.form-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(200, 16, 46, 0.12);
}

.modal-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1.25rem;
}

.modal-actions .btn {
  flex: 1;
  padding: 0.75rem;
}

/* ---- Empty State ---- */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text2);
}

.empty-state .icon {
  font-size: 3rem;
  margin-bottom: 1rem;
  opacity: 0.5;
}

.empty-state h3 {
  font-size: 1.1rem;
  color: var(--text);
  margin-bottom: 0.25rem;
}

.empty-state p {
  font-size: 0.88rem;
}

/* ---- Responsive ---- */
@media (max-width: 640px) {
  .charts-grid {
    grid-template-columns: 1fr;
  }

  .cost-grid {
    grid-template-columns: 1fr;
  }

  .stat-value {
    font-size: 1.4rem;
  }

  .modal-overlay {
    align-items: flex-end;
    padding: 0;
  }

  .modal {
    border-radius: 20px 20px 0 0;
    max-width: 100%;
    padding-bottom: 2rem;
  }
}

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

  .modal-overlay {
    align-items: center;
  }
}
