:root {
    --c-bg:        #f3f5f8;
    --c-card:      #ffffff;
    --c-text:      #1a2330;
    --c-muted:     #6a7585;
    --c-primary:   #0c2740;
    --c-primary-2: #143a5e;
    --c-accent:    #f29423;
    --c-ok:        #1f8e4a;
    --c-err:       #c0392b;
    --c-border:    #dde2ea;
    --c-input-bg:  #ffffff;
    --c-input-bg2: #eef2f7;
    --r-sm: 8px; --r-md: 12px; --r-lg: 18px;
    --shadow: 0 4px 16px rgba(20,40,70,.08);
}

html[data-theme="dark"] {
    --c-bg:        #0f1620;
    --c-card:      #1a2330;
    --c-text:      #e6ecf2;
    --c-muted:     #8a96a8;
    --c-primary:   #4a7bb5;
    --c-primary-2: #5a8bc5;
    --c-accent:    #f29423;
    --c-ok:        #2bc06a;
    --c-err:       #e74c3c;
    --c-border:    #2a3a4f;
    --c-input-bg:  #243140;
    --c-input-bg2: #1f2a38;
    --shadow: 0 4px 16px rgba(0,0,0,.4);
}

html[data-theme="dark"] input,
html[data-theme="dark"] select,
html[data-theme="dark"] textarea {
    background: var(--c-input-bg);
    color: var(--c-text);
    border-color: var(--c-border);
}
html[data-theme="dark"] .dash-table th,
html[data-theme="dark"] .users-table th { background: var(--c-input-bg2); }
html[data-theme="dark"] .ocr-block { background: #1f2e44; color: #b8d1f0; }
html[data-theme="dark"] .ocr-block.ocr-done { background: #1f2e44; }
html[data-theme="dark"] .ocr-block strong { color: #e6ecf2; }
html[data-theme="dark"] .status.sent       { background: #1c3a26; color: #5fd58a; }
html[data-theme="dark"] .status.pending    { background: #3a2d10; color: #e0a96a; }
html[data-theme="dark"] .status.failed     { background: #3a1c19; color: #e87060; }
html[data-theme="dark"] .status.reimbursed { background: #1a2c52; color: #7fa7e8; }
html[data-theme="dark"] .ocr-block.ocr-pending { background: #3a2d10; color: #e0a96a; }
html[data-theme="dark"] .ocr-block.ocr-failed  { background: #3a1c19; color: #e87060; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; color: var(--c-text); background: var(--c-bg); }
body { padding-bottom: env(safe-area-inset-bottom); }

#topbar {
    display: flex; align-items: center; gap: 12px;
    padding: calc(env(safe-area-inset-top) + 10px) 14px 10px;
    background: var(--c-primary); color: #fff;
    position: sticky; top: 0; z-index: 30;
}
#topbar h1 { flex: 1; font-size: 18px; font-weight: 600; margin: 0; }
.iconbtn {
    background: none; border: 0; color: #fff; font-size: 22px;
    width: 40px; height: 40px; border-radius: 8px; cursor: pointer;
}
.iconbtn:active { background: rgba(255,255,255,.15); }

main#app {
    max-width: 560px; margin: 0 auto; padding: 16px;
}

.view { animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

/* Login */
.login-card {
    background: var(--c-card); border-radius: var(--r-lg); box-shadow: var(--shadow);
    padding: 32px 24px; text-align: center; margin-top: 14vh;
}
.login-card .logo { width: auto; height: 64px; max-width: 80%; margin: 0 auto 8px; display: block; }
.login-card h2 { margin: 12px 0 24px; font-size: 22px; }
.login-card form { text-align: left; }

label { display: block; margin: 12px 0; font-size: 14px; color: var(--c-muted); font-weight: 500; }
input[type=email], input[type=password], input[type=text], textarea {
    display: block; width: 100%; margin-top: 4px;
    border: 1px solid var(--c-border); border-radius: var(--r-sm);
    padding: 12px 14px; font-size: 16px; background: #fff; color: var(--c-text);
    font-family: inherit;
}
input:focus, textarea:focus { outline: 2px solid var(--c-primary); outline-offset: -1px; border-color: var(--c-primary); }
textarea { resize: vertical; min-height: 96px; }

.btn {
    display: block; width: 100%;
    border: 0; padding: 14px 18px; font-size: 16px; font-weight: 600;
    border-radius: var(--r-sm); cursor: pointer;
    margin-top: 12px; transition: filter .15s;
}
.btn.primary { background: var(--c-primary); color: #fff; }
.btn.primary:active { filter: brightness(1.2); }
.btn.primary:disabled { opacity: .55; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--c-primary); border: 1px solid var(--c-border); }
.btn.danger { background: var(--c-err); color: #fff; }

.form-error { color: var(--c-err); margin: 12px 0 0; font-size: 14px; }
.form-ok    { color: var(--c-ok);  margin: 12px 0 0; font-size: 14px; }
.muted { color: var(--c-muted); }

/* Capture */
.photo-picker { display: block; cursor: pointer; }
.photo-picker input[type=file] { position: absolute; left: -9999px; }
.photo-preview {
    width: 100%; aspect-ratio: 3/4; background: #e6ecf2 center/cover no-repeat;
    border: 2px dashed #b8c4d4; border-radius: var(--r-md);
    display: flex; align-items: center; justify-content: center;
    color: var(--c-muted); font-size: 16px; text-align: center; padding: 16px;
}
.photo-preview.has-image { border-style: solid; color: transparent; }
.photo-preview .compressing { color: var(--c-muted); font-style: italic; padding: 24px; }
.photo-preview.is-pdf { background: #fdf2ee; color: var(--c-err); font-size: 64px; line-height: 1; }
.photo-preview .pdf-icon { font-size: 64px; text-align: center; color: var(--c-err); }
.photo-preview .pdf-icon small { font-size: 13px; color: var(--c-muted); display: block; margin-top: 12px; word-break: break-all; max-width: 90%; }

/* Historique */
.history-search {
    width: 100%; padding: 10px 14px; font-size: 14px;
    border: 1px solid var(--c-border); border-radius: var(--r-sm);
    background: #fff; margin-bottom: 12px;
}
.history-list { display: flex; flex-direction: column; gap: 10px; margin-top: 8px; }
.history-item {
    display: grid; grid-template-columns: 64px 1fr auto; gap: 12px;
    align-items: center; background: var(--c-card);
    border-radius: var(--r-md); padding: 10px; box-shadow: var(--shadow);
}
.history-item img { width: 64px; height: 64px; object-fit: cover; border-radius: var(--r-sm); background: #ddd; }
.history-meta { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.history-meta .when { font-size: 13px; color: var(--c-muted); }
.history-meta .detail {
    font-size: 14px;
    overflow: hidden; text-overflow: ellipsis;
    display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
}
.history-meta .status { align-self: flex-start; margin-top: 2px; }
.status { display: inline-block; font-size: 12px; padding: 4px 10px; border-radius: 999px; font-weight: 600; }
.status.sent    { background: #e2f6ea; color: var(--c-ok); }
.status.pending { background: #fef4e0; color: #b07a17; }
.status.failed  { background: #fde2de; color: var(--c-err); }
.status.reimbursed { background: #e5edff; color: #1f4ec0; }

.ocr-block {
    font-size: 13px; padding: 6px 10px; border-radius: 6px;
    background: #f0f4fa; color: #2a3a55; line-height: 1.5;
}
.ocr-block.ocr-pending { background: #fef4e0; color: #b07a17; font-style: italic; }
.ocr-block.ocr-failed  { background: #fde2de; color: var(--c-err); }
.ocr-block.ocr-done    { background: #e8efff; color: #1f3a78; border-left: 3px solid var(--c-primary); }
.ocr-block strong { color: var(--c-primary); }
.history-meta .badges { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }

/* Submissions admin */
.subs-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
@media (max-width: 600px) { .subs-stats { grid-template-columns: repeat(2, 1fr); } }
.subs-stats div {
    background: var(--c-card); border-radius: var(--r-md); padding: 12px 8px;
    text-align: center; box-shadow: var(--shadow);
}
.subs-stats strong { display: block; font-size: 22px; color: var(--c-primary); }
.subs-stats small  { display: block; font-size: 11px; color: var(--c-muted); margin-top: 2px; text-transform: uppercase; }
.subs-stats div.big { background: var(--c-primary); color: #fff; }
.subs-stats div.big strong { color: #fff; font-size: 22px; }
.subs-stats div.big small  { color: #c8d6e5; }

.bulk-bar {
    position: sticky; top: 56px; z-index: 25;
    background: var(--c-primary); color: #fff; padding: 10px 14px;
    border-radius: var(--r-md); margin-bottom: 12px;
    display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
    box-shadow: var(--shadow);
}
.bulk-bar span { flex: 1; min-width: 130px; font-weight: 600; }
.bulk-bar .btn { margin: 0; }
.btn.danger { background: var(--c-err); color: #fff; }

.subs-item.selected { background: #e8efff; border: 2px solid var(--c-primary); }
.subs-checkbox {
    display: flex; align-items: center; justify-content: center;
    background: #fff; border: 2px solid var(--c-border); border-radius: 4px;
    width: 22px; height: 22px; cursor: pointer; margin-right: 6px;
}
.subs-checkbox.checked { background: var(--c-primary); border-color: var(--c-primary); color: #fff; }
.subs-checkbox.checked::after { content: '✓'; font-weight: 700; }

.subs-advanced { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; align-items: center; }
.subs-advanced input, .subs-advanced select {
    padding: 8px 10px; font-size: 13px; border: 1px solid var(--c-border);
    border-radius: 8px; background: #fff;
}
.subs-advanced input[type=search] { flex: 1; min-width: 200px; }
.subs-advanced input[type=date]   { width: auto; }
.subs-advanced .btn { padding: 8px 12px; margin: 0; }

.subs-filters { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; padding-bottom: 4px; }
.chip {
    background: var(--c-card); border: 1px solid var(--c-border); color: var(--c-text);
    padding: 8px 14px; border-radius: 999px; font-size: 13px; cursor: pointer;
    white-space: nowrap; font-weight: 500;
}
.chip.active { background: var(--c-primary); color: #fff; border-color: var(--c-primary); }

.subs-list { display: flex; flex-direction: column; gap: 10px; }
.subs-item {
    display: grid; grid-template-columns: auto 1fr auto; gap: 12px;
    align-items: start; background: var(--c-card);
    border-radius: var(--r-md); padding: 12px; box-shadow: var(--shadow);
}
.subs-item .subs-left { display: flex; align-items: center; gap: 6px; }
.subs-item img { width: 80px; height: 80px; object-fit: cover; border-radius: var(--r-sm); background: #ddd; cursor: zoom-in; }
.subs-meta .row-btns { display: flex; gap: 6px; flex-wrap: wrap; }
.subs-meta { min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.subs-meta .user-line { font-size: 15px; }
.subs-meta .user-line small { color: var(--c-muted); font-size: 12px; }
.subs-meta .when { font-size: 12px; color: var(--c-muted); }
.subs-meta .detail { font-size: 14px; }
.subs-meta .badges { display: flex; gap: 4px; flex-wrap: wrap; }
.subs-meta .reimb-info { display: flex; flex-direction: column; gap: 4px; margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--c-border); }
.reimb-note { color: var(--c-muted); font-size: 12px; font-style: italic; }
.btn-sm { padding: 6px 12px; font-size: 13px; margin-top: 4px; }

/* Photo viewer / lightbox */
.photo-viewer { position: fixed; inset: 0; background: rgba(0,0,0,.92); z-index: 250; display: flex; align-items: center; justify-content: center; padding: 16px; }
.photo-viewer img { max-width: 100%; max-height: 100%; object-fit: contain; }
.photo-viewer-close {
    position: fixed; top: 12px; right: 12px;
    background: rgba(255,255,255,.15); border: 0; color: #fff;
    width: 44px; height: 44px; border-radius: 50%; font-size: 24px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}

.history-del {
    background: none; border: 0; cursor: pointer;
    font-size: 24px; line-height: 1; color: var(--c-muted);
    width: 36px; height: 36px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    transition: background .15s, color .15s;
    padding: 0;
}
.history-del:hover, .history-del:active {
    background: #fde2de; color: var(--c-err);
}
.history-del:disabled { opacity: .4; cursor: wait; }

/* Drawer */
#drawer {
    position: fixed; inset: 0 30% 0 0; max-width: 320px; background: var(--c-primary); color: #fff;
    z-index: 60; transform: translateX(-100%); transition: transform .25s ease;
    display: flex; flex-direction: column;
    padding-top: env(safe-area-inset-top);
}
#drawer.open { transform: none; }
#drawer .drawer-head { padding: 18px 18px 16px; border-bottom: 1px solid rgba(255,255,255,.1); }
#drawer .drawer-logo { display: block; height: 42px; width: auto; margin-bottom: 12px; }
#drawer .user-id strong { display: block; font-size: 16px; }
#drawer .user-id small  { display: block; color: #c8d6e5; font-size: 13px; margin-top: 2px; }
#drawer ul { list-style: none; margin: 0; padding: 8px 0; flex: 1; }
#drawer li a { display: block; padding: 14px 20px; color: #fff; text-decoration: none; font-size: 15px; }
#drawer li a:active { background: rgba(255,255,255,.1); }
#drawer .drawer-foot { padding: 14px 20px; font-size: 12px; color: #97afc7; }
#drawer .theme-toggle {
    background: rgba(255,255,255,.1); color: #fff; border: 0;
    padding: 10px; border-radius: 6px; width: 100%; cursor: pointer;
    font-size: 13px; margin-bottom: 8px;
}
#drawer .theme-toggle:active { background: rgba(255,255,255,.18); }
#drawer .version-line { font-size: 11px; color: #97afc7; }
body.drawer-open::before {
    content: ""; position: fixed; inset: 0; background: rgba(0,0,0,.45);
    z-index: 50;
}

/* Admin */
.users-table { width: 100%; border-collapse: collapse; margin-top: 12px; font-size: 14px; }
.users-table th, .users-table td { padding: 10px 8px; border-bottom: 1px solid var(--c-border); text-align: left; }
.users-table th { background: #eef2f7; font-weight: 600; font-size: 12px; color: var(--c-muted); text-transform: uppercase; }
.users-table .actions button { padding: 6px 10px; font-size: 12px; margin-right: 4px; border-radius: 6px; border: 1px solid var(--c-border); background: #fff; cursor: pointer; }

/* Dashboard */
.dash-kpi { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 18px; }
@media (max-width: 600px) { .dash-kpi { grid-template-columns: repeat(2, 1fr); } }
.dash-kpi .k {
    background: var(--c-card); border-radius: var(--r-md); padding: 14px 10px;
    text-align: center; box-shadow: var(--shadow);
}
.dash-kpi .k strong { display: block; font-size: 20px; color: var(--c-primary); }
.dash-kpi .k small  { display: block; font-size: 11px; color: var(--c-muted); margin-top: 2px; text-transform: uppercase; }
.dash-kpi .k.accent { background: var(--c-primary); color: #fff; }
.dash-kpi .k.accent strong { color: #fff; }
.dash-kpi .k.accent small  { color: #c8d6e5; }

.dash-h3 { margin: 24px 0 8px; font-size: 14px; text-transform: uppercase; color: var(--c-muted); letter-spacing: .04em; }

.dash-chart {
    background: var(--c-card); border-radius: var(--r-md); padding: 12px; box-shadow: var(--shadow);
}
.dash-bar { display: grid; grid-template-columns: 60px 1fr 90px; gap: 8px; align-items: center; padding: 6px 0; font-size: 13px; }
.dash-bar .label { color: var(--c-muted); }
.dash-bar .track { background: #eef2f7; border-radius: 4px; height: 18px; position: relative; overflow: hidden; }
.dash-bar .fill  { background: var(--c-primary); height: 100%; border-radius: 4px; transition: width .3s; }
.dash-bar .fill.reimb { background: var(--c-ok); }
.dash-bar .amount { text-align: right; font-weight: 600; }

.dash-cats { display: flex; flex-wrap: wrap; gap: 8px; }
.dash-cat {
    background: var(--c-card); padding: 10px 14px; border-radius: var(--r-md); box-shadow: var(--shadow);
    font-size: 13px; display: flex; flex-direction: column; min-width: 130px;
}
.dash-cat .cat-name { color: var(--c-muted); text-transform: capitalize; }
.dash-cat .cat-amount { color: var(--c-primary); font-weight: 600; font-size: 16px; margin-top: 2px; }
.dash-cat .cat-count { color: var(--c-muted); font-size: 11px; }

.dash-table { width: 100%; border-collapse: collapse; font-size: 13px; background: var(--c-card); border-radius: var(--r-md); overflow: hidden; box-shadow: var(--shadow); }
.dash-table th, .dash-table td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--c-border); }
.dash-table th { background: #eef2f7; font-weight: 600; font-size: 12px; color: var(--c-muted); text-transform: uppercase; }
.dash-table td.right { text-align: right; }

/* Profile stats */
.profile-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 18px; }
@media (max-width: 600px) { .profile-stats { grid-template-columns: repeat(1, 1fr); } }
.profile-stats div {
    background: var(--c-card); border-radius: var(--r-md); padding: 14px 10px;
    text-align: center; box-shadow: var(--shadow);
}
.profile-stats strong { display: block; font-size: 22px; color: var(--c-primary); }
.profile-stats small  { display: block; font-size: 11px; color: var(--c-muted); margin-top: 2px; text-transform: uppercase; }
.profile-stats div.big { background: var(--c-primary); color: #fff; }
.profile-stats div.big strong { color: #fff; }
.profile-stats div.big small  { color: #c8d6e5; }

/* form-row3 (3 inputs alignés) */
.form-row3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.form-row3 label { margin: 0; }

/* Subnav (onglets dans une vue) */
.subnav { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 1px solid var(--c-border); overflow-x: auto; }
.subnav a {
    padding: 10px 14px; font-size: 14px; color: var(--c-muted);
    text-decoration: none; font-weight: 500; white-space: nowrap;
    border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.subnav a.active { color: var(--c-primary); border-bottom-color: var(--c-primary); }

/* Settings form */
.settings-form h3 { margin: 20px 0 6px; font-size: 14px; text-transform: uppercase; color: var(--c-muted); letter-spacing: .04em; }
.settings-form label.checkbox { display: flex; align-items: center; gap: 8px; color: var(--c-text); font-weight: 500; }
.settings-form select {
    display: block; width: 100%; margin-top: 4px;
    border: 1px solid var(--c-border); border-radius: var(--r-sm);
    padding: 12px 14px; font-size: 16px; background: #fff;
}
.settings-form .row-actions { display: flex; gap: 8px; margin-top: 16px; }
.settings-form .row-actions .btn { flex: 1; margin-top: 0; }
.hint { display: block; color: var(--c-muted); font-size: 12px; margin-top: 4px; }
.qr-block { text-align: center; padding: 16px 0; }
.qr-block svg { max-width: 240px; height: auto; border: 1px solid var(--c-border); padding: 8px; background: #fff; }

.smtp-log {
    background: #0f1620; color: #c5e2ff; padding: 12px; border-radius: 6px;
    font-size: 12px; overflow-x: auto; margin-top: 12px; max-height: 240px;
    white-space: pre-wrap; word-break: break-all;
}

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 16px; }
.modal-card { background: #fff; border-radius: var(--r-md); padding: 20px; max-width: 420px; width: 100%; box-shadow: var(--shadow); }
.modal-card h3 { margin: 0 0 12px; }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }
.modal-actions .btn { flex: 1; margin-top: 0; }

/* iOS install hint */
.ios-hint { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: flex-end; z-index: 150; padding: 16px; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.ios-hint-card { background: #fff; border-radius: var(--r-md); padding: 20px; max-width: 420px; width: 100%; box-shadow: var(--shadow); position: relative; }
.ios-hint-card strong { display: block; font-size: 16px; margin-bottom: 8px; }
.ios-hint-card p { margin: 0; color: var(--c-muted); font-size: 14px; line-height: 1.5; }
.ios-hint-close { position: absolute; top: 8px; right: 8px; background: none; border: 0; font-size: 24px; color: var(--c-muted); cursor: pointer; width: 36px; height: 36px; }

/* FAB (floating action button) */
.fab {
    position: fixed;
    right: calc(20px + env(safe-area-inset-right));
    bottom: calc(24px + env(safe-area-inset-bottom));
    width: 64px; height: 64px; border-radius: 50%;
    background: var(--c-primary); color: #fff; border: 0;
    font-size: 36px; line-height: 1; font-weight: 300;
    cursor: pointer; z-index: 40;
    box-shadow: 0 6px 20px rgba(12,39,64,.35);
    display: flex; align-items: center; justify-content: center;
    transition: transform .15s, box-shadow .15s;
}
.fab:hover  { transform: scale(1.05); }
.fab:active { transform: scale(.95); box-shadow: 0 3px 12px rgba(12,39,64,.4); }

/* Toast */
.toast {
    position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
    transform: translateX(-50%);
    background: #1a2330; color: #fff; padding: 12px 18px; border-radius: 999px;
    font-size: 14px; z-index: 200; max-width: 90vw; box-shadow: var(--shadow);
}
.toast.err { background: var(--c-err); }
.toast.ok  { background: var(--c-ok); }
