/* Les variables (couleurs, typo, rayons, ombres) sont dans styles/theme.css */

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

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

body {
  margin: 0;
  background-color: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-family);
  font-weight: var(--fw-medium);
  font-size: var(--fs-body);
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  -webkit-tap-highlight-color: transparent;
}

button,
input,
output {
  font-family: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

h1, h2, p {
  margin: 0;
}

:focus-visible {
  outline: 2px solid var(--color-accent);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

.num {
  font-variant-numeric: tabular-nums;
}

/* Mise en page */
.app-container {
  max-width: var(--content-max-width);
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
}

.main-content {
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 24px);
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.view-body {
  display: flex;
  flex-direction: column;
  gap: var(--stack-gap);
  padding: 12px var(--gutter) 0;
}

/* En-tête de page */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 22px 20px 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-logo {
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  border-radius: var(--radius-field);
  background: var(--color-accent);
  color: var(--color-on-accent);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-logo);
}

.page-title {
  font-size: var(--fs-title);
  font-weight: var(--fw-extrabold);
  line-height: 1.15;
  letter-spacing: var(--tracking-tight);
}

.page-subtitle {
  font-size: var(--fs-small);
  font-weight: var(--fw-medium);
  color: var(--color-text-secondary);
}

/* Compte (connexion par lien magique Supabase) */
[data-auth="out"] .account-signed-in,
[data-auth="in"] .account-signed-out {
  display: none;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 8px;
}

.login-form .form-label {
  margin-bottom: 0;
}

.otp-input {
  text-align: center;
  font-size: var(--fs-stepper);
  font-weight: var(--fw-extrabold);
  letter-spacing: .25em;
}

#login-message:empty {
  display: none;
}

#account-email {
  color: var(--color-text);
  word-break: break-all;
}
.account {
  position: relative;
  flex: 0 0 auto;
}

.account-btn {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 0;
  background: var(--color-surface);
  box-shadow: var(--shadow-raised);
  color: var(--color-text);
  display: flex;
  align-items: center;
  justify-content: center;
}

.account-status {
  position: absolute;
  top: 1px;
  right: 1px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 2px solid var(--color-surface);
  background: var(--color-placeholder);
}

[data-auth="in"] .account-status {
  background: var(--color-accent);
}

.account-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 20;
  width: min(300px, calc(100vw - 32px));
  padding: 8px;
  border-radius: var(--radius-button);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  box-shadow: var(--shadow-card);
}

.account-menu-label {
  padding: 8px 10px;
  font-size: var(--fs-small);
  color: var(--color-text-secondary);
}

.account-menu-item {
  width: 100%;
  min-height: 48px;
  padding: 0 10px;
  border: 0;
  border-radius: var(--radius-icon);
  background: none;
  text-align: left;
  font-size: var(--fs-body);
  font-weight: var(--fw-bold);
  color: var(--color-danger);
}

.account-menu-item:hover {
  background: var(--color-danger-soft);
}

/* Barre de navigation fixe */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding: 8px 10px max(10px, env(safe-area-inset-bottom));
}

.bottom-nav-tabs {
  display: flex;
  justify-content: space-around;
  max-width: var(--content-max-width);
  margin: 0 auto;
}

.tab-btn {
  flex: 1;
  min-width: 0;
  min-height: var(--touch-target);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 0;
  border-radius: var(--radius-icon);
  background: none;
  color: var(--color-text-secondary);
  font-size: var(--fs-caption);
  font-weight: var(--fw-bold);
}

.tab-icon {
  width: 52px;
  height: 30px;
  border-radius: var(--radius-pill);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color .15s ease;
}

.tab-btn.active {
  color: var(--color-accent-text);
  font-weight: var(--fw-extrabold);
}

.tab-btn.active .tab-icon {
  background: var(--color-accent-soft);
}

/* Cartes */
.card {
  background: var(--color-surface);
  border-radius: var(--radius-card);
  padding: var(--card-padding);
  box-shadow: var(--shadow-card);
}

.card h2 {
  margin-bottom: 18px;
  font-size: var(--fs-title-sm);
  font-weight: var(--fw-extrabold);
  letter-spacing: var(--tracking-tight);
}

/* Formulaire */
.purchase-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-label {
  display: block;
  margin-bottom: 8px;
  font-size: var(--fs-label);
  font-weight: var(--fw-bold);
  color: var(--color-text-label);
}

.form-control {
  width: 100%;
  height: var(--field-height);
  padding: 0 16px;
  border-radius: var(--radius-field);
  border: 1.5px solid var(--color-field-border);
  background: var(--color-field-bg);
  color: var(--color-text);
  font-size: var(--fs-input);
  font-weight: var(--fw-semibold);
  transition: border-color .15s ease, box-shadow .15s ease;
}

