:root {
    --bg: #f4f6f8;
    --panel: #ffffff;
    --ink: #1c2430;
    --muted: #66748a;
    --line: #dde3ea;
    --accent: #2563eb;
    --overdue: #dc2626;
    --critical: #ea580c;
    --warning: #ca8a04;
    --soon: #16a34a;
    --ok: #94a3b8;
    --radius: 10px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font: 15px/1.5 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ------------------------------ каркас ------------------------------ */

header.top {
    background: #12233d;
    color: #fff;
    padding: 0 20px;
    display: flex;
    align-items: center;
    gap: 22px;
    flex-wrap: wrap;
    position: sticky;
    top: 0;
    z-index: 20;
}
header.top .brand { font-weight: 700; padding: 14px 0; letter-spacing: .2px; }
header.top nav { display: flex; gap: 16px; flex-wrap: wrap; flex: 1; }
header.top nav a { color: #c8d6ea; padding: 14px 0; border-bottom: 2px solid transparent; }
header.top nav a:hover { color: #fff; text-decoration: none; }
header.top nav a.active { color: #fff; border-bottom-color: #4d8dfd; }
header.top .who { font-size: 13px; color: #9fb2cc; }

main { max-width: 1360px; margin: 0 auto; padding: 22px 20px 60px; }

h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 17px; margin: 26px 0 10px; }
h3 { font-size: 15px; margin: 18px 0 8px; }

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 16px 18px;
    margin-bottom: 18px;
}
.row { display: flex; gap: 16px; flex-wrap: wrap; align-items: flex-start; }
.row > * { flex: 1 1 320px; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ------------------------------ плитки ------------------------------ */

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.tile {
    background: var(--panel);
    border: 1px solid var(--line);
    border-left: 4px solid var(--ok);
    border-radius: var(--radius);
    padding: 14px 16px;
    display: block;
}
.tile:hover { text-decoration: none; box-shadow: 0 2px 10px rgba(20, 35, 60, .08); }
.tile .n { font-size: 28px; font-weight: 700; line-height: 1.1; }
.tile .t { font-size: 13px; color: var(--muted); }
.tile.overdue  { border-left-color: var(--overdue); }
.tile.critical { border-left-color: var(--critical); }
.tile.warning  { border-left-color: var(--warning); }
.tile.soon     { border-left-color: var(--soon); }

/* ------------------------------ таблицы ----------------------------- */

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
tbody tr:hover { background: #f8fafc; }
.table-wrap { overflow-x: auto; }

.badge {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    border: 1px solid currentColor;
}
.badge.overdue  { color: var(--overdue); background: #fef2f2; }
.badge.critical { color: var(--critical); background: #fff7ed; }
.badge.warning  { color: var(--warning); background: #fefce8; }
.badge.soon     { color: var(--soon); background: #f0fdf4; }
.badge.ok       { color: var(--ok); background: #f8fafc; }
.badge.grey     { color: var(--muted); background: #f1f5f9; }

/* ------------------------------- формы ------------------------------ */

label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 3px; }
input[type=text], input[type=email], input[type=password], input[type=date],
input[type=number], input[type=search], select, textarea {
    width: 100%;
    padding: 7px 9px;
    border: 1px solid var(--line);
    border-radius: 7px;
    font: inherit;
    background: #fff;
    color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #bfd5ff; border-color: var(--accent); }
textarea { min-height: 70px; resize: vertical; }
.field { margin-bottom: 12px; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.checks { display: flex; flex-wrap: wrap; gap: 10px 18px; }
.checks label { display: flex; align-items: center; gap: 6px; color: var(--ink); font-size: 14px; }
.checks input { width: auto; }

button, .btn {
    display: inline-block;
    padding: 7px 14px;
    border: 1px solid var(--accent);
    background: var(--accent);
    color: #fff;
    border-radius: 7px;
    font: inherit;
    cursor: pointer;
}
button:hover, .btn:hover { filter: brightness(1.07); text-decoration: none; }
.btn.ghost, button.ghost { background: #fff; color: var(--accent); }
.btn.danger, button.danger { background: #fff; color: var(--overdue); border-color: var(--overdue); }
.btn.small, button.small { padding: 3px 9px; font-size: 13px; }
.actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 12px; }

form.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
form.filters .field { margin: 0; min-width: 150px; }

/* ------------------------------ сообщения --------------------------- */

.flash { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 14px; border: 1px solid; }
.flash.ok { background: #f0fdf4; border-color: #bbf7d0; color: #14532d; }
.flash.error { background: #fef2f2; border-color: #fecaca; color: #7f1d1d; }

details.inline > summary { cursor: pointer; color: var(--accent); font-size: 14px; }
details.inline[open] > summary { margin-bottom: 10px; }

.login {
    max-width: 380px;
    margin: 90px auto;
}
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }

/* --------------------------- крошки / «назад» ----------------------- */

.crumbs { font-size: 13px; margin: 0 0 10px; color: var(--muted); }
.crumbs a { color: var(--muted); }
.crumbs a:hover { color: var(--accent); }

/* ------------------------ сетка чекбоксов --------------------------- */

.checks-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 8px 18px;
}

/* ------------------------- лента событий ---------------------------- */

.timeline { list-style: none; margin: 4px 0 0; padding: 0; }
.timeline li {
    position: relative;
    padding: 0 0 14px 20px;
    border-left: 2px solid var(--line);
}
.timeline li::before {
    content: "";
    position: absolute;
    left: -6px; top: 4px;
    width: 10px; height: 10px;
    border-radius: 50%;
    background: var(--accent);
    border: 2px solid var(--panel);
}
.timeline li:last-child { border-left-color: transparent; }
.tl-date { display: block; font-size: 12px; color: var(--muted); }
.tl-title { display: block; font-weight: 600; }
.tl-detail { display: block; }

/* ------------------------------ вложения ---------------------------- */

.attach-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
}
.attach {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 8px;
    background: #fbfcfe;
}
.attach img {
    width: 100%;
    height: 120px;
    object-fit: cover;
    border-radius: 6px;
    display: block;
    background: #eef2f7;
}
.attach-file {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 120px;
    font-size: 44px;
    text-decoration: none;
    background: #eef2f7;
    border-radius: 6px;
}
.attach-meta { margin-top: 6px; word-break: break-word; }
.attach-meta form { margin-top: 4px; }
input[type=file] { width: 100%; font: inherit; padding: 6px 0; }

/* ----------------------------- мобильные ---------------------------- */

@media (max-width: 720px) {
    header.top {
        gap: 0;
        padding: 0 12px;
        align-items: stretch;
        flex-direction: column;
    }
    header.top .brand { padding: 12px 0 8px; }
    header.top nav {
        gap: 14px;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 2px;
        order: 3;
        /* Без этого Safari тянет nav по ширине содержимого вместо экрана,
           и overflow-x:auto не срабатывает — строка обрезается, не скроллясь. */
        min-width: 0;
        width: 100%;
        -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent);
        mask-image: linear-gradient(to right, #000 calc(100% - 18px), transparent);
    }
    header.top nav a { padding: 8px 0; white-space: nowrap; flex: none; }
    header.top .who { order: 2; padding: 4px 0; }
    header.top form { position: absolute; top: 10px; right: 12px; }

    main { padding: 14px 12px 48px; }
    h1 { font-size: 20px; }

    /* На узком экране широкие таблицы карточек — в одну колонку. */
    .row > * { flex-basis: 100%; }
    form.filters { flex-direction: column; align-items: stretch; }
    form.filters .field { min-width: 0; }
    form.filters button, form.filters .btn { width: 100%; text-align: center; }

    /* Таблицы деталей (th|td парами) складываем построчно. */
    table.stack, table.stack tbody, table.stack tr, table.stack th, table.stack td {
        display: block; width: 100%;
    }
    table.stack th { border: 0; padding-bottom: 0; }
    table.stack td { padding-top: 2px; border-bottom: 1px solid var(--line); }

    .actions { flex-direction: column; align-items: stretch; }
    .actions button, .actions .btn { width: 100%; text-align: center; }
}
