:root {
    --bg: #f6f5f4;
    --surface: #ffffff;
    --surface-strong: #ffffff;
    --headerbar: #ebebeb;
    --border: rgba(0, 0, 0, 0.12);
    --text: rgba(0, 0, 0, 0.8);
    --muted: rgba(0, 0, 0, 0.55);
    --accent: #3584e4;
    --accent-strong: #2f6fc4;
    --accent-subtle: rgba(53, 132, 228, 0.1);
    --green: #2ec27e;
    --green-subtle: rgba(46, 194, 126, 0.12);
    --red: #e01b24;
    --red-subtle: rgba(224, 27, 36, 0.1);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(0, 0, 0, 0.06);
    --radius-xl: 12px;
    --radius-lg: 12px;
    --radius-md: 8px;
    --radius-sm: 6px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
    margin: 0;
    min-height: 100vh;
    max-width: 100vw;
    overflow-x: hidden; /* backstop: page itself never scrolls sideways — wide content scrolls in its own container */
    font-family: "Inter", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--bg);
}

.muted { color: var(--muted); }
.hidden { display: none !important; }

/* ── shared card / topbar (qhub language) ─────────────── */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow);
}

.page-shell {
    width: min(1180px, calc(100% - 32px));
    margin: 0 auto;
    padding: 24px 0 48px;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 10px 14px;
    margin-bottom: 18px;
    background: var(--headerbar);
    border: 1px solid var(--border);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow);
}

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

.page-title { display: none; } /* показывается вместо полного названия на мобильном — см. @media ниже */

.brand-dot {
    width: 12px;
    height: 12px;
    border-radius: 999px;
    background: linear-gradient(180deg, #62a0ea, #3584e4);
    box-shadow: 0 0 0 4px rgba(53, 132, 228, 0.15);
}

.topnav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.pill, .small-tag, .meta-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border-radius: 999px;
    padding: 8px 12px;
    font-size: 0.85rem;
}

.meta-chip, .small-tag {
    background: rgba(0, 0, 0, 0.045);
    border: 1px solid rgba(0, 0, 0, 0.05);
}

/* ── buttons / forms ───────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 9px 16px;
    border: 1px solid transparent;
    border-radius: 999px;
    font: inherit;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: 0.18s ease;
    white-space: nowrap;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-strong); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; }
.btn-ghost { background: rgba(0, 0, 0, 0.04); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: rgba(53, 132, 228, 0.1); border-color: rgba(53, 132, 228, 0.2); }
.btn-sm { padding: 6px 12px; font-size: 0.78rem; }
.btn-block { width: 100%; justify-content: center; }
.icon-btn { gap: 6px; }
.icon-btn svg { flex-shrink: 0; }

/* ── mobile hamburger + slide-out drawer (hidden on desktop, shown ≤640px) ─── */
.hamburger-btn {
    display: none;
    background: none; border: none; color: var(--text); cursor: pointer;
    padding: 4px; margin-right: 2px; flex-shrink: 0;
}

.drawer-backdrop {
    display: none;
    position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 190;
}
.drawer-backdrop.open { display: block; }

.mobile-drawer {
    display: none;
    position: fixed; top: 0; left: 0; bottom: 0; width: 250px; z-index: 200;
    background: var(--surface); border-right: 1px solid var(--border);
    box-shadow: 4px 0 16px rgba(0, 0, 0, 0.12);
    transform: translateX(-100%); transition: transform 0.22s ease;
    flex-direction: column; padding: 10px 0;
    padding-top: calc(10px + env(safe-area-inset-top));
    padding-bottom: env(safe-area-inset-bottom);
}
.mobile-drawer.open { transform: translateX(0); }

.drawer-header {
    display: flex; align-items: center; gap: 10px;
    padding: 10px 18px 16px; font-weight: 700;
}
.drawer-item {
    display: flex; align-items: center; gap: 12px;
    padding: 12px 18px; color: var(--text); text-decoration: none;
    background: none; border: none; font: inherit; font-size: 0.9rem; text-align: left;
    cursor: pointer; width: 100%;
}
.drawer-item svg { flex-shrink: 0; color: var(--muted); }
.drawer-item.active { color: var(--accent); background: var(--accent-subtle); }
.drawer-item.active svg { color: var(--accent); }

