:root {
  color-scheme: light dark;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --danger: #dc2626;
  --over: #dc2626;
  --under: #0284c7;
  --ok: #16a34a;
  --grid: #e2e8f0;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 4px 16px rgba(15, 23, 42, .05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #131c31;
    --surface-2: #1b2540;
    --border: #263149;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --primary: #3b82f6;
    --danger: #ef4444;
    --over: #f87171;
    --under: #38bdf8;
    --ok: #4ade80;
    --grid: #263149;
    --shadow: none;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }
.grow { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }

/* ------------------------------------------------------------- connexion */
.login {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 22px;
  width: min(360px, 100%);
  text-align: center;
}
.login-logo { font-size: 40px; }
.login-card h1 { margin: 6px 0 2px; font-size: 21px; }
.login-card p { margin: 0 0 18px; }
.login-card input { width: 100%; margin-bottom: 12px; }

/* --------------------------------------------------------------- layout */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.topbar-title { display: flex; align-items: center; gap: 8px; font-size: 17px; }
.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: var(--border);
  flex: none;
}
.dot.live { background: var(--ok); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 22%, transparent); animation: pulse 1.8s infinite; }
@keyframes pulse { 50% { opacity: .45; } }

main {
  padding: 14px 14px calc(84px + env(safe-area-inset-bottom));
  max-width: 780px;
  margin: 0 auto;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 { margin: 0 0 10px; font-size: 15px; letter-spacing: .01em; }
.card > p.small { margin: -6px 0 12px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

/* ------------------------------------------------------------ contrôles */
input, select, textarea, button { font: inherit; font-size: 16px; }

input[type=text], input[type=number], input[type=password], input[type=date], select {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  min-height: 44px;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
/* Champ que la dictée n'a pas su remplir. */
input.needs, select.needs { outline: 2px solid var(--over); outline-offset: 1px; }

label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
label input, label select { margin-top: 5px; }

.btn {
  padding: 11px 15px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  font-size: 15px;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); font-weight: 600; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.icon-btn {
  width: 40px; height: 40px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}
.mini-select { width: auto; min-height: 36px; padding: 6px 10px; font-size: 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chips.tight { margin-bottom: 14px; }
.chip {
  padding: 9px 13px;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}
.chip .swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.chip.selected { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary); font-weight: 600; }

.duration-row { display: flex; align-items: center; gap: 8px; margin-top: 5px; }
.duration-row input { width: 90px; }
.duration-row span { color: var(--muted); font-size: 14px; }

/* Champ texte avec bouton de dictée intégré. */
.mic-field { position: relative; }
.mic-field input { padding-right: 50px; }
.mic-btn {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.mic-btn:hover { background: var(--border); }
.timer-idle .mic-field { margin-bottom: 10px; }
.mic-btn.listening {
  background: var(--danger);
  color: #fff;
  animation: listening 1.2s ease-in-out infinite;
}
@keyframes listening {
  50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--danger) 25%, transparent); }
}

/* Dictée d'une saisie complète. */
.dictate { margin-bottom: 16px; }
.dictate #btn-dictate.listening {
  background: var(--danger);
  border-color: var(--danger);
  animation: listening 1.2s ease-in-out infinite;
}
.dictate-hint { margin: 7px 0 0; }
.dictate-result {
  margin: 10px 0 0;
  padding: 9px 11px;
  border-radius: 9px;
  font-size: 13px;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.dictate-result.heard { font-style: italic; color: var(--muted); }
.dictate-result.ok { border-color: color-mix(in srgb, var(--ok) 55%, var(--border)); }
.dictate-result.partial { border-color: color-mix(in srgb, var(--over) 55%, var(--border)); }

.inline-form { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.inline-form > * { flex: 1 1 120px; width: auto; }
.inline-form .btn { flex: 0 0 46px; }

/* --------------------------------------------------------------- onglets */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 30;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  border: 0;
  background: none;
  color: var(--muted);
  padding: 9px 4px 10px;
  font-size: 11px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  cursor: pointer;
}
.tab span { font-size: 19px; line-height: 1; }
.tab.active { color: var(--primary); font-weight: 600; }

.segmented {
  display: grid;
  grid-auto-flow: column;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 12px;
}
.seg {
  border: 0;
  border-radius: 9px;
  background: none;
  color: var(--muted);
  padding: 9px 4px;
  min-height: 40px;
  font-size: 14px;
  cursor: pointer;
}
.seg.active { background: var(--primary); color: var(--primary-text); font-weight: 600; }

.period-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
  font-size: 14px;
}

