/* ============================================================
   Reset & base
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; }
* { -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; font-size: 16px; }

body {
    margin: 0;
    padding: 1.5rem;
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    background: #f4f6f8;
    color: #1f2937;
    line-height: 1.5;
}

h1 { font-size: 1.35rem; margin: 0 0 1rem; font-weight: 600; }
h2 { font-size: 1.15rem; margin: 0 0 1rem; font-weight: 600; }

/* ============================================================
   Layout
   ============================================================ */
.wrap { max-width: 860px; margin: 0 auto; }
.wrap.wide { max-width: 100%; }

.card {
    background: #fff;
    padding: 1.5rem;
    border-radius: 12px;
    box-shadow: 0 4px 18px rgba(0, 0, 0, .06);
    margin-bottom: 1.5rem;
}

.back {
    display: inline-block;
    margin-bottom: 1rem;
    color: #2563eb;
    text-decoration: none;
    font-size: 1rem;
    padding: 6px 4px;
    min-height: 36px;
}
.back:hover { text-decoration: underline; }

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
}
.topbar .back { margin-bottom: 0; }

.lock-link {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    color: #64748b;
    text-decoration: none;
    font-size: .95rem;
    padding: 8px .9rem;
    border-radius: 8px;
    min-height: 40px;
}
.lock-link:hover { color: #b91c1c; background: #fef2f2; }

/* ============================================================
   Hero (index)
   ============================================================ */
.hero {
    background: #fff;
    padding: 2rem;
    border-radius: 12px;
    box-shadow: 0 4px 18px rgba(0, 0, 0, .06);
    margin-bottom: 1.5rem;
}
.hero h1 {
    font-size: 1.15rem;
    font-weight: 600;
    line-height: 1.55;
    margin: 0 0 .5rem;
    color: #0f172a;
}
.hero .org-name { color: #2563eb; }
.hero p {
    margin: .85rem 0 0;
    color: #475569;
    font-size: 1rem;
    line-height: 1.55;
}

/* ============================================================
   Menu (index)
   ============================================================ */
.menu-title {
    font-size: 1.1rem;
    color: #334155;
    margin: 0 0 .85rem;
    font-weight: 600;
}
.menu {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 1.1rem;
}
.menu-item {
    display: block;
    text-decoration: none;
    color: inherit;
    background: #fff;
    padding: 1.35rem 1.5rem;
    border-radius: 12px;
    box-shadow: 0 4px 18px rgba(0, 0, 0, .06);
    border-left: 4px solid #94a3b8;
    transition: transform .1s ease, box-shadow .1s ease;
    min-height: 100px;
}
.menu-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 22px rgba(0, 0, 0, .08);
}
.menu-item h2 {
    font-size: 1.15rem;
    margin: 0 0 .4rem;
    color: #0f172a;
}
.menu-item p {
    margin: 0;
    font-size: .95rem;
    color: #64748b;
    line-height: 1.5;
}
.menu-item .tag {
    display: inline-block;
    font-size: .72rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    padding: .2rem .65rem;
    border-radius: 999px;
    margin-bottom: .7rem;
    font-weight: 600;
}
.menu-item.theme-nutrition { border-left-color: #2563eb; }
.menu-item.theme-nutrition h2 { color: #1d4ed8; }
.menu-item.theme-nutrition:hover { box-shadow: 0 8px 22px rgba(37, 99, 235, .18); }
.menu-item.theme-nutrition .tag { background: #dbeafe; color: #1e40af; }
.menu-item.theme-attendance { border-left-color: #10b981; }
.menu-item.theme-attendance h2 { color: #047857; }
.menu-item.theme-attendance:hover { box-shadow: 0 8px 22px rgba(16, 185, 129, .18); }
.menu-item.theme-attendance .tag { background: #d1fae5; color: #065f46; }

/* ============================================================
   Forms
   ============================================================ */
label {
    display: block;
    font-size: .95rem;
    color: #334155;
    margin: 1rem 0 .4rem;
    font-weight: 500;
}

input[type=text],
input[type=email],
input[type=date],
input[type=number],
input[type=password],
select,
textarea {
    width: 100%;
    padding: .8rem .95rem;
    border: 1px solid #cbd3da;
    border-radius: 8px;
    font-size: 1rem;
    font-family: inherit;
    background: #fff;
    min-height: 48px;
    -webkit-appearance: none;
    appearance: none;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: #2563eb;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

/* Основные кнопки отправки (только для форм) */
form > button[type=submit], .btn {
    margin-top: 1.5rem;
    padding: .9rem 1.5rem;
    border: 0;
    border-radius: 8px;
    background: #2563eb;
    color: #fff;
    font-size: 1rem;
    cursor: pointer;
    min-height: 52px;
    font-weight: 500;
    font-family: inherit;
}
form > button[type=submit]:hover, .btn:hover { background: #1d4ed8; }
form > button[type=submit]:active, .btn:active {
    background: #1e40af;
    transform: translateY(1px);
}

.grid { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem 1rem; }
.grid .full { grid-column: 1 / -1; }

.hint {
    font-size: .9rem;
    margin-top: .45rem;
    min-height: 1.3em;
    color: #64748b;
}
.hint.warn { color: #b91c1c; font-weight: 500; }

.err, .ok, .info {
    padding: .8rem 1rem;
    border-radius: 8px;
    font-size: .95rem;
    margin-bottom: 1rem;
    line-height: 1.5;
}
.err  { background: #fee2e2; color: #991b1b; }
.ok   { background: #dcfce7; color: #166534; }
.info { background: #e0f2fe; color: #075985; }

/* ============================================================
   Tables (general)
   ============================================================ */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: .95rem;
}
th, td {
    padding: .6rem .75rem;
    border-bottom: 1px solid #eef1f4;
    text-align: left;
    vertical-align: middle;
}
th {
    background: #f8fafc;
    font-weight: 600;
    color: #334155;
}
td.num, th.num { text-align: right; }

.table-wrap {
    overflow: auto;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    -webkit-overflow-scrolling: touch;
    max-height: 70vh;
}

/* ============================================================
   Matrix / stat tables
   ============================================================ */
table.stat {
    border-collapse: separate;
    border-spacing: 0;
    font-size: .88rem;
    width: max-content;
    min-width: 100%;
}
table.stat th, table.stat td {
    padding: .45rem .55rem;
    border-right: 1px solid #e2e8f0;
    border-bottom: 1px solid #e2e8f0;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
}
table.stat thead th {
    background: #f8fafc;
    font-weight: 600;
    color: #334155;
    position: sticky;
    top: 0;
    z-index: 2;
}
table.stat th.sticky-col,
table.stat td.sticky-col {
    position: sticky;
    left: 0;
    text-align: left;
    background: #f8fafc;
    z-index: 3;
}
table.stat thead th.sticky-col { z-index: 4; }
table.stat tbody tr:nth-child(even) td.sticky-col { background: #f1f5f9; }
table.stat td.cell.has-val { background: #eff6ff; font-weight: 500; }
table.stat td.cell.at-max  { background: #dcfce7; }
table.stat td.cell .empty  { color: #cbd5e1; }
table.stat td.total        { background: #fefce8; font-weight: 600; }

table.stat tfoot td {
    background: #fefce8;
    font-weight: 600;
    position: sticky;
    bottom: 0;
    z-index: 2;
}
table.stat tfoot td.sticky-col { z-index: 3; }
table.stat tfoot td.grand      { background: #fde68a; }

/* Attendance color levels */
table.stat td.cell.at-zero { background: #dcfce7; color: #166534; }
table.stat td.cell.lvl-low { background: #eff6ff; }
table.stat td.cell.lvl-mid { background: #fef9c3; }
table.stat td.cell.lvl-high{ background: #fed7aa; }
table.stat td.cell.lvl-crit{ background: #fecaca; font-weight: 600; }

table.stat td.cell .num {
    font-size: 1rem;
    font-weight: 600;
    line-height: 1.15;
}
table.stat td.cell .detail {
    font-size: .78rem;
    color: #64748b;
    line-height: 1.2;
    margin-top: 2px;
}
table.stat td.cell.lvl-crit .detail { color: #7f1d1d; }
table.stat td.cell.at-zero  .detail { color: #166534; }

/* ============================================================
   KPI boxes
   ============================================================ */
.kpi {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: .75rem;
    margin: .75rem 0 1.25rem;
}
.kpi .box {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    padding: .8rem 1rem;
}
.kpi .box .label {
    font-size: .78rem;
    color: #64748b;
    text-transform: uppercase;
    letter-spacing: .03em;
    margin-bottom: .2rem;
}
.kpi .box .value {
    font-size: 1.45rem;
    font-weight: 600;
    color: #0f172a;
    line-height: 1.1;
}
.kpi .box.box-sick   { border-color: #fecaca; }
.kpi .box.box-sick   .value { color: #b91c1c; }
.kpi .box.box-arvi   { border-color: #fed7aa; }
.kpi .box.box-arvi   .value { color: #c2410c; }
.kpi .box.box-covid  { border-color: #fecdd3; }
.kpi .box.box-covid  .value { color: #9f1239; }
.kpi .box.box-other  .value { color: #334155; }
.kpi .box.box-absent { border-color: #bfdbfe; background: #eff6ff; }
.kpi .box.box-absent .value { color: #1d4ed8; }

/* ============================================================
   Filter bar (nutritionstat)
   ============================================================ */
.filter-bar {
    display: flex;
    align-items: center;
    gap: .75rem;
    flex-wrap: wrap;
}
.filter-bar label {
    margin: 0;
    font-size: .95rem;
    font-weight: 600;
    color: #334155;
}
.filter-bar select { min-width: 200px; }
.filter-bar .current {
    font-size: .9rem;
    color: #64748b;
}
.filter-bar .current strong { color: #0f172a; }
.filter-bar button {
    padding: .65rem 1rem;
    border: 0;
    border-radius: 8px;
    background: #2563eb;
    color: #fff;
    font-size: .95rem;
    cursor: pointer;
    min-height: 44px;
    font-family: inherit;
}

/* ============================================================
   Toolbar / filter actions (attendancestat)
   ============================================================ */
.toolbar {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    margin-bottom: 1rem;
}
.toolbar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}
.toolbar-header label {
    margin: 0;
    font-size: 1rem;
    font-weight: 600;
    color: #334155;
}

.filter-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    width: 100%;
}
.filter-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    align-items: center; /* Выравниваем по центру на десктопе */
}
.filter-info {
    font-size: 0.9rem;
    color: #64748b;
    margin-top: 0.25rem;
}
.filter-info strong { color: #0f172a; }

/* Кнопки фильтра (Выбрать, Очистить, Применить, Сбросить, Экспорт) */
.btn-link, .btn-primary, .btn-secondary, .btn-export {
    font-family: inherit;
    cursor: pointer;
    text-decoration: none;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: .95rem;
    font-weight: 500;
    min-height: 44px;
    line-height: 1.2; /* Фикс перекоса высоты шрифта */
    margin: 0;        /* Убираем внешние отступы */
}
.btn-link {
    background: #fff;
    border: 1px solid #cbd3da;
    padding: .65rem .9rem;
    color: #334155;
}
.btn-link:hover { background: #f1f5f9; }
.btn-primary {
    background: #2563eb;
    color: #fff;
    border: 0;
    padding: .65rem 1.1rem;
}
.btn-primary:hover  { background: #1d4ed8; }
.btn-primary:active { transform: translateY(1px); }
.btn-secondary {
    background: #fff;
    color: #334155;
    border: 1px solid #cbd3da;
    padding: .65rem 1rem;
}
.btn-secondary:hover { background: #f1f5f9; }
.btn-export {
    background: #16a34a;
    color: #fff;
    border: 0;
    padding: .75rem 1.25rem;
    min-height: 48px;
    white-space: nowrap;
}
.btn-export:hover  { background: #15803d; }
.btn-export:active { transform: translateY(1px); }

/* ============================================================
   Summary
   ============================================================ */
.summary {
    color: #475569;
    font-size: .95rem;
    margin-bottom: .85rem;
    line-height: 1.65;
}
.summary strong { color: #0f172a; }

/* ============================================================
   Month blocks
   ============================================================ */
.month-block { margin-bottom: 1.75rem; }
.month-block:last-child { margin-bottom: 0; }
.month-head {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: .5rem 1rem;
    margin: 0 0 .75rem;
    padding-bottom: .6rem;
    border-bottom: 1px solid #e2e8f0;
}
.month-head h3 { margin: 0; font-size: 1.1rem; color: #0f172a; }
.month-head .meta { color: #64748b; font-size: .9rem; }
.month-head .meta strong { color: #0f172a; }

table.mtable { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.mtable th, table.mtable td {
    padding: .55rem .7rem;
    border-bottom: 1px solid #eef1f4;
    text-align: right;
    white-space: nowrap;
}
table.mtable th:first-child,
table.mtable td:first-child { text-align: left; }
table.mtable thead th {
    background: #f8fafc;
    color: #334155;
    font-weight: 600;
}
table.mtable tbody tr:nth-child(even) td { background: #fafbfc; }
table.mtable tfoot td {
    background: #fefce8;
    font-weight: 600;
    border-top: 2px solid #e2e8f0;
}
table.mtable td.col-absent       { background: #eff6ff; font-weight: 600; color: #1d4ed8; }
table.mtable tfoot td.col-absent { background: #dbeafe; }

/* ============================================================
   Legend, muted
   ============================================================ */
.legend {
    font-size: .88rem;
    color: #64748b;
    margin-top: 1rem;
    line-height: 1.75;
}
.legend span.sw {
    display: inline-block;
    padding: .15rem .5rem;
    border-radius: 4px;
    margin-right: .2rem;
}

.muted {
    color: #94a3b8;
    font-size: .82rem;
    margin-left: .3rem;
    font-weight: 400;
}

/* ============================================================
   Select2 overrides (Fixed Scroll & Wrapping)
   ============================================================ */
.select2-container {
    width: 100% !important;
}
.select2-container .select2-selection--single {
    height: 48px !important;
    border-radius: 8px !important;
    border-color: #cbd3da !important;
}
.select2-container--default .select2-selection--single .select2-selection__rendered {
    line-height: 46px !important;
    font-size: 1rem;
    padding-left: .9rem;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 46px !important;
    width: 44px;
}
.select2-container--default.select2-container--focus .select2-selection--single {
    border-color: #2563eb !important;
}

/* Multi-select fixing horizontal scroll */
.select2-container--default .select2-selection--multiple {
    min-height: 48px;
    border: 1px solid #cbd3da;
    border-radius: 8px;
    padding: 4px 6px;
    overflow: hidden;
}
.select2-container--default.select2-container--focus .select2-selection--multiple {
    border-color: #2563eb;
}
.select2-container--default .select2-selection--multiple .select2-selection__rendered {
    display: flex !important;
    flex-wrap: wrap !important;
    white-space: normal !important;
    align-items: center;
    padding: 0;
    margin: 0;
}
.select2-container--default .select2-selection--multiple .select2-selection__choice {
    font-size: .95rem;
    padding: 3px 10px;
    margin: 2px;
    background-color: #e2e8f0;
    border: none;
    color: #334155;
}
.select2-container--default .select2-selection--multiple .select2-search__field {
    width: 100% !important;
    min-width: 150px;
    font-size: 1rem;
    padding: 4px;
    margin: 0;
}
.select2-container--default .select2-results__option {
    padding: 10px 14px;
    font-size: 1rem;
}

/* ============================================================
   Login page
   ============================================================ */
body.login-page {
    display: flex;
    min-height: 100vh;
    align-items: center;
    justify-content: center;
    padding: 2rem 1rem;
}
body.login-page .wrap { max-width: 420px; width: 100%; }
body.login-page .card h1 { font-size: 1.25rem; margin: 0 0 .35rem; }
body.login-page .subtitle {
    color: #64748b;
    font-size: .95rem;
    margin: 0 0 1.25rem;
}
body.login-page form > button[type=submit] {
    width: 100%;
    margin-top: 1.5rem;
}

/* ============================================================
   MOBILE
   ============================================================ */
@media (max-width: 768px) {
    html { font-size: 17px; }
    body { padding: 1rem .8rem calc(1rem + env(safe-area-inset-bottom)); }

    h1 { font-size: 1.4rem; }
    h2 { font-size: 1.2rem; }

    .card { padding: 1.15rem; border-radius: 12px; margin-bottom: 1.15rem; }

    .back { font-size: 1rem; min-height: 40px; padding: 8px 4px; }
    .topbar { gap: .5rem; margin-bottom: 1rem; }
    .lock-link { font-size: .95rem; padding: 10px .9rem; min-height: 44px; }

    .hero { padding: 1.5rem 1.35rem; border-radius: 12px; margin-bottom: 1.15rem; }
    .hero h1 { font-size: 1.1rem; line-height: 1.55; }
    .hero p  { font-size: 1rem; margin-top: 1rem; }

    .menu { grid-template-columns: 1fr; gap: .9rem; }
    .menu-item { padding: 1.35rem 1.4rem; min-height: 96px; border-radius: 12px; border-left-width: 5px; }
    .menu-item h2 { font-size: 1.25rem; margin-bottom: .5rem; }
    .menu-item p  { font-size: 1rem; line-height: 1.5; }
    .menu-item .tag { font-size: .78rem; padding: .25rem .75rem; margin-bottom: .75rem; }

    label { font-size: 1rem; margin: 1.15rem 0 .45rem; }
    input[type=text], input[type=email], input[type=date], input[type=number], input[type=password], select, textarea {
        font-size: 17px !important;
        padding: .95rem 1rem;
        min-height: 54px;
        border-radius: 10px;
    }
    form > button[type=submit], .btn {
        min-height: 58px;
        font-size: 1.1rem;
        padding: 1rem 1.5rem;
        width: 100%;
        border-radius: 10px;
        margin-top: 1.5rem;
    }

    .grid { grid-template-columns: 1fr; gap: .35rem; }
    .grid .full { grid-column: 1 / -1; }

    .hint { font-size: 1rem; margin-top: .5rem; }
    .err, .ok, .info { font-size: 1rem; padding: 1rem 1.1rem; margin-bottom: 1.15rem; }

    table { font-size: 1rem; }
    th, td { padding: .7rem .8rem; }
    .table-wrap { max-height: none; border-radius: 10px; }

    table.stat { font-size: .95rem; }
    table.stat th, table.stat td { padding: .55rem .65rem; }
    table.stat td.cell .num    { font-size: 1.05rem; }
    table.stat td.cell .detail { font-size: .82rem; }
    table.stat td.sticky-col   { font-size: .9rem; }
    .sticky-col .muted { display: block; font-size: .78rem; }

    .kpi { grid-template-columns: repeat(2, 1fr); gap: .6rem; }
    .kpi .box         { padding: .85rem 1rem; border-radius: 10px; }
    .kpi .box .label  { font-size: .78rem; margin-bottom: .3rem; }
    .kpi .box .value  { font-size: 1.5rem; }

    .filter-bar { gap: .75rem; flex-direction: column; align-items: stretch; }
    .filter-bar label { flex-basis: 100%; font-size: 1rem; margin: 0; }
    .filter-bar select { font-size: 17px !important; flex: 1; min-width: 0; width: 100%; }
    .filter-bar .current { flex-basis: 100%; font-size: .95rem; margin-left: 0; text-align: center; }
    .filter-bar button { width: 100%; min-height: 54px; font-size: 1.05rem; padding: .9rem 1rem; margin-top: 0.5rem; }

    .toolbar { flex-direction: column; gap: 1rem; }
    .toolbar-header { flex-direction: column; align-items: flex-start; gap: 0.75rem; width: 100%; }
    .toolbar-header .btn-export { width: 100%; justify-content: center; margin: 0; }
    
    .filter-form { flex-direction: column; width: 100%; gap: .75rem; }
    
    .filter-actions { 
        width: 100%; 
        gap: .5rem; 
        align-items: stretch; /* На мобилке растягиваем кнопки по высоте */
    }
    .filter-actions .btn-link { flex: 1; min-height: 50px; font-size: 1rem; padding: .8rem .5rem; }
    .filter-actions .btn-primary, .filter-actions .btn-secondary { flex: 1; min-height: 50px; font-size: 1rem; }
    .filter-info { font-size: .95rem; text-align: center; width: 100%; }

    .month-block { overflow-x: auto; -webkit-overflow-scrolling: touch; margin-bottom: 1.5rem; padding-bottom: .5rem; }
    .month-head { gap: .4rem .8rem; margin-bottom: .85rem; }
    .month-head h3 { font-size: 1.15rem; }
    .month-head .meta { font-size: .95rem; }
    table.mtable { min-width: 600px; font-size: 1rem; }
    table.mtable th, table.mtable td { padding: .65rem .75rem; }

    /* Select2 Mobile overrides */
    .select2-container--default .select2-results__option { padding: 12px 14px; font-size: 1rem; }
    .select2-container--default .select2-selection--multiple { padding: 8px; }
    .select2-container--default .select2-selection--multiple .select2-search__field { font-size: 17px !important; }
    .select2-container--default .select2-selection--multiple .select2-selection__choice { font-size: 1rem; padding: 6px 12px; margin: 4px; }

    .legend { font-size: .9rem; line-height: 1.8; }
    .muted  { font-size: .85rem; }

    body.login-page { padding: 1rem .8rem; align-items: flex-start; }
    body.login-page .card h1 { font-size: 1.35rem; }
    body.login-page .subtitle { font-size: 1rem; }

    .summary { font-size: 1rem; line-height: 1.7; text-align: center; }
}