/* ===== Tokens : surfaces & texte suivent le thème Telegram, séries validées CVD ===== */
:root {
  --bg: var(--tg-theme-secondary-bg-color, #efeff4);
  --card: var(--tg-theme-section-bg-color, #ffffff);
  --field: var(--tg-theme-secondary-bg-color, #efeff4);
  --text: var(--tg-theme-text-color, #0b0b0b);
  --hint: var(--tg-theme-hint-color, #8a8a8e);
  --sep: var(--tg-theme-section-separator-color, rgba(60, 60, 67, 0.16));
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #d03b3b);

  --in: #0f9d7a;
  --out: #e0533a;
  --profit: #2a78d6;
  --warn: #c98500;
  --grid: rgba(120, 120, 128, 0.18);

  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;

  --radius: 16px;
  --safe-top: var(--tg-content-safe-area-inset-top, 0px);
  --safe-bottom: var(--tg-safe-area-inset-bottom, env(safe-area-inset-bottom, 0px));
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: var(--tg-theme-secondary-bg-color, #000000);
    --card: var(--tg-theme-section-bg-color, #1c1c1e);
    --field: var(--tg-theme-secondary-bg-color, #2c2c2e);
    --text: var(--tg-theme-text-color, #ffffff);
    --hint: var(--tg-theme-hint-color, #98989e);
    --sep: var(--tg-theme-section-separator-color, rgba(84, 84, 88, 0.6));
    --in: #199e70; --out: #d95926; --profit: #3987e5; --warn: #fab219;
    --grid: rgba(120, 120, 128, 0.22);
  }
}
:root[data-theme="dark"] {
  --bg: var(--tg-theme-secondary-bg-color, #000000);
  --card: var(--tg-theme-section-bg-color, #1c1c1e);
  --field: var(--tg-theme-secondary-bg-color, #2c2c2e);
  --text: var(--tg-theme-text-color, #ffffff);
  --hint: var(--tg-theme-hint-color, #98989e);
  --sep: var(--tg-theme-section-separator-color, rgba(84, 84, 88, 0.6));
  --in: #199e70; --out: #d95926; --profit: #3987e5; --warn: #fab219;
  --grid: rgba(120, 120, 128, 0.22);
}

/* ===== Base ===== */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
  min-height: 100vh;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
b { font-weight: 600; }
[hidden] { display: none !important; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.eyebrow {
  font: 500 11px/1 var(--mono); letter-spacing: 0.08em; text-transform: uppercase; color: var(--hint);
  display: flex; align-items: center; gap: 6px;
}
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 2px; flex: none; }
.dot-in { background: var(--in); } .dot-out { background: var(--out); } .dot-profit { background: var(--profit); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

/* ===== Layout ===== */
#app { padding: calc(var(--safe-top) + 8px) 14px calc(var(--safe-bottom) + 84px); max-width: 640px; margin: 0 auto; }
.tab { display: flex; flex-direction: column; gap: 12px; }

.hero { padding: 14px 4px 4px; }
.hero-amount {
  font: 600 46px/1.05 var(--mono); letter-spacing: -0.03em; margin: 8px 0 10px;
  word-break: break-all;
}
.hero-amount.neg { color: var(--out); }
.hero-amount .cur { font-size: 0.55em; color: var(--hint); font-weight: 500; margin-left: 4px; }
.hero-split { display: flex; gap: 18px; color: var(--hint); font-size: 13px; }
.hero-split span { display: flex; align-items: center; gap: 6px; }
.hero-split b { font-family: var(--mono); color: var(--text); font-weight: 500; }
.hero-title { font-size: 28px; font-weight: 700; margin-top: 6px; letter-spacing: -0.02em; }

/* Les deux touches de caisse */
.keys { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.key {
  height: 60px; border-radius: var(--radius); color: #fff; font-weight: 600; font-size: 15px;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  transition: transform 0.08s ease, filter 0.12s ease;
}
.key:active { transform: scale(0.97); filter: brightness(0.92); }
.key-in { background: var(--in); } .key-out { background: var(--out); }
.key-accent { background: var(--accent); color: var(--accent-text); }
.key-soft { background: var(--card); color: var(--text); }
.key-soft svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.keys-stock { grid-template-columns: 1.5fr 1fr; }
.key-sign { font: 600 24px/1 var(--mono); }

/* Historique du stock (dans le sheet) */
.hist-list { display: flex; flex-direction: column; gap: 6px; max-height: 60vh; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }
.hist { background: var(--field); border-radius: 12px; padding: 10px 12px; display: flex; align-items: center; gap: 10px; }
.hist-main { flex: 1; min-width: 0; }
.hist-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-note { font-size: 13px; color: var(--hint); margin-top: 2px; overflow-wrap: anywhere; }
.hist-note.none { font-style: italic; }
.hist-time { font: 500 11px var(--mono); color: var(--hint); margin-top: 3px; }
.hist-delta { font: 600 17px var(--mono); letter-spacing: -0.02em; white-space: nowrap; display: flex; align-items: center; gap: 7px; }
.hist-delta small { font: 500 11px var(--mono); color: var(--hint); }
.sheet .chips { margin-bottom: 12px; }
.sheet .empty { background: var(--field); }

/* Sélecteur de période */
.seg { display: flex; background: var(--card); border-radius: 12px; padding: 3px; gap: 2px; }
.seg button {
  flex: 1; height: 34px; border-radius: 9px; font-weight: 500; color: var(--hint); font-size: 14px;
  transition: background 0.15s ease, color 0.15s ease;
}
.seg button.active { background: var(--field); color: var(--text); font-weight: 600; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06); }
:root[data-theme="dark"] .seg button.active { background: var(--field); }

.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tile { background: var(--card); border-radius: 14px; padding: 12px 12px 10px; min-width: 0; }
.tile-value { font: 600 17px/1.2 var(--mono); margin-top: 8px; letter-spacing: -0.02em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile-value.neg { color: var(--out); }

.card { background: var(--card); border-radius: var(--radius); padding: 14px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 8px; }
.card-title { font-weight: 600; font-size: 15px; }
.card-sub { font: 500 12px var(--mono); color: var(--hint); }
.card-text { margin: 8px 0 12px; color: var(--hint); font-size: 14px; }
.card-text.small { font-size: 12.5px; margin: 10px 0 0; }
.legend { display: flex; gap: 12px; font-size: 12px; color: var(--hint); }
.chart-box { position: relative; height: 190px; }
.chart-empty { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: var(--hint); font-size: 13px; text-align: center; padding: 20px; }

.kv { display: flex; justify-content: space-between; gap: 12px; padding: 9px 0; border-top: 1px solid var(--sep); font-size: 14px; }
.kv:first-of-type { border-top: 0; }
.kv span { color: var(--hint); }
.kv b { text-align: right; word-break: break-all; }
/* Gestion des accès (onglet Plus) */
.access-row { display: flex; align-items: center; gap: 10px; padding: 10px 0; border-top: 1px solid var(--sep); }
.access-row:first-child { border-top: 0; }
.access-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.access-main b { font: 600 15px var(--mono); letter-spacing: -0.01em; }
.access-name { font-size: 12.5px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.access-tag { flex: none; font-size: 11px; font-weight: 600; color: var(--hint); background: var(--field); border-radius: 8px; padding: 5px 8px; }
.access-del { flex: none; width: 36px; height: 36px; border: 0; border-radius: 10px; background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); font-size: 15px; cursor: pointer; }
.access-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.access-form .btn { grid-column: 1 / -1; }
.status-line { margin-top: 10px; font-size: 13px; color: var(--hint); display: flex; align-items: center; gap: 6px; }
.status-line.ok { color: var(--in); }

/* ===== Journal des mouvements (le cahier de caisse) ===== */
.list-head { display: flex; justify-content: space-between; align-items: baseline; padding: 4px 4px 0; }
.journal { display: flex; flex-direction: column; gap: 6px; }
.day-head { font: 500 11px/1 var(--mono); letter-spacing: 0.08em; text-transform: uppercase; color: var(--hint); padding: 12px 4px 4px; display: flex; justify-content: space-between; }
.day-head b { font-weight: 500; letter-spacing: 0; text-transform: none; }
.tx {
  width: 100%; background: var(--card); border-radius: 12px; padding: 11px 12px;
  display: flex; align-items: center; gap: 8px; text-align: left; min-height: 52px;
}
.tx:active { filter: brightness(0.96); }
.tx-main { min-width: 0; flex: 0 1 auto; }
.tx-reason { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-reason.none { color: var(--hint); font-style: italic; font-weight: 400; }
.tx-time { font: 500 11px var(--mono); color: var(--hint); margin-top: 2px; }
.leader { flex: 1 1 auto; border-bottom: 2px dotted var(--sep); min-width: 16px; margin: 0 2px; transform: translateY(2px); }
.tx-amount { font: 600 15px var(--mono); letter-spacing: -0.02em; white-space: nowrap; display: flex; align-items: center; gap: 7px; }
.empty { text-align: center; color: var(--hint); padding: 28px 16px; font-size: 14px; background: var(--card); border-radius: var(--radius); }
.empty b { display: block; color: var(--text); margin-bottom: 4px; }

/* ===== Stock ===== */
.hero-stock { padding-bottom: 8px; }
.stock-stats { display: flex; gap: 22px; margin-top: 10px; }
.stock-stats div { display: flex; flex-direction: column; }
.stock-stats b { font: 600 30px/1 var(--mono); letter-spacing: -0.03em; }
.stock-stats span { font-size: 12px; color: var(--hint); margin-top: 4px; }
.stat-out b { color: var(--out); } .stat-low b { color: var(--warn); }
.stat-out.zero b, .stat-low.zero b { color: var(--text); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; height: 34px; padding: 0 14px; border-radius: 17px; background: var(--card); color: var(--hint); font-weight: 500; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.chip b { font-family: var(--mono); font-weight: 500; }
.chip.active { background: var(--text); color: var(--bg); }
.chip.active b { color: inherit; }

.items { display: flex; flex-direction: column; gap: 8px; }
.item { background: var(--card); border-radius: var(--radius); padding: 12px 12px 10px; }
.item-top { display: flex; align-items: flex-start; gap: 10px; }
.item-name { font-weight: 600; font-size: 16px; flex: 1; min-width: 0; text-align: left; overflow-wrap: anywhere; }
.item-name small { display: block; font: 500 11px var(--mono); color: var(--hint); margin-top: 3px; font-weight: 400; }
.badge { flex: none; font: 600 11px/1 var(--sans); padding: 5px 8px; border-radius: 7px; display: inline-flex; align-items: center; gap: 4px; letter-spacing: 0.02em; }
.badge-out { background: color-mix(in srgb, var(--out) 16%, transparent); color: var(--out); }
.badge-low { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge-ok { background: color-mix(in srgb, var(--in) 14%, transparent); color: var(--in); }
.item-bottom { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.item-qty { font: 600 30px/1 var(--mono); letter-spacing: -0.03em; min-width: 54px; }
.item-qty.zero { color: var(--out); }
.item-qty small { font: 500 11px var(--mono); color: var(--hint); letter-spacing: 0.06em; text-transform: uppercase; margin-left: 4px; }
.item-actions { margin-left: auto; display: flex; gap: 8px; }
.qbtn { height: 40px; min-width: 64px; padding: 0 12px; border-radius: 11px; font: 600 15px var(--sans); display: flex; align-items: center; gap: 5px; justify-content: center; }
.qbtn:active { filter: brightness(0.92); transform: scale(0.97); }
.qbtn:disabled { opacity: 0.35; pointer-events: none; }
.qbtn-minus { background: color-mix(in srgb, var(--out) 14%, transparent); color: var(--out); }
.qbtn-plus { background: color-mix(in srgb, var(--in) 14%, transparent); color: var(--in); }
.qbtn .s { font: 600 18px/1 var(--mono); }
.item-menu { width: 36px; height: 36px; border-radius: 10px; color: var(--hint); font-size: 20px; line-height: 1; flex: none; display: flex; align-items: center; justify-content: center; }

/* ===== Boutons & champs ===== */
.btn { height: 48px; padding: 0 18px; border-radius: 13px; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 8px; background: var(--field); }
.btn:active { filter: brightness(0.92); }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-block { width: 100%; }
.btn-small { height: 36px; padding: 0 14px; border-radius: 10px; background: var(--accent); color: var(--accent-text); font-size: 14px; flex: none; }
.btn-danger { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }
.btn-in { background: var(--in); color: #fff; } .btn-out { background: var(--out); color: #fff; }
.btn:disabled { opacity: 0.55; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font: 500 11px/1 var(--mono); letter-spacing: 0.08em; text-transform: uppercase; color: var(--hint); }
.input {
  height: 48px; width: 100%; border-radius: 12px; background: var(--field); border: 1.5px solid transparent; padding: 0 14px;
  outline: none; -webkit-appearance: none; appearance: none;
}
.input:focus { border-color: var(--accent); }
.input.big { font: 600 34px/1 var(--mono); height: 64px; letter-spacing: -0.02em; text-align: right; padding-right: 60px; }
.amount-wrap { position: relative; }
.amount-wrap .cur { position: absolute; right: 16px; top: 50%; transform: translateY(-50%); font: 500 18px var(--mono); color: var(--hint); pointer-events: none; }
input[type="date"].input { min-width: 0; }
.chips-inline { display: flex; gap: 6px; flex-wrap: wrap; }
.chips-inline button { height: 30px; padding: 0 11px; border-radius: 15px; background: var(--field); font-size: 13px; font-weight: 500; }
.chips-inline button.active { background: var(--text); color: var(--bg); }
.typeseg { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; background: var(--field); padding: 4px; border-radius: 13px; }
.typeseg button { height: 40px; border-radius: 10px; font-weight: 600; color: var(--hint); }
.typeseg button.active-in { background: var(--in); color: #fff; }
.typeseg button.active-out { background: var(--out); color: #fff; }
.preview { font: 500 13px var(--mono); color: var(--hint); text-align: right; }
.preview b { color: var(--text); }
.preview.bad b { color: var(--out); }

/* ===== Bottom sheet ===== */
.overlay { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.42); z-index: 40; animation: fade 0.18s ease; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50; background: var(--card);
  border-radius: 22px 22px 0 0; padding: 10px 16px calc(var(--safe-bottom) + 16px);
  max-height: 92vh; overflow-y: auto; animation: up 0.22s cubic-bezier(0.2, 0.8, 0.2, 1);
  max-width: 640px; margin: 0 auto;
}
.sheet::before { content: ""; display: block; width: 36px; height: 4px; border-radius: 2px; background: var(--sep); margin: 0 auto 12px; }
.sheet-title { font-size: 19px; font-weight: 700; letter-spacing: -0.01em; margin: 0 0 14px; }
.sheet-title small { display: block; font: 500 12px var(--mono); color: var(--hint); margin-top: 3px; letter-spacing: 0; }
.sheet form { display: flex; flex-direction: column; gap: 14px; }
.sheet-actions { display: flex; gap: 10px; margin-top: 4px; }
.sheet-actions .btn { flex: 1; }
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* ===== Barre d'onglets ===== */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; background: var(--card); border-top: 1px solid var(--sep);
  padding: 6px 8px calc(var(--safe-bottom) + 6px);
}
.tabbar button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; color: var(--hint); font-size: 11px; font-weight: 500; padding: 4px 0; border-radius: 10px; }
.tabbar button.active { color: var(--accent); }
.tabbar svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ===== Bannière, toast, gate ===== */
.banner { background: var(--card); border-radius: 14px; padding: 12px 12px 12px 14px; display: flex; align-items: center; gap: 10px; border-left: 4px solid var(--accent); }
.banner-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
.banner-body span { color: var(--hint); }
.banner-close { width: 32px; height: 32px; color: var(--hint); font-size: 22px; flex: none; }

.toast { position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 92px); transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500; max-width: 90vw; text-align: center; animation: up 0.2s ease; }
.toast.err { background: var(--out); color: #fff; }

.gate { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; }
.gate-card { background: var(--card); border-radius: 20px; padding: 28px 22px; max-width: 360px; width: 100%; text-align: center; display: flex; flex-direction: column; gap: 10px; align-items: center; }
.gate-card h1 { font-size: 22px; margin: 0; letter-spacing: -0.01em; }
.gate-text { color: var(--hint); margin: 0; font-size: 14px; }
.gate-id { font: 600 20px var(--mono); background: var(--field); padding: 10px 16px; border-radius: 12px; user-select: all; -webkit-user-select: all; }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--sep); border-top-color: var(--accent); animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