/* Safari iOS : le champ date natif impose sa largeur minimale et déborde */
.form-control[type="date"] {
  display: block;
  min-width: 0;
  max-width: 100%;
  -webkit-appearance: none;
  appearance: none;
  /* Sans l'apparence native, iOS place la valeur en haut : on la recentre (hauteur moins les bordures) */
  padding-top: 0;
  padding-bottom: 0;
  line-height: calc(var(--field-height) - 3px);
}

.form-control[type="date"]::-webkit-date-and-time-value {
  text-align: left;
  margin: 0;
  padding: 0;
  line-height: calc(var(--field-height) - 3px);
}

.form-control::placeholder {
  color: var(--color-placeholder);
}

.form-control:focus {
  outline: none;
  border-color: var(--color-accent);
  box-shadow: var(--focus-ring);
}

/* Boutons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0 18px;
  border: 0;
  border-radius: var(--radius-button);
  background: var(--color-surface);
  color: var(--color-accent-text);
  font-size: var(--fs-body);
  font-weight: var(--fw-extrabold);
  transition: background-color .15s ease, opacity .15s ease;
}

.btn--primary {
  min-height: var(--button-height);
  background: var(--color-accent);
  color: var(--color-on-accent);
  font-size: var(--fs-title-sm);
  box-shadow: var(--shadow-accent);
}

.btn--primary:hover:not(:disabled) {
  background: var(--color-accent-text);
}

.btn--full-width {
  width: 100%;
}

.btn:disabled {
  opacity: .45;
  cursor: not-allowed;
  box-shadow: none;
}

#message {
  font-size: var(--fs-label);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
  text-align: center;
}

#message:empty {
  display: none;
}

/* Accueil : stock et bouton « J'ai ouvert un sac » */
[data-auth="in"] .home-signed-out {
  display: none;
}

.home-hint {
  font-size: var(--fs-label);
  color: var(--color-text-secondary);
  margin-top: -8px;
}

.count-card h2 {
  font-size: var(--fs-title);
}

.count-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-top: 16px;
}

.count-form .link-btn {
  align-self: center;
}

.stock-card {
  padding: var(--card-padding);
  border-radius: var(--radius-card);
  background: var(--color-accent);
  color: var(--color-on-accent);
  box-shadow: var(--shadow-accent-card);
}

.stock-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stock-label {
  font-size: var(--fs-label);
  font-weight: var(--fw-semibold);
  opacity: .92;
}

.stock-recount {
  min-height: 44px;
  margin: -12px -4px -12px 0;
  padding: 0 4px;
  border: 0;
  background: none;
  color: var(--color-on-accent);
  font-size: var(--fs-small);
  font-weight: var(--fw-bold);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.stock-value {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0 8px;
  margin-top: 4px;
  font-size: var(--fs-hero);
  font-weight: var(--fw-extrabold);
  letter-spacing: var(--tracking-tighter);
  line-height: 1.2;
}

.stock-autonomy {
  font-size: var(--fs-label);
  font-weight: var(--fw-bold);
  letter-spacing: 0;
}

.stock-meta {
  margin-top: 6px;
  font-size: var(--fs-small);
  font-weight: var(--fw-semibold);
}

.open-bag-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: 100%;
  min-height: 64px;
  margin-top: 18px;
  border: 0;
  border-radius: var(--radius-button);
  background: var(--color-surface);
  color: var(--color-accent-text);
  font-size: var(--fs-title-sm);
  font-weight: var(--fw-extrabold);
  transition: transform .1s ease, opacity .15s ease;
}

.open-bag-btn:active {
  transform: scale(.98);
}

.open-bag-btn:disabled {
  opacity: .7;
}

.open-bag-btn:focus-visible {
  outline-color: var(--color-on-accent);
}

.stock-last {
  margin-top: 10px;
  text-align: center;
  font-size: var(--fs-small);
  font-weight: var(--fw-semibold);
  opacity: .95;
}

.stock-undo {
  min-height: 32px;
  padding: 0 2px;
  border: 0;
  background: none;
  color: inherit;
  font-size: inherit;
  font-weight: var(--fw-extrabold);
  text-decoration: underline;
  text-underline-offset: 3px;
}

.week-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 16px var(--card-padding);
}

.week-label {
  font-size: var(--fs-label);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
}

.week-value {
  font-size: var(--fs-body);
  font-weight: var(--fw-extrabold);
  white-space: nowrap;
}

/* Écran Saisie : formulaire */
.form-card h2 {
  font-size: var(--fs-title);
}

.form-control[type="number"] {
  -moz-appearance: textfield;
  appearance: textfield;
}