.form-group { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.form-group.full-width { grid-column: 1 / -1; }
.form-group label { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.form-group input, .form-group select, .form-group textarea {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text);
    font: inherit;
    font-size: 0.9rem;
    padding: 10px 12px;
    outline: none;
    transition: border-color 0.15s;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--accent); }

.error-msg { color: var(--red); font-size: 0.82rem; text-align: center; margin-top: 12px; min-height: 18px; }

/* ── login page ─────────────────────────────────────────── */
.auth-body { display: flex; align-items: center; justify-content: center; }
.login-wrap { width: 100%; max-width: 380px; padding: 20px; }
.login-card { padding: 32px 28px; }
.login-brand { display: flex; flex-direction: column; align-items: center; gap: 6px; margin-bottom: 26px; text-align: center; }
.login-brand .brand-dot { width: 20px; height: 20px; margin-bottom: 6px; }
.login-brand h1 { margin: 0; font-size: 1.4rem; }
.login-brand p { margin: 0; font-size: 0.85rem; }
.login-version { text-align: center; margin-top: 8px; font-size: 0.7rem; color: var(--muted); }

/* ── app layout ──────────────────────────────────────────── */
.layout, .section-stack { display: grid; gap: 20px; }
/* grid/flex items default to min-width:auto — without this a wide child (a table,
   a fixed-width chart) refuses to shrink and blows the whole page out horizontally
   instead of scrolling inside its own overflow-x container. */
.layout > *, .hero-grid > *, .diary-grid > *, .trends-grid > *, .summary-grid > * { min-width: 0; }

.hero-grid {
    display: grid;
    grid-template-columns: minmax(0, 1.7fr) minmax(300px, 1fr);
    gap: 20px;
    align-items: stretch;
}

/* ── chat card ───────────────────────────────────────────── */
.chat-card { padding: 22px; display: flex; flex-direction: column; height: 640px; }

.chat-messages {
    flex: 1;
    min-height: 0; /* без этого flex-элемент не сжимается и раздувает страницу вместо внутреннего скролла */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 4px 4px 12px;
}

.msg-bubble {
    max-width: 78%;
    padding: 10px 14px;
    border-radius: 16px;
    font-size: 0.9rem;
    line-height: 1.5;
    white-space: pre-wrap;
}

