:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-alt: #f1f3f8;
    --border: #e3e6ee;
    --text: #1f2937;
    --text-muted: #6b7280;
    --primary: #3b6cff;
    --primary-hover: #2c55d6;
    --primary-soft: #e6edff;
    --danger: #dc3a3a;
    --warning: #c97a0a;
    --success: #2f9e6f;
    --radius: 8px;
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, .04), 0 1px 3px rgba(15, 23, 42, .06);
    --shadow-md: 0 8px 20px -8px rgba(15, 23, 42, .25);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Ubuntu, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1.45;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

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

/* ===== AUTH ===== */
body.auth {
    display: grid;
    place-items: center;
    min-height: 100vh;
    background: linear-gradient(135deg, #e8efff 0%, #f4f6fb 60%);
}
.auth-card {
    background: var(--surface);
    width: min(380px, 92vw);
    padding: 36px 32px;
    border-radius: 14px;
    box-shadow: 0 25px 50px -20px rgba(31, 41, 55, .25);
}
.auth-card h1 {
    margin: 0 0 6px;
    text-align: center;
    font-size: 22px;
    color: var(--primary);
}
.auth-card .subtitle {
    margin: 0 0 24px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}

.warning {
    background: #fff7e6;
    color: #8a5a00;
    border: 1px solid #f3d997;
    padding: 10px 12px;
    border-radius: var(--radius);
    margin-bottom: 16px;
    font-size: 13px;
}
.error-box {
    background: #fde9e8;
    color: #a31f1c;
    border: 1px solid #f5b5b3;
    padding: 8px 12px;
    border-radius: var(--radius);
    font-size: 13px;
}

/* ===== FORMS ===== */
.form { display: grid; gap: 14px; }
.form label {
    display: grid;
    gap: 6px;
    font-size: 12.5px;
    color: var(--text-muted);
    font-weight: 500;
}
.form input,
.form select,
.form textarea {
    font: inherit;
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 9px 11px;
    background: #fff;
    outline: none;
    width: 100%;
}
.form textarea { min-height: 80px; resize: vertical; }
.form input:focus,
.form select:focus,
.form textarea:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 108, 255, .15);
}
.form .row-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

/* ===== BUTTONS ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font: inherit;
    font-weight: 500;
    padding: 8px 14px;
    border-radius: var(--radius);
    border: 1px solid transparent;
    cursor: pointer;
    transition: background .12s, border-color .12s, color .12s, filter .12s;
    white-space: nowrap;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:hover:not(:disabled) { filter: brightness(.92); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-alt); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover:not(:disabled) { filter: brightness(.92); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; }

/* ===== APP LAYOUT ===== */
body.app {
    display: grid;
    grid-template-columns: 240px 1fr;
    min-height: 100vh;
}
.sidebar {
    background: #18213a;
    color: #e2e8f0;
    padding: 18px 12px 24px;
    display: flex;
    flex-direction: column;
    position: sticky;
    top: 0;
    height: 100vh;
    overflow-y: auto;
}
.sidebar .brand {
    display: flex;
    align-items: center;
    gap: 9px;
    font-size: 18px;
    font-weight: 600;
    letter-spacing: .3px;
    padding: 6px 12px 20px;
    color: #fff;
}
.sidebar .brand img {
    width: 31px;
    height: 31px;
    object-fit: contain;
    flex: 0 0 auto;
}
.sidebar nav { display: flex; flex-direction: column; gap: 1px; }
.sidebar .nav-group {
    font-size: 10.5px;
    text-transform: uppercase;
    letter-spacing: .09em;
    color: #6b7593;
    padding: 14px 12px 4px;
}
.sidebar a {
    color: #cdd5e3;
    text-decoration: none;
    padding: 7px 12px;
    border-radius: 6px;
    font-size: 13.5px;
    display: block;
}
.sidebar a:hover { background: rgba(255, 255, 255, .06); color: #fff; text-decoration: none; }
.sidebar a.active { background: var(--primary); color: #fff; }
.equipment-subnav { margin: 4px 0 8px 10px; padding-left: 8px; border-left: 1px solid #6b7593; }
.equipment-subnav[hidden] { display: none; }
.equipment-groups { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.equipment-groups h2 { margin: 0 0 12px; font-size: 16px; }
.equipment-links { margin: 0; padding-left: 18px; }
.equipment-links li + li { margin-top: 10px; }

.main {
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 5;
}
.topbar h1 {
    font-size: 18px;
    margin: 0;
    font-weight: 600;
}
.topbar-right {
    display: flex;
    align-items: center;
    gap: 14px;
}
.organization-picker { display: flex; align-items: center; gap: 6px; }
.organization-picker select {
    max-width: 250px;
    min-width: 140px;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text);
    font: inherit;
}
.form label.organization-access-option { display: flex; flex-direction: row; align-items: center; gap: 10px; }
.organization-access-option input[type="checkbox"] { width: auto; }
@media (max-width: 800px) {
    .topbar { flex-wrap: wrap; gap: 10px; }
    .topbar-right { flex-wrap: wrap; gap: 8px; }
    .organization-picker select { max-width: 200px; }
}
#user-info {
    font-size: 13px;
    color: var(--text-muted);
}
.view {
    padding: 24px;
    flex: 1;
}

/* ===== TOOLBAR + TABLE ===== */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 14px;
    gap: 12px;
    flex-wrap: wrap;
}
.toolbar .search { flex: 1; min-width: 160px; max-width: 320px; }
.toolbar .search input {
    width: 100%;
    font: inherit;
    padding: 8px 11px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fff;
}
.toolbar .search input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 108, 255, .15);
    outline: none;
}