.form-control[type="number"]::-webkit-inner-spin-button,
.form-control[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.stepper {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stepper-btn {
  flex: 0 0 var(--field-height);
  width: var(--field-height);
  height: var(--field-height);
  border-radius: var(--radius-field);
  border: 1.5px solid var(--color-field-border);
  background: var(--color-field-bg);
  color: var(--color-text);
  font-size: 24px;
  font-weight: var(--fw-semibold);
  line-height: 1;
}

.stepper-btn:active {
  background: var(--color-divider);
}

.stepper-input {
  flex: 1;
  min-width: 0;
  padding: 0 8px;
  text-align: center;
  font-size: var(--fs-stepper);
  font-weight: var(--fw-extrabold);
}

.qty-presets {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.qty-preset {
  position: relative;
  flex: 1;
  height: 38px;
  border-radius: var(--radius-pill);
  border: 1.5px solid var(--color-field-border);
  background: var(--color-surface);
  color: var(--color-text-label);
  font-size: var(--fs-label);
  font-weight: var(--fw-bold);
}

/* Zone de toucher portée à 44px sans changer le visuel */
.qty-preset::after {
  content: '';
  position: absolute;
  inset: -4px 0;
}

.qty-preset[aria-pressed="true"] {
  border-color: var(--color-accent);
  background: var(--color-accent-soft);
  color: var(--color-accent-text);
  font-weight: var(--fw-extrabold);
}

.input-suffix {
  position: relative;
}

.input-suffix .form-control {
  padding-right: 70px;
}

.input-suffix-text {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  font-size: var(--fs-label);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
  pointer-events: none;
}

.total-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 16px 18px;
  border-radius: var(--radius-button);
  background: var(--color-accent-soft);
}

.total-label {
  font-size: var(--fs-label);
  font-weight: var(--fw-bold);
  color: var(--color-text-strong);
}

.total-hint {
  margin-top: 2px;
  font-size: var(--fs-caption);
  font-weight: var(--fw-medium);
  color: var(--color-text-secondary);
}

.total-value {
  font-size: var(--fs-total);
  font-weight: var(--fw-extrabold);
  letter-spacing: var(--tracking-tighter);
  color: var(--color-text);
  white-space: nowrap;
}

.total-value:empty::before {
  content: '0,00';
}

.total-value::after {
  content: '\00a0€';
}

/* Écran Saisie : derniers achats */
.recent-card {
  padding: 18px 20px 8px;
}

[data-auth="out"] .recent-card {
  display: none;
}

.recent-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.recent-card h2 {
  margin: 0;
}

.link-btn {
  min-height: 44px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--color-accent-text);
  font-size: var(--fs-label);
  font-weight: var(--fw-bold);
}

.purchase-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.purchase-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 0;
  border-top: 1px solid var(--color-divider);
}

.purchase-item:last-child {
  padding-bottom: 14px;
}

