:root {
    color-scheme: dark;
    --bg: #0f172a;
    --surface: #1e293b;
    --surface-2: #273549;
    --text: #e2e8f0;
    --text-dim: #94a3b8;
    --accent: #22c55e;
    --accent-2: #38bdf8;
    --danger: #ef4444;
    --warn: #f59e0b;
    --border: #334155;
    --radius: 12px;
    --tabbar-height: 60px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
    :root {
        color-scheme: light;
        --bg: #f8fafc;
        --surface: #ffffff;
        --surface-2: #f1f5f9;
        --text: #0f172a;
        --text-dim: #475569;
        --border: #e2e8f0;
    }
}

* {
    box-sizing: border-box;
}

html, body {
    height: 100%;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

button {
    font-family: inherit;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 10;
}

.app-title {
    font-weight: 700;
    font-size: 1.1rem;
}

.offline-badge {
    background: var(--warn);
    color: #1c1917;
    font-size: 0.75rem;
    font-weight: 700;
    min-width: 22px;
    height: 22px;
    border-radius: 11px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

.offline-badge[hidden] {
    display: none;
}

main {
    flex: 1;
    padding: 16px;
    padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom) + 16px);
    max-width: 640px;
    width: 100%;
    margin: 0 auto;
}

.view {
    display: none;
}

.view.active {
    display: block;
}

/* --- Tab bar --- */

.tabbar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding-bottom: var(--safe-bottom);
    z-index: 10;
}

.tab-btn {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-dim);
    padding: 12px 4px;
    min-height: 44px;
    font-size: 0.85rem;
    cursor: pointer;
}

.tab-btn.active {
    color: var(--accent);
    font-weight: 600;
}

/* --- Today: entry form --- */

.entry-form {
    display: flex;
    gap: 8px;
}

.entry-form input {
    flex: 1;
    min-width: 0;
    min-height: 48px;
    padding: 12px 14px;
    font-size: 1rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
}

.entry-form input:focus {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.save-preset-toggle {
    min-width: 44px;
    min-height: 44px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    font-size: 1.1rem;
    cursor: pointer;
}

.save-preset-panel {
    display: flex;
    gap: 8px;
    margin-top: 8px;
}

.save-preset-panel input {
    flex: 1;
    min-width: 0;
    min-height: 44px;
    padding: 10px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
}

.save-preset-panel button {
    min-height: 44px;
    padding: 0 14px;
    border-radius: var(--radius);
    border: none;
    background: var(--accent);
    color: #052e16;
    font-weight: 700;
    cursor: pointer;
}

/* --- Aperçu en direct pendant la frappe --- */

.entry-preview {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}

.preview-item {
    font-size: 0.8rem;
    padding: 4px 10px;
    border-radius: 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text-dim);
}

.preview-item.ok {
    color: var(--accent);
    border-color: var(--accent);
}

.preview-item.warn {
    color: var(--warn);
    border-color: var(--warn);
}

/* --- Chips --- */

.chips {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 12px 0;
}

.chip {
    min-height: 44px;
    padding: 0 16px;
    border-radius: 22px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.9rem;
    cursor: pointer;
}

.chip:active {
    background: var(--surface-2);
}

.preset-chip {
    display: flex;
    align-items: stretch;
    min-height: 44px;
    border-radius: 22px;
    border: 1px solid var(--border);
    background: var(--surface);
    overflow: hidden;
}

.preset-chip-label {
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 0.9rem;
    padding: 0 14px;
    cursor: pointer;
}

.preset-chip-delete {
    border: none;
    border-left: 1px solid var(--border);
    background: transparent;
    color: var(--text-dim);
    padding: 0 12px;
    cursor: pointer;
}

/* --- Macro counters --- */

.macro-counters {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    margin: 16px 0;
}

/* Fibres : valeur informative sans objectif, en pleine largeur sous les 4
   compteurs principaux plutôt que de casser la grille à 4 colonnes. */
.macro-counters .counter[data-macro="fiber"] {
    grid-column: 1 / -1;
}

.counter {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 10px 8px;
    text-align: center;
}

