/* ============ Design tokens ============ */
:root {
  --bg: #fafaf9;
  --surface: #ffffff;
  --surface-2: #f4f4f2;
  --text: #18181b;
  --muted: #6b6b73;
  --border: #e7e5e4;
  --border-strong: #d0cdca;
  --btn-bg: #18181b;
  --btn-text: #fafafa;
  --focus: #2563eb;
  --danger: #dc2626;
  --success: #15803d;
  --success-bg: #dcfce7;
  --error-bg: #fee2e2;

  --top: #0369a1;
  --top-bg: #e0f2fe;
  --middle: #b45309;
  --middle-bg: #fef3c7;
  --bottom: #047857;
  --bottom-bg: #d1fae5;

  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.04), 0 10px 30px rgb(0 0 0 / 0.08);
  --font: ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #0e0e10;
  --surface: #17171a;
  --surface-2: #1f1f23;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --border: #2a2a2e;
  --border-strong: #3f3f46;
  --btn-bg: #f4f4f5;
  --btn-text: #18181b;
  --focus: #60a5fa;
  --danger: #f87171;
  --success: #4ade80;
  --success-bg: #0f2a1a;
  --error-bg: #3a1414;

  --top: #38bdf8;
  --top-bg: #0c2a3b;
  --middle: #fbbf24;
  --middle-bg: #33260a;
  --bottom: #34d399;
  --bottom-bg: #0b2e22;

  --shadow: 0 1px 2px rgb(0 0 0 / 0.4), 0 10px 30px rgb(0 0 0 / 0.5);
  color-scheme: dark;
}

/* Sans JavaScript : on suit le thème du système */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #0e0e10;
    --surface: #17171a;
    --surface-2: #1f1f23;
    --text: #f4f4f5;
    --muted: #a1a1aa;
    --border: #2a2a2e;
    --border-strong: #3f3f46;
    --btn-bg: #f4f4f5;
    --btn-text: #18181b;
    color-scheme: dark;
  }
}

/* Couleurs par niveau de tunnel */
.f-top { --fc: var(--top); --fc-bg: var(--top-bg); }
.f-middle { --fc: var(--middle); --fc-bg: var(--middle-bg); }
.f-bottom { --fc: var(--bottom); --fc-bg: var(--bottom-bg); }

/* ============ Base ============ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; line-height: 1.2; letter-spacing: -0.01em; }
h1 { font-size: 26px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 600; }
p { margin: 0; }
a { color: inherit; }
.muted { color: var(--muted); }
.link { text-decoration: underline; text-underline-offset: 3px; text-decoration-color: var(--border-strong); }
.link:hover { text-decoration-color: currentColor; }
.center { text-align: center; }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; border-radius: 8px; }

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

/* ============ Header ============ */
.topbar { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--border); }
.topbar-inner { display: flex; align-items: center; gap: 20px; max-width: 1400px; margin: 0 auto; padding: 0 24px; height: 56px; }
.brand { font-weight: 650; text-decoration: none; letter-spacing: -0.01em; }
.nav { display: flex; gap: 4px; flex: 1; }
.nav a, .subnav a { text-decoration: none; color: var(--muted); padding: 6px 10px; border-radius: 8px; font-size: 14px; }
.nav a:hover, .subnav a:hover { color: var(--text); background: var(--surface-2); }
.nav a.is-active, .subnav a.is-active { color: var(--text); background: var(--surface-2); }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.account-link { color: var(--muted); text-decoration: none; font-size: 14px; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.account-link:hover { color: var(--text); }
.subnav { border-top: 1px solid var(--border); }
.subnav .topbar-inner { height: 44px; gap: 4px; }
.floating-toggle { position: fixed; top: 16px; right: 16px; z-index: 30; }

/* ============ Layout ============ */
.main { min-height: calc(100vh - 57px); }
.page { max-width: 1400px; margin: 0 auto; padding: 32px 24px 64px; display: flex; flex-direction: column; gap: 20px; }
.page.narrow { max-width: 760px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.crumb a { color: var(--muted); text-decoration: none; font-size: 14px; }
.crumb a:hover { color: var(--text); }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.error-code { font-size: 64px; font-weight: 650; letter-spacing: -0.03em; }

/* ============ Boutons & champs ============ */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--btn-bg); color: var(--btn-text);
  border: 1px solid var(--btn-bg); border-radius: 10px;
  padding: 9px 16px; font: inherit; font-weight: 550; font-size: 14px;
  text-decoration: none; cursor: pointer; transition: opacity 0.15s, background 0.15s, border-color 0.15s;
}
.btn:hover { opacity: 0.88; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border-strong); }
.btn.ghost:hover { background: var(--surface-2); opacity: 1; }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger); color: #fff; opacity: 1; }
.btn.sm { padding: 6px 12px; font-size: 13px; }
.btn.block { width: 100%; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: 10px; cursor: pointer;
  background: transparent; color: var(--text); border: 1px solid var(--border); text-decoration: none;
}
.icon-btn:hover { background: var(--surface-2); }
[data-theme='dark'] .i-moon, :root:not([data-theme='dark']) .i-sun { display: none; }

