/* MyDiabetes – mobile-first */

:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-soft: #eff6ff;
    --on-primary: #ffffff;
    --bg: #f3f4f6;
    --surface: #ffffff;
    --text: #111827;
    --text-muted: #6b7280;
    --border: #d1d5db;
    --success-bg: #dcfce7;
    --success-text: #166534;
    --error-bg: #fee2e2;
    --error-text: #991b1b;
    --notice-bg: #fef9c3;
    --notice-text: #713f12;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --secondary: #e5e7eb;
    --secondary-hover: #d1d5db;
    --radius: 12px;
    --nav-height: 64px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --primary: #3b82f6;
        --primary-hover: #60a5fa;
        --primary-soft: #1e293b;
        --on-primary: #ffffff;
        --bg: #0f172a;
        --surface: #1e293b;
        --text: #f1f5f9;
        --text-muted: #94a3b8;
        --border: #475569;
        --success-bg: #14532d;
        --success-text: #dcfce7;
        --error-bg: #7f1d1d;
        --error-text: #fee2e2;
        --notice-bg: #713f12;
        --notice-text: #fef9c3;
        --danger: #ef4444;
        --danger-hover: #f87171;
        --secondary: #334155;
        --secondary-hover: #475569;
    }
}

* { box-sizing: border-box; }

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

body {
    margin: 0;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: 16px;
    line-height: 1.5;
    background: var(--bg);
    color: var(--text);
    padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom));
}

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

/* --- Nagłówek i nawigacja --- */

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

.brand {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--primary);
    text-decoration: none;
}

/* Na telefonie menu jest paskiem na dole ekranu */
.app-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    height: calc(var(--nav-height) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface);
    border-top: 1px solid var(--border);
}

.app-nav a {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-muted);
    text-decoration: none;
    font-size: 0.8rem;
    font-weight: 600;
}

.app-nav a.active { color: var(--primary); }

@media (max-width: 767px) {
    .app-nav a { font-size: 0.7rem; }
    .app-nav svg { width: 24px; height: 24px; }
}

.app-nav svg,
.icon-btn svg {
    width: 26px;
    height: 26px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

@media (min-width: 768px) {
    body { padding-bottom: 0; }

    .app-header { padding: 12px 24px; }

    .app-nav {
        position: static;
        height: auto;
        padding: 0;
        gap: 8px;
        border: none;
        background: none;
    }

    .app-nav a {
        flex-direction: row;
        gap: 6px;
        padding: 8px 14px;
        border-radius: 8px;
        font-size: 0.95rem;
    }

    .app-nav a.active { background: var(--primary-soft); }

    .app-nav svg { width: 20px; height: 20px; }
}

/* --- Układ --- */

.container {
    max-width: 640px;
    margin: 0 auto;
    padding: 12px;
}

.card {
    background: var(--surface);
    border-radius: var(--radius);
    padding: 18px 14px;
    margin-bottom: 20px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.card.narrow { max-width: 440px; margin-left: auto; margin-right: auto; }

.card-title,
.page-title {
    margin: 0 0 16px;
    font-size: 1.4rem;
    font-weight: 700;
}

.section-title {
    margin: 0 0 10px;
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-muted);
}

.section-title::first-letter { text-transform: uppercase; }

.list-section { margin-bottom: 24px; }

.empty,
.hint,
.meta {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.hint { margin: 10px 0 6px; }

.link-more {
    display: inline-block;
    padding: 12px 0;
    font-weight: 600;
    text-decoration: none;
}

/* --- Komunikaty --- */

.flash,
.notice {
    padding: 12px 14px;
    margin-bottom: 16px;
    border-radius: 10px;
    font-weight: 500;
}

.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }
.notice { background: var(--notice-bg); color: var(--notice-text); }

/* --- Formularze --- */

.field {
    margin: 0 0 18px;
    padding: 0;
    border: none;
    min-width: 0;
}

.label {
    display: block;
    margin-bottom: 6px;
    padding: 0;
    font-weight: 600;
}

.label small {
    font-weight: 400;
    color: var(--text-muted);
}

input[type="text"],
input[type="password"],
input[type="search"],
input[type="date"],
input[type="datetime-local"],
textarea {
    width: 100%;
    min-height: 54px;
    padding: 12px 14px;
    font: inherit;
    font-size: 1.15rem; /* min. 16px – inaczej iOS powiększa stronę */
    color: var(--text);
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 10px;
    outline: none;
}

textarea { resize: vertical; }

input:focus,
textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2);
}