.table-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: auto;
    box-shadow: var(--shadow-sm);
}
table.data {
    width: 100%;
    border-collapse: collapse;
    font-size: 13.5px;
}
table.data th,
table.data td {
    padding: 10px 14px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
    white-space: nowrap;
}
table.data td.wrap { white-space: normal; }
table.data th {
    background: var(--surface-alt);
    color: var(--text-muted);
    font-weight: 600;
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: .05em;
}
table.data tr:last-child td { border-bottom: none; }
table.data tr:hover td { background: #fafbfd; }
.row-actions {
    display: flex;
    gap: 6px;
    justify-content: flex-end;
}

/* ===== FILES ===== */
.files-description { max-width: 680px; }
.file-name {
    display: grid;
    gap: 2px;
    min-width: 180px;
    max-width: 320px;
}
.file-name strong,
.file-name span {
    overflow: hidden;
    text-overflow: ellipsis;
}
.file-name span { font-size: 12px; }
.file-link-actions {
    display: flex;
    gap: 6px;
}
.file-upload-note { margin-top: 12px; }
.downloads-panel:not(:empty) {
    margin-top: 22px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
}
.downloads-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 12px;
}
.downloads-heading h2 {
    margin: 0 0 3px;
    font-size: 16px;
}
.downloads-table { max-width: 640px; }
.empty {
    padding: 48px 24px;
    text-align: center;
    color: var(--text-muted);
}
.pill {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--surface-alt);
    font-size: 12px;
}
.pill-primary { background: var(--primary-soft); color: var(--primary); }

/* ===== LINK CHIPS (несколько ссылок в ячейке) ===== */
.link-cell {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    white-space: normal;
    max-width: 320px;
}
.link-chip {
    display: inline-flex;
    align-items: center;
    max-width: 180px;
    padding: 2px 9px;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: var(--surface-alt);
    font-size: 12px;
    color: var(--primary);
    text-decoration: none;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.link-chip:hover { background: var(--primary-soft); text-decoration: none; }

/* ===== LINKS EDITOR (в форме) ===== */
.links-editor { display: grid; gap: 8px; }
.links-list { display: grid; gap: 8px; }
.link-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr) auto;
    gap: 8px;
    align-items: center;
}
.link-row .btn { padding: 6px 10px; }

/* ===== MODAL ===== */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, .45);
    display: grid;
    place-items: center;
    z-index: 50;
    padding: 16px;
}
.modal {
    background: var(--surface);
    border-radius: 10px;
    width: min(540px, 100%);
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: var(--shadow-md);
    padding: 24px 26px;
}
.modal h2 {
    margin: 0 0 18px;
    font-size: 18px;
}
.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 22px;
}

/* ===== TOAST ===== */
#toast-root {
    position: fixed;
    right: 18px;
    bottom: 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 100;
}
.toast {
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 3px solid var(--primary);
    padding: 10px 14px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    font-size: 13px;
    min-width: 220px;
    animation: toast-in .18s ease;
}
.toast.ok { border-left-color: var(--success); }
.toast.err { border-left-color: var(--danger); }
@keyframes toast-in {
    from { transform: translateY(8px); opacity: 0; }
    to { transform: none; opacity: 1; }
}