.msg-user {
    align-self: flex-end;
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.msg-assistant {
    align-self: flex-start;
    background: var(--bg);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
}

.msg-assistant.msg-error { border-color: var(--red); color: var(--red); background: var(--red-subtle); }
.msg-assistant.msg-pending { color: var(--muted); font-style: italic; }

.msg-images { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.msg-images img {
    width: 84px; height: 84px; object-fit: cover;
    border-radius: var(--radius-sm); cursor: pointer;
    border: 1px solid rgba(255, 255, 255, 0.4);
}
.msg-assistant .msg-images img { border-color: var(--border); }

.chat-composer { border-top: 1px solid var(--border); padding-top: 12px; }
.chat-photo-preview { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.chat-photo-preview .thumb { position: relative; width: 56px; height: 56px; }
.chat-photo-preview .thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.chat-photo-preview .thumb button {
    position: absolute; top: -6px; right: -6px; width: 18px; height: 18px;
    border-radius: 50%; border: none; background: var(--red); color: #fff;
    font-size: 11px; line-height: 1; cursor: pointer;
}

.chat-input-row { display: flex; align-items: flex-end; gap: 8px; }
.attach-btn {
    display: flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; border-radius: var(--radius-md);
    border: 1px solid var(--border); background: var(--bg);
    color: var(--muted); cursor: pointer; flex-shrink: 0;
}
.attach-btn:hover { border-color: var(--accent); color: var(--accent); }

.chat-input-row textarea {
    flex: 1; resize: none; max-height: 120px;
    background: var(--bg); border: 1px solid var(--border);
    border-radius: var(--radius-md); padding: 10px 12px;
    font: inherit; font-size: 0.9rem; outline: none;
}
.chat-input-row textarea:focus { border-color: var(--accent); }

/* ── today card ──────────────────────────────────────────── */
.today-card { padding: 22px; display: flex; flex-direction: column; gap: 16px; }
.today-card-header { display: flex; align-items: center; justify-content: space-between; }
.today-kcal { text-align: center; padding: 8px 0; }
.kcal-remaining { font-size: 2.4rem; font-weight: 800; letter-spacing: -0.02em; }
.kcal-remaining.over { color: var(--red); }

.macro-row { display: flex; justify-content: space-between; gap: 8px; }
.macro-item { flex: 1; text-align: center; padding: 10px 6px; background: var(--bg); border-radius: var(--radius-md); }
.macro-item .val { font-weight: 700; font-size: 1.05rem; }
.macro-item .lbl { font-size: 0.68rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

.today-weight { text-align: center; padding-top: 8px; border-top: 1px solid var(--border); font-size: 0.9rem; }
.today-weight strong { font-size: 1.1rem; }

/* ── trend charts ────────────────────────────────────────── */
.trends-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.trend-chart-wrap { min-width: 0; }
.trend-chart-title { font-size: 0.8rem; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.chart-host { position: relative; width: 100%; }
.chart-host svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-tooltip {
    position: absolute; transform: translateX(-50%);
    background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 6px 10px; font-size: 0.78rem; box-shadow: var(--shadow);
    pointer-events: none; opacity: 0; transition: opacity 0.1s; white-space: nowrap; z-index: 5;
}
.chart-tooltip.visible { opacity: 1; }
.chart-tooltip-row { display: flex; justify-content: space-between; gap: 10px; }
.chart-tooltip-row + .chart-tooltip-row { margin-top: 2px; }
.chart-tooltip-row.muted { color: var(--muted); font-size: 0.72rem; }
.chart-tooltip-row strong { font-variant-numeric: tabular-nums; }

@media (max-width: 640px) {
    .trends-grid { grid-template-columns: 1fr; }
}

/* ── weekly / monthly summary ───────────────────────────────── */
.summary-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.summary-col { background: var(--bg); border-radius: var(--radius-md); padding: 14px 16px; }
.summary-col-title { font-size: 0.8rem; font-weight: 700; margin-bottom: 10px; }
.summary-stat-row { display: flex; justify-content: space-between; align-items: baseline; padding: 4px 0; font-size: 0.86rem; }
.summary-stat-row .lbl { color: var(--muted); }
.summary-stat-row .val { font-weight: 600; font-variant-numeric: tabular-nums; }
.summary-stat-row .val.good { color: #0ca30c; }
.summary-stat-row .val.critical { color: #d03b3b; }

@media (max-width: 640px) {
    .summary-grid { grid-template-columns: 1fr; }
}

/* ── history table ───────────────────────────────────────── */
.section-card { padding: 24px 26px; }
.section-heading { margin-bottom: 14px; }
.section-heading h2 { margin: 0; font-size: 1.3rem; }

.history-table-wrap { overflow-x: auto; }
.history-table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
.history-table th, .history-table td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.history-table th { color: var(--muted); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em; }
.history-table th.num, .history-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.history-table tr:hover td { background: rgba(53, 132, 228, 0.05); cursor: pointer; }
.history-table td.remaining-neg { color: var(--red); }
.history-table td.remaining-pos { color: var(--green); }

.empty-text { color: var(--muted); font-size: 0.9rem; padding: 24px 0; text-align: center; }

/* ── modal ───────────────────────────────────────────────── */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45);
    display: none; align-items: center; justify-content: center;
    z-index: 200; padding: 20px;
}
.modal-overlay.open { display: flex; }
.modal { width: 100%; max-width: 560px; max-height: 88vh; overflow-y: auto; }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 20px 22px 0; }
.modal-header h2 { margin: 0; font-size: 1.1rem; }
.modal-close { background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 16px 22px 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; padding: 14px 22px 20px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 16px; }

/* ── diary page ──────────────────────────────────────────── */
.diary-grid {
    display: grid;
    grid-template-columns: 280px minmax(0, 1fr);
    gap: 20px;
    align-items: start;
}

.day-list-card { padding: 20px; }
.day-list { display: flex; flex-direction: column; gap: 6px; max-height: 640px; overflow-y: auto; }
.day-list-item {
    display: flex; align-items: center; justify-content: space-between; gap: 8px;
    padding: 10px 12px; border-radius: var(--radius-md); cursor: pointer;
    border: 1px solid transparent;
}
.day-list-item:hover { background: rgba(53, 132, 228, 0.08); }
.day-list-item.active { background: var(--accent-subtle); border-color: rgba(53, 132, 228, 0.25); }
.day-list-item .dli-date { font-weight: 600; font-size: 0.88rem; }
.day-list-item .dli-kcal { font-size: 0.78rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.day-detail-card { padding: 22px 26px; min-height: 400px; }
.day-detail-header {
    display: flex; align-items: center; justify-content: space-between;
    gap: 12px; margin-bottom: 16px;
}
.day-detail-nav { display: flex; align-items: center; gap: 10px; }
.day-detail-header h2 { margin: 0; font-size: 1.2rem; }

/* ── day detail modal ───────────────────────────────────── */
.day-frontmatter { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.prose { font-size: 0.88rem; line-height: 1.6; }
.prose table { width: 100%; border-collapse: collapse; margin: 0 0 14px; }
.prose th, .prose td { padding: 7px 10px; border: 1px solid var(--border); text-align: left; }
.prose th { background: rgba(0, 0, 0, 0.04); font-weight: 600; font-size: 0.78rem; }
.prose td:nth-child(3), .prose td:nth-child(4), .prose td:nth-child(5), .prose td:nth-child(6),
.prose th:nth-child(3), .prose th:nth-child(4), .prose th:nth-child(5), .prose th:nth-child(6) { text-align: right; }
.day-photos { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; padding-bottom: 16px; }
.day-photos img { width: 100px; height: 100px; object-fit: cover; border-radius: var(--radius-sm); border: 1px solid var(--border); cursor: pointer; }

/* ── lightbox ────────────────────────────────────────────── */
.lightbox {
    position: fixed; inset: 0; background: rgba(0, 0, 0, 0.85);
    display: none; align-items: center; justify-content: center;
    z-index: 300; padding: 20px; cursor: zoom-out;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 92vw; max-height: 88vh; border-radius: var(--radius-md); object-fit: contain; }

/* ── responsive ──────────────────────────────────────────── */
@media (max-width: 980px) {
    .hero-grid { grid-template-columns: minmax(0, 1fr); }
    .chat-card { height: 520px; }
    .diary-grid { grid-template-columns: minmax(0, 1fr); }
    .day-list { max-height: 240px; }
}

@media (max-width: 640px) {
    .page-shell { width: min(100% - 16px, 1180px); padding: 10px 0 84px; }
    .topbar { flex-direction: row; align-items: center; gap: 8px; padding: 10px 12px; }
    .brand { font-size: 0.85rem; }
    .brand-full { display: none; } /* полное название прячем на мобильном */
    .page-title { display: inline; } /* вместо него — заголовок текущего экрана: Чат / Сегодня / По дням */
    #userChip { display: none; } /* дублирует имя пользователя, места на мобильном мало */
    .topnav { gap: 4px; }
    .nav-link-diary { display: none; } /* переехала в выезжающий сайдбар */
    .form-grid { grid-template-columns: 1fr; }
    .msg-bubble { max-width: 90%; font-size: 0.85rem; }

    /* иконки вместо текстовых кнопок в шапке */
    .icon-btn { width: 36px; height: 36px; padding: 0; justify-content: center; border-radius: 50%; }
    .icon-btn .btn-label { display: none; }

    /* гамбургер + выезжающий сайдбар для переключения Чат/Обзор/Дневник —
       сайдбар не занимает постоянное место на экране (он поверх контента,
       появляется по кнопке), поэтому резервировать под него отступ не нужно */
    .hamburger-btn { display: flex; align-items: center; justify-content: center; }
    .mobile-drawer { display: flex; }
    body[data-active="chat"] [data-view="overview"] { display: none; }
    body[data-active="overview"] [data-view="chat"] { display: none; }
    .hero-grid { gap: 0; }

    /* Чат в своей вкладке — не карточка-окошко, а сама страница: без рамки,
       тени, скругления и внешних отступов, во всю ширину и высоту экрана.
       Вся цепочка предков переведена на flex:1/min-height:0, чтобы высота
       считалась браузером точно, и единственным скроллящимся местом было
       .chat-messages — сам вьюпорт (body) в этом режиме не скроллится. */
    body[data-active="chat"] { height: 100dvh; overflow: hidden; }
    body[data-active="chat"] .page-shell {
        width: 100%; padding: 0; height: 100dvh;
        display: flex; flex-direction: column;
    }
    body[data-active="chat"] .topbar {
        flex-shrink: 0; border-radius: 0; border-left: none; border-right: none; border-top: none;
        box-shadow: none; margin-bottom: 0;
    }
    body[data-active="chat"] main.layout { flex: 1; min-height: 0; display: flex; gap: 0; }
    body[data-active="chat"] .hero-grid { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 0; }
    body[data-active="chat"] .chat-card {
        flex: 1; min-height: 0; height: auto;
        background: transparent; border: none; box-shadow: none; border-radius: 0; padding: 0;
    }
    body[data-active="chat"] .chat-messages { padding: 12px 14px; }
    body[data-active="chat"] .chat-composer {
        flex-shrink: 0;
        padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
        border-top: 1px solid var(--border); background: var(--surface);
    }
    body[data-active="overview"] #newChatBtn { display: none; } /* нерелевантна вне чата */

    /* меньше воздуха в карточках и заголовках на телефоне */
    .btn { padding: 8px 12px; font-size: 0.8rem; }
    .card, .today-card, .section-card, .day-list-card, .day-detail-card { padding: 14px; }
    .login-card { padding: 22px 18px; }
    .section-heading h2, .modal-header h2 { font-size: 1.05rem; }
    .login-brand h1 { font-size: 1.25rem; }
    .kcal-remaining { font-size: 1.9rem; }
    .macro-item { padding: 8px 4px; }
    .macro-item .val { font-size: 0.95rem; }
    .attach-btn { width: 36px; height: 36px; }
    .modal-header, .modal-body, .modal-actions { padding-left: 16px; padding-right: 16px; }

    .history-table { font-size: 0.78rem; }
    .history-table th, .history-table td { padding: 7px 8px; }
    .day-list-item { padding: 8px 10px; }
    .day-list-item .dli-date { font-size: 0.82rem; }
    .prose { font-size: 0.82rem; }
    .prose th, .prose td { padding: 6px 8px; }
    .summary-col { padding: 12px 14px; }
    .trend-chart-title { font-size: 0.76rem; }

    .day-detail-card { padding: 14px; }
    .day-detail-header { flex-wrap: wrap; }
    .day-detail-nav { flex: 1 1 100%; justify-content: center; }
    .day-detail-header h2 { flex: 1; text-align: center; font-size: 1.05rem; }
    #editDayBtn { flex: 1 1 100%; }
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #242424;
        --surface: #383838;
        --surface-strong: #383838;
        --headerbar: #303030;
        --border: rgba(255, 255, 255, 0.12);
        --text: rgba(255, 255, 255, 0.8);
        --muted: rgba(255, 255, 255, 0.55);
        --accent: #78aeed;
        --accent-strong: #97c2f2;
        --accent-subtle: rgba(120, 174, 237, 0.14);
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 0 0 1px rgba(255, 255, 255, 0.06);
    }
    .meta-chip, .small-tag { background: rgba(255, 255, 255, 0.06); border-color: rgba(255, 255, 255, 0.06); }
    .btn-ghost { background: rgba(255, 255, 255, 0.06); }
    .msg-user { color: #10151c; }
    .prose th { background: rgba(255, 255, 255, 0.06); }
}