.purchase-icon {
  flex: 0 0 42px;
  width: 42px;
  height: 42px;
  border-radius: var(--radius-icon);
  background: var(--color-accent-soft);
  color: var(--color-accent-text);
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.purchase-date {
  font-size: var(--fs-body);
  font-weight: var(--fw-bold);
}

.purchase-meta {
  margin-top: 2px;
  font-size: var(--fs-small);
  font-weight: var(--fw-medium);
  color: var(--color-text-secondary);
}

.purchase-total {
  flex: 0 0 auto;
  font-size: var(--fs-input);
  font-weight: var(--fw-extrabold);
  white-space: nowrap;
}

.delete-btn {
  flex: 0 0 36px;
  width: 36px;
  height: 44px;
  margin-right: -8px;
  padding: 0;
  border: 0;
  border-radius: var(--radius-icon);
  background: none;
  color: var(--color-text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.delete-btn:hover,
.delete-btn:focus-visible {
  color: var(--color-danger);
  background: var(--color-danger-soft);
}

.recent-card .empty-state {
  padding: 12px 0 14px;
  border-top: 1px solid var(--color-divider);
}

/* Écrans étroits : l'icône décorative laisse la place à la date et au montant */
@media (max-width: 389px) {
  .purchase-item {
    gap: 10px;
  }

  .purchase-icon {
    display: none;
  }
}

/* Écran Stats */
.hero-card {
  padding: var(--card-padding);
  border-radius: var(--radius-card);
  background: var(--color-accent);
  color: var(--color-on-accent);
  box-shadow: var(--shadow-accent-card);
}

.hero-label {
  font-size: var(--fs-label);
  font-weight: var(--fw-semibold);
  opacity: .92;
}

.hero-value {
  margin-top: 4px;
  font-size: var(--fs-hero);
  font-weight: var(--fw-extrabold);
  letter-spacing: var(--tracking-tighter);
  line-height: 1.2;
}

.hero-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  margin-top: 14px;
  font-size: var(--fs-small);
  font-weight: var(--fw-semibold);
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.stat-card {
  background: var(--color-surface);
  border-radius: var(--radius-tile);
  padding: 16px;
  box-shadow: var(--shadow-tile);
}

.stat-label {
  font-size: var(--fs-small);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
}

.stat-value {
  margin-top: 6px;
  font-size: var(--fs-title);
  font-weight: var(--fw-extrabold);
}

.stat-sub {
  margin-top: 2px;
  font-size: var(--fs-caption);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
}

.chart-card {
  padding: 18px 18px 16px;
}

.chart-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.chart-card h2 {
  margin: 0;
}

.segmented {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius-icon);
  background: var(--color-segment-bg);
}

.segmented-btn {
  position: relative;
  min-height: 32px;
  padding: 0 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--color-text-label);
  font-size: var(--fs-small);
  font-weight: var(--fw-bold);
}

/* Zone de toucher portée à 44px sans changer le visuel */
.segmented-btn::after {
  content: '';
  position: absolute;
  inset: -6px 0;
}

.segmented-btn[aria-pressed="true"] {
  background: var(--color-surface);
  color: var(--color-accent-text);
  font-weight: var(--fw-extrabold);
  box-shadow: var(--shadow-raised);
}

.chart-empty {
  padding: 24px 8px;
  text-align: center;
  font-size: var(--fs-label);
  color: var(--color-text-secondary);
}

.chart-legend {
  display: flex;
  gap: 16px;
  margin-bottom: 12px;
  font-size: var(--fs-small);
  font-weight: var(--fw-semibold);
  color: var(--color-text-label);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.legend-bar {
  width: 12px;
  height: 12px;
  border-radius: 4px;
  background: var(--color-accent);
}

.legend-line {
  width: 14px;
  height: 3px;
  border-radius: 2px;
  background: var(--color-spend);
}

.chart-container {
  position: relative;
  height: 186px;
}

.btn--secondary {
  min-height: var(--field-height);
  box-shadow: var(--shadow-button);
}

/* Écran Détails */
.month-card {
  padding: 6px 18px;
}

.month-list {
  margin: 0;
  padding: 0;
  list-style: none;
}

.month-item {
  padding: 10px 0;
  border-top: 1px solid var(--color-divider);
}

.month-item:first-child {
  border-top: 0;
}

.month-item:last-child {
  padding-bottom: 14px;
}

.month-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.month-name {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  font-size: var(--fs-body);
  font-weight: var(--fw-bold);
}

.badge {
  padding: 2px 8px;
  border-radius: var(--radius-pill);
  background: var(--color-badge-bg);
  color: var(--color-badge-text);
  font-size: var(--fs-badge);
  font-weight: var(--fw-extrabold);
}

.month-meta {
  margin-top: 2px;
  font-size: var(--fs-caption);
  font-weight: var(--fw-semibold);
  color: var(--color-text-secondary);
}

.month-total {
  flex: 0 0 auto;
  font-size: var(--fs-body);
  font-weight: var(--fw-extrabold);
  white-space: nowrap;
}

.progress {
  height: 8px;
  margin-top: 8px;
  border-radius: var(--radius-pill);
  background: var(--color-divider);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: var(--radius-pill);
  background: var(--color-accent);
}

.month-empty {
  padding: 14px 0;
}

/* Consommation dans le détail mensuel */
.month-legend {
  display: flex;
  gap: 16px;
  padding: 10px 0 6px;
  font-size: var(--fs-caption);
  font-weight: var(--fw-semibold);
  color: var(--color-text-label);
}

.month-legend + .month-item {
  border-top: 1px solid var(--color-divider);
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: var(--radius-pill);
  background: var(--color-accent);
}

.legend-dot--burned,
.progress-fill--burned {
  background: var(--color-spend);
}

.month-row--burned {
  margin-top: 8px;
}

.month-burned-cost {
  flex: 0 0 auto;
  font-size: var(--fs-small);
  font-weight: var(--fw-bold);
  color: var(--color-text-secondary);
  white-space: nowrap;
}

.summary-card {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  padding: 16px 14px;
  border-radius: var(--radius-summary);
  background: var(--color-accent);
  color: var(--color-on-accent);
  box-shadow: var(--shadow-accent-card);
}

.summary-label {
  font-size: var(--fs-caption);
  font-weight: var(--fw-semibold);
  opacity: .9;
}

.summary-value {
  margin-top: 4px;
  font-size: clamp(16px, 4.6vw, 18px);
  font-weight: var(--fw-extrabold);
  white-space: nowrap;
}

.empty-state,
.month-empty {
  color: var(--color-text-secondary);
  font-size: var(--fs-label);
}