.counter-label {
    font-size: 0.75rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.counter-value {
    font-size: 0.85rem;
    margin: 4px 0 8px;
    white-space: nowrap;
}

.progress {
    height: 6px;
    border-radius: 3px;
    background: var(--surface-2);
    overflow: hidden;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.25s ease;
}

.progress-bar.over {
    background: var(--warn);
}

/* --- Entry list --- */

.entry-list {
    list-style: none;
    margin: 16px 0 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.entry-row {
    overflow: hidden;
    border-radius: var(--radius);
}

.entry-row.needs-correction .entry-swipe {
    border: 1px solid var(--warn);
}

.entry-swipe {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 10px 10px;
    transition: transform 0.2s ease;
    touch-action: pan-y;
}

.entry-main {
    flex: 1;
    min-width: 0;
}

.entry-name {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.entry-macros {
    font-size: 0.8rem;
    color: var(--text-dim);
}

.entry-grams {
    width: 64px;
    min-height: 40px;
    text-align: right;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    padding: 4px 8px;
}

.entry-delete {
    min-width: 44px;
    min-height: 44px;
    border-radius: 8px;
    border: none;
    background: transparent;
    color: var(--danger);
    font-size: 1.1rem;
    cursor: pointer;
}

/* --- Correction d'une entry non résolue --- */

.entry-fix-toggle {
    display: block;
    min-height: 32px;
    margin-top: 6px;
    padding: 0 12px;
    font-size: 0.8rem;
    font-weight: 600;
    border-radius: 8px;
    border: 1px solid var(--warn);
    background: transparent;
    color: var(--warn);
    cursor: pointer;
}

.entry-fix-panel {
    margin-top: 8px;
    padding: 10px;
    border-radius: var(--radius);
    background: var(--surface-2);
}

.fix-search {
    width: 100%;
    min-height: 44px;
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.95rem;
}

.fix-results {
    list-style: none;
    margin: 8px 0 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
    max-height: 180px;
    overflow-y: auto;
}

.fix-result-item {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 0 12px;
    border-radius: 8px;
    background: var(--surface);
    cursor: pointer;
}

.fix-result-item:active {
    background: var(--border);
}

.fix-manual {
    margin-top: 12px;
    font-size: 0.8rem;
    color: var(--text-dim);
}

.fix-manual-fields {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
    margin: 6px 0;
}

.fix-manual-fields .fix-fiber {
    grid-column: 1 / -1;
}

.fix-manual-fields input {
    min-height: 40px;
    text-align: center;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
}

.fix-manual-save {
    min-height: 44px;
    width: 100%;
    border-radius: 8px;
    border: none;
    background: var(--accent);
    color: #052e16;
    font-weight: 700;
    cursor: pointer;
}

/* --- Week view --- */

.nav-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 16px;
}

.nav-row button {
    min-width: 44px;
    min-height: 44px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1.2rem;
    cursor: pointer;
}

.nav-row button:disabled {
    opacity: 0.4;
    cursor: default;
}

.nav-row span {
    font-weight: 600;
}

.bars-chart {
    position: relative;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    height: 200px;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 12px;
}

.bar-col {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 100%;
}

.bar {
    width: 100%;
    max-width: 28px;
    background: var(--accent);
    border-radius: 4px 4px 0 0;
    min-height: 2px;
}

.bar.over {
    background: var(--warn);
}

.bar-label {
    margin-top: 6px;
    font-size: 0.7rem;
    color: var(--text-dim);
}

.target-line {
    position: absolute;
    left: 12px;
    right: 12px;
    border-top: 2px dashed var(--text-dim);
}

.week-averages {
    margin-top: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.avg-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 12px;
    align-items: center;
    background: var(--surface);
    border-radius: var(--radius);
    padding: 10px 14px;
}

.avg-label {
    color: var(--text-dim);
}

.avg-value {
    font-weight: 600;
}

.avg-delta {
    font-size: 0.85rem;
    min-width: 48px;
    text-align: right;
}

.avg-delta.over {
    color: var(--warn);
}

.avg-delta.under {
    color: var(--accent-2);
}

.avg-delta.ok {
    color: var(--accent);
}

/* --- History view --- */

.weight-form {
    display: flex;
    gap: 8px;
    margin-bottom: 8px;
}

.weight-form input {
    flex: 1;
    min-height: 44px;
    padding: 10px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
}

.weight-form button {
    min-height: 44px;
    padding: 0 16px;
    border-radius: var(--radius);
    border: none;
    background: var(--accent);
    color: #052e16;
    font-weight: 700;
    cursor: pointer;
}

.weight-current-label {
    color: var(--text-dim);
    font-size: 0.85rem;
    margin: 0 0 16px;
}

.history-range {
    margin-bottom: 8px;
}

.history-range-btn {
    flex: 1;
    min-height: 36px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-dim);
    font-size: 0.85rem;
    cursor: pointer;
}

.history-range-btn.active {
    background: var(--accent);
    color: #052e16;
    border-color: var(--accent);
    font-weight: 700;
}

.history-chart-title {
    font-size: 0.9rem;
    color: var(--text-dim);
    margin: 16px 0 8px;
}

.chart-container {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 12px;
}

.line-chart {
    width: 100%;
    height: 120px;
    display: block;
}

.chart-line {
    fill: none;
    stroke: var(--accent);
    stroke-width: 2;
    vector-effect: non-scaling-stroke;
}

.chart-dot {
    fill: var(--accent);
}

.chart-target-line {
    stroke: var(--text-dim);
    stroke-width: 1;
    stroke-dasharray: 4;
}

.chart-empty {
    color: var(--text-dim);
    font-size: 0.85rem;
    text-align: center;
    margin: 0;
    padding: 24px 0;
}

/* --- Targets view --- */

.targets-form {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.targets-form label {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 0.9rem;
    color: var(--text-dim);
}

.targets-form input {
    min-height: 44px;
    padding: 10px 12px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 1rem;
}

.targets-form button {
    min-height: 48px;
    border-radius: var(--radius);
    border: none;
    background: var(--accent);
    color: #052e16;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
}

.saved-msg {
    margin-top: 12px;
    color: var(--accent);
    font-size: 0.9rem;
}

.export-btn {
    margin-top: 28px;
    width: 100%;
    min-height: 48px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-size: 0.95rem;
    cursor: pointer;
}

/* --- Toast d'annulation --- */

.undo-toast {
    position: fixed;
    left: 16px;
    right: 16px;
    bottom: calc(var(--tabbar-height) + var(--safe-bottom) + 12px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    border-radius: var(--radius);
    background: var(--surface-2);
    border: 1px solid var(--border);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
    z-index: 20;
    max-width: 640px;
    margin: 0 auto;
}

#undo-toast-btn {
    min-height: 36px;
    padding: 0 14px;
    border-radius: 8px;
    border: none;
    background: var(--accent);
    color: #052e16;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
}