/* -------------------------------------------------------------- minuteur */
.timer-clock {
  font-size: 42px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
  text-align: center;
  margin: 12px 0 16px;
}
.timer-running-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  color: #fff;
  background: var(--muted);
}

/* --------------------------------------------------------------- saisies */
.entries { display: flex; flex-direction: column; }
.entry {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 0;
  border-top: 1px solid var(--border);
}
.entry:first-child { border-top: 0; }
.entry .bar { width: 4px; align-self: stretch; border-radius: 2px; flex: none; }
.entry .main { flex: 1; min-width: 0; }
.entry .title { font-size: 14px; }
.entry .sub { font-size: 12px; color: var(--muted); }
.entry .dur { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 14px; white-space: nowrap; }
.entry .acts { display: flex; gap: 4px; }
.entry .acts button {
  border: 0; background: none; color: var(--muted);
  font-size: 15px; cursor: pointer; padding: 6px; min-width: 32px;
}
.day-head {
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-transform: capitalize;
  padding: 12px 0 4px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: space-between;
}
.day-head:first-child { border-top: 0; padding-top: 0; }

/* ----------------------------------------------------------------- bilan */
.total-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; text-align: center; }
.total-grid span { display: block; margin-bottom: 3px; }
.total-grid strong { font-size: 21px; font-variant-numeric: tabular-nums; }
.delta.over { color: var(--over); }
.delta.under { color: var(--under); }
.delta.ok { color: var(--ok); }
#total-context { margin: 12px 0 0; text-align: center; }

.alert-card { border-color: color-mix(in srgb, var(--over) 45%, var(--border)); }
.alert-card h2 { color: var(--over); }
.alert-line { margin: 0 0 8px; font-size: 14px; }
.alert-line:last-child { margin-bottom: 0; }
.alert-line b { font-variant-numeric: tabular-nums; }

.fac-rows { display: flex; flex-direction: column; gap: 16px; }
.fac-row .fac-head {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  margin-bottom: 7px;
}
.fac-row .fac-name { font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 7px; }
.fac-row .fac-name .swatch { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.fac-row .fac-pct { font-size: 12px; color: var(--muted); font-weight: 400; }
.fac-row .fac-delta { font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

.track {
  position: relative;
  height: 24px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
}
.track .budget { position: absolute; inset: 0 auto 0 0; background: var(--grid); }
.track .actual { position: absolute; inset: 0 auto 0 0; opacity: .95; border-radius: 0 4px 4px 0; }
.track .mark { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--text); opacity: .75; }
.fac-figures { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 5px; font-variant-numeric: tabular-nums; }
.legend-note { margin: 14px 0 0; }

/* ------------------------------------------------------------ graphiques */
.chart-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.chart-wrap svg { display: block; }
.legend { display: flex; flex-wrap: wrap; gap: 10px 14px; margin-top: 10px; font-size: 12.5px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.legend-item .swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }

/* -------------------------------------------------------------- réglages */
.fac-block {
  padding: 10px 0;
  border-top: 1px solid var(--border);
}
.fac-block:first-child { border-top: 0; }
.fac-block.off { opacity: .45; }
.fac-aliases { margin-top: 6px; min-height: 38px !important; padding: 7px 10px !important; font-size: 13px !important; }

.fac-edit {
  display: grid;
  grid-template-columns: 1fr 62px 62px 44px 40px;
  gap: 6px;
  align-items: center;
}
.fac-edit input[type=text], .fac-edit input[type=number] { min-height: 40px; padding: 8px; font-size: 14px; }
.fac-edit input[type=color] { width: 40px; height: 40px; padding: 2px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); }
.fac-edit .del { border: 0; background: none; color: var(--muted); font-size: 16px; cursor: pointer; }
.version { margin-top: 4px; }

.error { color: var(--danger); font-size: 13px; margin: 10px 0 0; }

/* ----------------------------------------------------------------- toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 14px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25);
  max-width: 90vw;
}

@media (min-width: 640px) {
  .fac-edit { grid-template-columns: 1fr 90px 80px 48px 40px; }
}