[aria-invalid="true"] { border-color: var(--danger); }

.field-error {
    margin: 6px 0 0;
    color: var(--danger);
    font-size: 0.9rem;
    font-weight: 500;
}

/* Trzy pola liczbowe obok siebie; etykiety mogą się zawijać, pola zostają w jednej linii */
.number-row {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
}

.number-row .field {
    display: flex;
    flex-direction: column;
}

.number-row .label {
    flex: 1;
    line-height: 1.25;
}

.number-row .label small { display: block; }

.number-row input {
    text-align: center;
    font-size: 1.4rem;
    font-weight: 600;
}

/* Typ posiłku: duże przyciski zamiast listy rozwijanej */
.type-picker {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

@media (min-width: 340px) {
    .type-picker { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (min-width: 600px) {
    .type-picker { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

.type-picker input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.type-picker label {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    padding: 6px 4px;
    text-align: center;
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.2;
    background: var(--surface);
    border: 2px solid var(--border);
    border-radius: 10px;
    cursor: pointer;
    user-select: none;
}

.type-picker input:checked + label {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--on-primary);
}

.type-picker input:focus-visible + label {
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.35);
}

.checkbox {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
    cursor: pointer;
}

.checkbox input { width: 22px; height: 22px; }

.form-footer { text-align: center; margin: 16px 0 0; }

/* --- Podpowiedzi posiłków --- */

.autocomplete { position: relative; }

.suggest-list {
    position: absolute;
    z-index: 5;
    left: 0;
    right: 0;
    top: calc(100% + 4px);
    margin: 0;
    padding: 4px;
    list-style: none;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
    max-height: 280px;
    overflow-y: auto;
}

.suggest-list button {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 48px;
    padding: 10px 12px;
    font: inherit;
    text-align: left;
    color: var(--text);
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
}

.suggest-list button:hover,
.suggest-list button:focus { background: var(--primary-soft); outline: none; }

.quick-picks {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    max-width: 100%;
    min-height: 44px;
    padding: 8px 14px;
    font: inherit;
    font-size: 0.95rem;
    color: var(--text);
    background: var(--primary-soft);
    border: 1px solid var(--border);
    border-radius: 999px;
    cursor: pointer;
}

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

.chip-text {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chip-meta,
.suggest-meta {
    flex-shrink: 0;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--primary);
}

/* --- Przyciski --- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    padding: 10px 20px;
    font: inherit;
    font-weight: 600;
    text-decoration: none;
    white-space: nowrap;
    border: none;
    border-radius: 10px;
    cursor: pointer;
}

.btn-primary { background: var(--primary); color: var(--on-primary); }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--secondary); color: var(--text); }
.btn-secondary:hover { background: var(--secondary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: var(--danger-hover); }
.btn-block { width: 100%; }
.btn-lg { min-height: 56px; font-size: 1.15rem; }
.btn:disabled { opacity: 0.6; cursor: wait; }

.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.actions > * { flex: 1 1 120px; }
.actions form { display: flex; }
.actions form .btn { flex: 1; }

.pager {
    display: flex;
    justify-content: space-between;
    gap: 10px;
}

.pager .btn:only-child { margin-left: auto; }

/* --- Lista posiłków --- */

.meal-card {
    display: flex;
    align-items: stretch;
    margin-bottom: 10px;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.meal-card-link {
    flex: 1;
    min-width: 0;
    padding: 12px 14px;
    color: inherit;
    text-decoration: none;
}

.meal-card-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 4px;
}

.meal-time { font-weight: 700; font-size: 1.05rem; }

.badge {
    display: inline-block;
    padding: 2px 10px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--primary);
    background: var(--primary-soft);
    border-radius: 999px;
}

.badge-lg { font-size: 0.95rem; padding: 4px 12px; }

.meal-desc {
    margin: 0 0 6px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow-wrap: anywhere;
}

.meal-values {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 16px;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.meal-values b { color: var(--text); }

.icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    flex-shrink: 0;
    color: var(--text-muted);
    border-left: 1px solid var(--border);
}

.icon-btn:hover,
.icon-btn:active { color: var(--primary); }

/* --- Szczegóły posiłku --- */

.detail-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}

.detail-date { font-weight: 600; color: var(--text-muted); }

.detail-desc {
    margin: 0 0 18px;
    font-size: 1.2rem;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.detail-values {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    margin: 0 0 16px;
}

.detail-values div {
    padding: 12px 8px;
    text-align: center;
    background: var(--bg);
    border-radius: 10px;
}

.detail-values dt { font-size: 0.85rem; color: var(--text-muted); }

.detail-values dd {
    margin: 4px 0 0;
    font-size: 1.5rem;
    font-weight: 700;
}

.detail-values dd small {
    display: block;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-muted);
}

/* --- Przyciski formularza nowego posiłku: "Zapisz" szerszy niż "Wyczyść" --- */

.form-actions {
    display: flex;
    gap: 10px;
}

.form-actions .btn-primary { flex: 2; }
.form-actions .btn-secondary { flex: 1; }

/* --- Elementy rozwijane (uwagi, wyszukiwanie, ulubione) --- */

summary { cursor: pointer; }

.notes-toggle summary {
    list-style: none;
    color: var(--primary);
}

.notes-toggle summary::before { content: "+ "; }
.notes-toggle[open] summary::before { content: "– "; }
.notes-toggle summary::-webkit-details-marker,
.fav-add summary::-webkit-details-marker { display: none; }

.filters summary {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
}

.filters[open] summary { margin-bottom: 14px; }

.fav-add { margin-top: 12px; }
.fav-add summary { list-style: none; }
.fav-add[open] summary { margin-bottom: 14px; }

.date-row {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
}

.field-narrow { max-width: 180px; }

.meal-notes {
    margin: 6px 0 0;
    font-size: 0.85rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.detail-notes {
    margin: 0 0 16px;
    padding: 12px 14px;
    background: var(--bg);
    border-radius: 10px;
}

.detail-notes h2 {
    margin: 0 0 4px;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.detail-notes p {
    margin: 0;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

/* --- Ulubione --- */

.fav-card { margin-bottom: 12px; }

.fav-name {
    margin: 0 0 4px;
    font-size: 1.15rem;
}

.fav-desc {
    margin: 0 0 6px;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.fav-card .actions { margin-top: 12px; }

/* --- Statystyki --- */

a.chip { text-decoration: none; font-weight: 600; }

.chip-active {
    background: var(--primary);
    border-color: var(--primary);
    color: var(--on-primary);
}

.period-form { margin-top: 14px; }

.period-label {
    margin: 0 0 10px;
    font-weight: 600;
    color: var(--text-muted);
}

.tiles {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin-bottom: 6px;
}

.tile {
    padding: 14px 12px;
    background: var(--surface);
    border-radius: var(--radius);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

.tile-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    line-height: 1.3;
}

.tile-value {
    margin: 4px 0 2px;
    font-size: 1.6rem;
    font-weight: 700;
}

.tile-value small {
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-muted);
}

.tile-sub { font-size: 0.8rem; color: var(--text-muted); }

.table-card { padding: 0; overflow: hidden; }

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

.data-table th,
.data-table td {
    padding: 10px 8px;
    text-align: center;
    border-bottom: 1px solid var(--border);
}

.data-table th {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    background: var(--bg);
}

.data-table th:first-child,
.data-table td:first-child { text-align: left; padding-left: 14px; }

.data-table tr:last-child td { border-bottom: none; }

.data-table td small {
    display: block;
    font-size: 0.75rem;
    color: var(--text-muted);
}

.data-table a { font-weight: 600; text-decoration: none; }

[hidden] { display: none !important; }