.form { display: flex; flex-direction: column; gap: 16px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field label, .label { font-size: 13px; font-weight: 550; }
.hint { font-size: 13px; color: var(--muted); }
input[type='text'], input[type='email'], input[type='password'], input[type='url'], input[type='date'], select, textarea {
  width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 10px; padding: 10px 12px;
}
textarea { resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
input::placeholder, textarea::placeholder { color: var(--muted); opacity: 0.7; }
.field.check label, .radios label, .check-item { display: flex; align-items: center; gap: 8px; font-weight: 450; font-size: 14px; cursor: pointer; }
input[type='checkbox'], input[type='radio'] { width: 16px; height: 16px; accent-color: var(--text); margin: 0; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.actions form { margin: 0; }

fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px 20px; margin: 0; display: flex; flex-direction: column; gap: 16px; background: var(--surface); min-width: 0; }
legend { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); padding: 0 8px; margin-left: -8px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.radios { display: flex; flex-wrap: wrap; gap: 8px 20px; }
.client-picker { display: none; flex-direction: column; gap: 8px; padding: 12px 14px; border: 1px dashed var(--border-strong); border-radius: 10px; max-height: 220px; overflow: auto; }
.form-content:has(#aud-selected:checked) .client-picker { display: flex; }
.sticky-actions { position: sticky; bottom: 0; background: linear-gradient(to top, var(--bg) 70%, transparent); padding: 16px 0 8px; }

/* Choix du niveau de tunnel */
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.seg label { position: relative; cursor: pointer; }
.seg input { position: absolute; opacity: 0; inset: 0; }
.seg label > span { display: flex; flex-direction: column; gap: 2px; height: 100%; padding: 10px 12px; border: 1px solid var(--border-strong); border-radius: 10px; font-size: 14px; font-weight: 550; background: var(--surface); }
.seg label > span small { font-weight: 450; color: var(--muted); font-size: 12px; }
.seg label > span .dot { margin-bottom: 4px; }
.seg input:checked + span { border-color: var(--fc); background: var(--fc-bg); box-shadow: inset 0 0 0 1px var(--fc); }
.seg input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 2px; }

/* ============ Messages ============ */
.flash { max-width: 1400px; margin: 16px auto 0; padding: 12px 16px; border-radius: 10px; font-size: 14px; border: 1px solid transparent; word-break: break-word; }
.page .flash { margin: 0; }
.flash-success { background: var(--success-bg); color: var(--success); border-color: color-mix(in srgb, var(--success) 30%, transparent); }
.flash-error { background: var(--error-bg); color: var(--danger); border-color: color-mix(in srgb, var(--danger) 30%, transparent); }
.main > .flash { width: calc(100% - 48px); }
.notice { padding: 14px 16px; border: 1px dashed var(--border-strong); border-radius: var(--radius); color: var(--muted); }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); letter-spacing: 0.02em; }
.badge-ok { color: var(--success); background: var(--success-bg); border-color: transparent; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--fc, var(--muted)); flex: none; }

/* ============ Connexion ============ */
.login { display: grid; place-items: center; min-height: 100vh; padding: 24px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 32px; display: flex; flex-direction: column; gap: 16px; box-shadow: var(--shadow); }
.login-brand { font-weight: 650; letter-spacing: -0.01em; }
.login-help { font-size: 13px; text-align: center; }
.login .flash { margin: 0; }

/* ============ Planning ============ */
.week-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.week-head h1 { margin-top: 4px; }
.week-range { display: inline-block; margin-left: 8px; font-weight: 450; color: var(--muted); font-size: 20px; }
.week-nav { display: flex; align-items: center; gap: 8px; }
.legend { display: flex; flex-wrap: wrap; gap: 8px 20px; list-style: none; margin: 0; padding: 0; font-size: 13px; color: var(--muted); }
.legend li { display: flex; align-items: center; gap: 8px; }

.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 12px; align-items: start; }
.day { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.day-head { display: flex; flex-direction: column; padding: 4px 2px 10px; border-bottom: 2px solid var(--border); }
.day.is-today .day-head { border-bottom-color: var(--text); }
.day-name { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.day.is-today .day-name { color: var(--text); font-weight: 600; }
.day-num { font-size: 24px; font-weight: 650; letter-spacing: -0.02em; }
.day-num small { font-size: 13px; font-weight: 450; color: var(--muted); letter-spacing: 0; }
.day-body { display: flex; flex-direction: column; gap: 8px; }
.empty { font-size: 13px; color: var(--muted); padding: 6px 2px; opacity: 0.8; }
.add { display: block; text-align: center; padding: 10px; border: 1px dashed var(--border-strong); border-radius: 10px; color: var(--muted); text-decoration: none; font-size: 13px; }
.add:hover { color: var(--text); border-color: var(--text); }

.card {
  display: flex; flex-direction: column; gap: 6px; min-width: 0;
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--fc);
  border-radius: 10px; padding: 12px; text-decoration: none; color: inherit;
  transition: transform 0.15s, box-shadow 0.15s, border-color 0.15s;
}
.card:hover { transform: translateY(-1px); box-shadow: var(--shadow); border-color: var(--border-strong); border-left-color: var(--fc); }
.card.is-draft { border-style: dashed; border-left-style: solid; opacity: 0.85; }
.tag { display: inline-flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--fc); }
.card-title { font-weight: 600; font-size: 14px; line-height: 1.35; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 4; -webkit-box-orient: vertical; overflow: hidden; }
.card-meta { font-size: 12px; color: var(--muted); }
.card-flags { display: flex; gap: 4px; flex-wrap: wrap; }