/* ===== DASHBOARD ===== */
.section-title {
    margin: 0 0 12px;
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--text-muted);
    font-weight: 600;
}
.task-stats-section { margin-bottom: 26px; }
.task-stats-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 12px;
}
.task-stat-tile {
    display: flex;
    align-items: center;
    gap: 13px;
    min-height: 92px;
    padding: 15px 17px;
    border-radius: 12px;
    color: #fff;
    text-decoration: none;
    box-shadow: var(--shadow-md);
    transition: transform .12s, filter .12s;
}
.task-stat-tile:hover {
    color: #fff;
    text-decoration: none;
    transform: translateY(-2px);
    filter: brightness(1.05);
}
.task-stat-badge {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    border-radius: 10px;
    background: rgba(255, 255, 255, .18);
    font-size: 15px;
    font-weight: 700;
}
.task-stat-value {
    font-size: 27px;
    font-weight: 700;
    line-height: 1;
}
.task-stat-label {
    margin-top: 6px;
    font-size: 12.5px;
    line-height: 1.25;
    opacity: .95;
}
.task-todo  { background: linear-gradient(135deg, #ef7d32 0%, #dc3a3a 100%); }
.task-today { background: linear-gradient(135deg, #24a56f 0%, #12b886 100%); }
.task-week  { background: linear-gradient(135deg, #3b6cff 0%, #397ad7 100%); }
.task-month { background: linear-gradient(135deg, #7758d9 0%, #9b51cf 100%); }
.task-year  { background: linear-gradient(135deg, #43506d 0%, #61708f 100%); }
.status-section { margin-bottom: 26px; }
.status-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
    gap: 16px;
}
.status-block {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 20px 22px;
    border-radius: 12px;
    color: #fff;
    text-decoration: none;
    box-shadow: var(--shadow-md);
    transition: transform .12s, filter .12s;
}
.status-block:hover {
    text-decoration: none;
    transform: translateY(-2px);
    filter: brightness(1.05);
}
.status-block .sb-icon {
    font-size: 34px;
    line-height: 1;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, .18));
}
.status-block .sb-value {
    font-size: 34px;
    font-weight: 700;
    line-height: 1;
}
.status-block .sb-label {
    font-size: 13.5px;
    opacity: .95;
    margin-top: 6px;
}
.sb-wait     { background: linear-gradient(135deg, #f6a723 0%, #ef6f3c 100%); }
.sb-progress { background: linear-gradient(135deg, #3b6cff 0%, #6a4bff 100%); }
.sb-ready    { background: linear-gradient(135deg, #22a06b 0%, #17c08a 100%); }

.cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 14px;
    margin-bottom: 22px;
}
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 18px;
    box-shadow: var(--shadow-sm);
}
.card .label {
    color: var(--text-muted);
    font-size: 12.5px;
    text-transform: uppercase;
    letter-spacing: .05em;
}
.card .value {
    font-size: 26px;
    font-weight: 600;
    margin-top: 6px;
    color: var(--text);
}
.card .card-sub {
    margin-top: 4px;
    font-size: 12.5px;
    color: var(--text-muted);
}

/* ===== TASKS ===== */
.task-filters {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    flex-wrap: wrap;
}
.task-filters .search { width: min(320px, 100%); }
.task-status-filter {
    font: inherit;
    color: var(--text);
    padding: 8px 32px 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fff;
    outline: none;
}
.task-status-filter:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 108, 255, .15);
}
.tasks-table { min-width: 980px; }
.tasks-table .task-text {
    min-width: 260px;
    max-width: 480px;
    line-height: 1.45;
}
.task-completed .task-text {
    color: var(--text-muted);
    text-decoration: line-through;
}
.urgency-pill {
    display: inline-block;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
}
.urgency-urgent { background: #fde9e8; color: #b52b28; }
.urgency-moderate { background: #fff1d6; color: #9a6200; }
.urgency-not-urgent { background: #e9f7f0; color: #257a56; }
.task-pending-pill { background: var(--primary-soft); color: var(--primary); }
.task-done-pill { background: #e9f7f0; color: #257a56; }
.task-done-date {
    display: grid;
    justify-items: start;
    gap: 4px;
}
.task-done-date .muted { font-size: 11.5px; }

/* ===== DETAIL PAGE ===== */
.detail-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px 22px;
    box-shadow: var(--shadow-sm);
    margin-bottom: 18px;
}
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 14px 24px;
}
.detail-item { display: grid; gap: 3px; }
.detail-label {
    font-size: 11.5px;
    text-transform: uppercase;
    letter-spacing: .05em;
    color: var(--text-muted);
}
.detail-value {
    font-size: 15px;
    color: var(--text);
    font-weight: 500;
}

/* ===== RECONCILE BANNER (сверка сумм счёта) ===== */
.reconcile {
    margin: 2px 0 18px;
    padding: 12px 16px;
    border-radius: var(--radius);
    font-size: 14px;
    font-weight: 500;
    border: 1px solid transparent;
}
.reconcile-ok {
    background: #e9f7f0;
    color: #1f7a52;
    border-color: #b6e2cd;
}
.reconcile-err {
    background: #fde9e8;
    color: #a31f1c;
    border-color: #f5b5b3;
}

/* ===== COMPATIBILITY ===== */
.compat-layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 16px;
}
.compat-list {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    max-height: 70vh;
    overflow-y: auto;
}
.compat-search {
    padding: 10px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--surface);
    z-index: 1;
}
.compat-search input {
    width: 100%;
    font: inherit;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fff;
}
.compat-search input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 108, 255, .15);
    outline: none;
}
.compat-list .item {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    font-size: 13.5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.compat-list .item:hover { background: var(--surface-alt); }
.compat-list .item.active { background: var(--primary-soft); color: var(--primary); font-weight: 500; }
.compat-list .item:last-child { border-bottom: none; }

@media (max-width: 800px) {
    body.app { grid-template-columns: 1fr; }
    .sidebar { position: static; height: auto; }
    .compat-layout { grid-template-columns: 1fr; }
    .form .row-2 { grid-template-columns: 1fr; }
}
