:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --text: #0f172a;
    --muted: #64748b;
    --border: #e2e8f0;
    --primary: #1d4ed8;
    --primary-ink: #ffffff;
    --accent: #ea580c;
    --danger: #dc2626;
    --ok: #15803d;
    --ok-bg: #dcfce7;
    --warn: #b45309;
    --warn-bg: #fef3c7;
    --danger-bg: #fee2e2;
    --radius: 12px;
    --tabbar-h: 64px;
    color-scheme: light;
}

:root[data-theme="dark"] {
    --bg: #0b1120;
    --surface: #151d31;
    --text: #e5e9f5;
    --muted: #94a3b8;
    --border: #263150;
    --primary: #3b82f6;
    --accent: #fb923c;
    --ok: #4ade80;
    --ok-bg: #123524;
    --warn: #fbbf24;
    --warn-bg: #3a2c0b;
    --danger: #f87171;
    --danger-bg: #3b1515;
    color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

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

body {
    margin: 0;
    font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.45;
    padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
}

a { color: var(--primary); }

h1 { font-size: 1.4rem; margin: 0; }
h2 { font-size: 1.05rem; margin: 1.2rem 0 .5rem; }

.topbar {
    position: sticky; top: 0; z-index: 10;
    background: var(--primary); color: #fff;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand { color: #fff; text-decoration: none; display: inline-flex; align-items: center; gap: 8px; font-weight: 700; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-user { display: flex; align-items: center; gap: 10px; font-size: .85rem; }
.topbar-name { max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-user form { margin: 0; }
.btn-logout, .btn-theme, .btn-install {
    background: rgba(255, 255, 255, .18); color: #fff; border: 1px solid rgba(255, 255, 255, .45);
    border-radius: 8px; padding: 6px 10px; font: inherit; font-size: .8rem; cursor: pointer;
}
.btn-theme { display: inline-flex; align-items: center; justify-content: center; padding: 7px; min-width: 34px; min-height: 34px; }
.btn-install { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.offline-badge {
    background: rgba(0, 0, 0, .22); color: #fff; border: 1px solid rgba(255, 255, 255, .35);
    border-radius: 999px; padding: 4px 9px; font-size: .72rem; font-weight: 700; white-space: nowrap;
}
.theme-icon-sun { display: none; }
:root[data-theme="dark"] .theme-icon-moon { display: none; }
:root[data-theme="dark"] .theme-icon-sun { display: block; }
.login-card { max-width: 420px; margin: 8vh auto; }
.login-card h1 { margin-bottom: 6px; }
.login-hint { color: var(--muted); margin: 0 0 18px; }
.check-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 8px; }
.check-grid label {
    display: flex; align-items: flex-start; gap: 8px; font-weight: 500;
    background: var(--bg); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px;
}
.check-grid input { width: auto; min-height: auto; margin: 3px 0 0; }
.check-label { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.check-label input { width: auto; min-height: auto; }
.check-grid small { display: block; font-weight: 400; }
.inline-form { display: inline; }
.inline-form form { margin: 0; }

.container { max-width: 760px; margin: 0 auto; padding: 16px; }

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

.muted { color: var(--muted); }
small.muted { display: block; }

/* Boutons */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 10px 16px; border-radius: 10px; border: 1px solid var(--border);
    background: var(--surface); color: var(--text); font: inherit; font-weight: 600;
    text-decoration: none; cursor: pointer; min-height: 44px;
}
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-danger { color: var(--danger); }
.btn-small { padding: 6px 10px; min-height: 36px; font-size: .9rem; }

.quick-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }

.install-card {
    display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
    background: linear-gradient(135deg, var(--surface), var(--bg));
    border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: 0 10px 30px rgba(15, 23, 42, .07);
    padding: 14px; margin-bottom: 16px;
}
.install-icon {
    width: 44px; height: 44px; flex: none; border-radius: 12px;
    background: var(--primary); color: var(--primary-ink);
    display: grid; place-items: center;
}
.install-copy { flex: 1 1 220px; }
.install-copy h2 { margin: 0 0 3px; font-size: 1rem; }
.install-copy p { margin: 0; color: var(--muted); font-size: .88rem; }
.install-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.install-help {
    position: fixed; left: 16px; right: 16px; z-index: 40;
    bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom));
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: 0 14px 36px rgba(15, 23, 42, .2);
    padding: 14px; display: flex; align-items: flex-start; gap: 12px;
}
.install-help strong { font-size: .95rem; }
.install-help p { margin: 4px 0 0; color: var(--muted); font-size: .88rem; }
.install-help .btn { flex: none; }