/* ============ Détail d'un contenu ============ */
.sheet {
  width: min(720px, calc(100vw - 24px)); max-height: min(88vh, 900px); padding: 0;
  border: 1px solid var(--border); border-radius: 16px; background: var(--surface); color: var(--text);
  box-shadow: var(--shadow); overflow: auto;
}
.sheet::backdrop { background: rgb(0 0 0 / 0.5); backdrop-filter: blur(2px); }
.sheet-body { padding: 0; }
.detail { position: relative; display: flex; flex-direction: column; gap: 22px; padding: 28px; }
.detail-page { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; }
.detail-close { position: absolute; top: 16px; right: 16px; }
.detail-head { display: flex; flex-direction: column; gap: 10px; padding-right: 44px; }
.detail-head h2 { font-size: 24px; font-weight: 650; letter-spacing: -0.02em; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; list-style: none; margin: 0; padding: 0; }
.chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2); }
.chip-label { color: var(--muted); font-size: 12px; }
.chip-funnel { background: var(--fc-bg); border-color: transparent; color: var(--fc); font-weight: 600; }
.funnel-help { font-size: 13px; color: var(--muted); }
.sec { display: flex; flex-direction: column; gap: 8px; }
.sec h3 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); font-weight: 600; }
.sec-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.prose { white-space: pre-line; overflow-wrap: anywhere; }
.prose-lead { font-size: 17px; font-weight: 550; line-height: 1.4; }
.script { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; padding: 16px; font-size: 15px; line-height: 1.65; }
.detail-foot { padding-top: 14px; border-top: 1px solid var(--border); }

/* ============ Administration ============ */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.stat { display: flex; flex-direction: column; gap: 4px; padding: 20px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); text-decoration: none; transition: border-color 0.15s; }
.stat:hover { border-color: var(--border-strong); }
.stat-num { font-size: 36px; font-weight: 650; letter-spacing: -0.03em; line-height: 1; }
.stat-label { color: var(--muted); font-size: 14px; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; padding: 12px 16px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }
.cell-actions { text-align: right; white-space: nowrap; }

.danger-zone { margin-top: 12px; padding-top: 20px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 10px; align-items: flex-start; }

.kv { display: flex; flex-direction: column; gap: 10px; margin: 0; padding: 16px 20px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.kv div { display: flex; gap: 16px; }
.kv dt { width: 80px; color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.token-box { padding: 16px 18px; border: 1px solid var(--success); background: var(--success-bg); border-radius: var(--radius); display: flex; flex-direction: column; gap: 10px; }
.token { display: block; padding: 10px 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; overflow-wrap: anywhere; user-select: all; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }

/* ============ Responsive ============ */
@media (max-width: 1040px) {
  .week { grid-template-columns: 1fr; gap: 20px; }
  .day { display: grid; grid-template-columns: 84px minmax(0, 1fr); column-gap: 16px; }
  .day-head { border-bottom: none; border-right: 2px solid var(--border); padding: 2px 12px 2px 2px; }
  .day.is-today .day-head { border-right-color: var(--text); border-bottom-color: transparent; }
  .day-body { padding-top: 2px; }
  .card-title { -webkit-line-clamp: 3; }
}

@media (max-width: 640px) {
  .topbar-inner { padding: 0 16px; gap: 10px; }
  .account-link { display: none; }
  .page { padding: 24px 16px 56px; }
  .main > .flash { width: calc(100% - 32px); }
  .grid-2, .grid-3, .seg { grid-template-columns: 1fr; }
  .week-range { display: block; margin: 4px 0 0; font-size: 16px; }
  .day { grid-template-columns: 78px minmax(0, 1fr); column-gap: 12px; }
  .day-name { font-size: 11px; letter-spacing: 0.04em; }
  .detail { padding: 22px 18px; }
  .sheet { width: 100vw; max-width: 100vw; max-height: 92vh; margin: auto 0 0; border-radius: 16px 16px 0 0; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; }
  .table tr { padding: 12px 16px; border-bottom: 1px solid var(--border); }
  .table td { border: none; padding: 3px 0; }
  .table td::before { content: attr(data-label) ' : '; color: var(--muted); font-size: 12px; }
  .cell-actions { text-align: left; padding-top: 8px !important; }
  .cell-actions::before { content: none !important; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