/* Listes */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.list-item {
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
    padding: 12px 14px; color: inherit; text-decoration: none;
}
.row-actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.row-actions form { margin: 0; }

.card-link {
    display: block; background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 10px 14px; margin-bottom: 8px; color: inherit; text-decoration: none;
}

.badge {
    display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: .85rem; font-weight: 700;
    background: var(--border); color: var(--text); white-space: nowrap;
}
.badge-ok { background: var(--ok-bg); color: var(--ok); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }

.grid-2 { display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 640px) { .grid-2 { grid-template-columns: 1fr 1fr; } }

.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.offline-card { padding: 44px 16px; }
.offline-card .install-icon { margin: 0 auto 14px; }
.offline-card h1 { margin-bottom: 6px; }
.offline-card p { margin: 0 0 18px; }

/* Messages */
.flash { border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px; border: 1px solid var(--border); background: var(--surface); }
.flash ul { margin: 0; padding-left: 18px; }
.flash-success { background: var(--ok-bg); border-color: var(--ok); color: var(--ok); }
.flash-error { background: var(--danger-bg); border-color: var(--danger); color: var(--danger); }

/* Formulaires */
.form { display: grid; gap: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.form label { display: grid; gap: 6px; font-weight: 600; font-size: .95rem; }
input, select, textarea {
    width: 100%; padding: 10px 12px; font: inherit; font-weight: 400;
    border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--text); min-height: 44px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 1px; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; align-items: center; }
.form-actions form { margin: 0; }

.lines { border: 1px dashed var(--border); border-radius: var(--radius); padding: 12px; display: grid; gap: 10px; }
.lines legend { font-weight: 700; padding: 0 6px; }
.line { display: grid; grid-template-columns: 1fr 96px auto; gap: 8px; margin-bottom: 8px; align-items: center; }

/* Détails */
.meta { display: grid; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin: 0 0 14px; }
.meta div { display: flex; justify-content: space-between; gap: 12px; }
.meta dt { color: var(--muted); }
.meta dd { margin: 0; font-weight: 600; text-align: right; }

.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; white-space: nowrap; }

/* Signature */
.signature-block { margin-top: 18px; }
.signature-img { display: block; max-width: 100%; width: 320px; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); }
.pad { position: relative; }
.pad-canvas {
    display: block; width: 100%; height: 220px; background: #fff; border: 2px dashed var(--border);
    border-radius: var(--radius); touch-action: none; cursor: crosshair;
}
.pad-label { position: absolute; left: 12px; top: 8px; font-size: .8rem; color: #94a3b8; pointer-events: none; }
.pad-clear { position: absolute; right: 8px; top: 8px; }
.pad-error { color: var(--danger); margin: 0; font-weight: 600; }

/* Barre d'onglets */
.tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    display: grid; grid-auto-flow: column; grid-auto-columns: 1fr;
    background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom); min-height: var(--tabbar-h);
}
.tabbar a {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    color: var(--muted); text-decoration: none; font-size: .75rem; font-weight: 600;
}
.tabbar a.active { color: var(--primary); }

@media (max-width: 480px) {
    .topbar-actions { gap: 6px; }
    .topbar-name { display: none; }
    .btn-install span { display: none; }
    .btn-install { padding: 7px; }
    .install-card { padding: 12px; }
    .install-actions { width: 100%; }
    .install-actions .btn { flex: 1; }
}

.print-only { display: none; }

@media print {
    .topbar, .tabbar, .no-print, .flash { display: none !important; }
    .print-only { display: block; }
    body { background: #fff; color: #000; padding: 0; }
    .container { max-width: none; padding: 0; }
    .meta, .table { border-color: #999; }
}
