:root {
    --bg: #1e1e1e;
    --bg-sidebar: #181818;
    --bg-card: #262626;
    --bg-hover: #2a2a2a;
    --bg-input: #1a1a1a;
    --bg-table: #1f1f1f;
    --border: #303030;
    --text: #dcddde;
    --muted: #7f7f7f;
    --dim: #5a5a5a;
    --accent: #7f6df2;
    --success: #4caf6d;
    --warning: #e0a458;
    --danger: #e26a6a;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.4 -apple-system, "Segoe UI", system-ui, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ===== TOPBAR ===== */
.topbar {
    display: flex; align-items: center; gap: 24px;
    padding: 14px 28px;
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 700; font-size: 16px; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a {
    padding: 8px 14px; border-radius: 6px;
    color: var(--muted); transition: .15s;
}
.topbar nav a:hover { background: var(--bg-hover); color: var(--text); text-decoration: none; }
.topbar nav a.active { background: var(--accent); color: white; }
.rightside { display: flex; gap: 12px; align-items: center; }
#globalSearch {
    background: var(--bg-input); color: var(--text);
    border: 1px solid var(--border); padding: 7px 12px;
    border-radius: 6px; width: 240px; font-size: 13px;
}
#globalSearch:focus { outline: 1px solid var(--accent); }
.logout {
    color: var(--muted); font-size: 13px;
    padding: 7px 12px; border: 1px solid var(--border); border-radius: 6px;
}
.logout:hover { background: var(--bg-hover); text-decoration: none; }

/* ===== AUTOCOMPLETE ===== */
.search-results {
    position: fixed; top: 60px; right: 28px;
    width: 320px; max-height: 400px; overflow-y: auto;
    background: var(--bg-card); border: 1px solid var(--border);
    border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,0.4);
    z-index: 20;
}
.search-results a {
    display: flex; justify-content: space-between;
    padding: 10px 14px; border-bottom: 1px solid var(--border);
    color: var(--text);
}
.search-results a:last-child { border-bottom: 0; }
.search-results a:hover { background: var(--bg-hover); text-decoration: none; }
.search-results .meta { color: var(--muted); font-size: 12px; }

/* ===== LAYOUT ===== */
main { padding: 28px 32px 60px; max-width: 1600px; margin: 0 auto; }
h1 { margin: 0 0 24px; font-size: 22px; }
h2 { margin: 0 0 14px; font-size: 16px; color: var(--text); }
.muted { color: var(--muted); font-size: 13px; }
.back { display: inline-block; margin-bottom: 8px; color: var(--muted); }

/* ===== KPI ===== */
.kpi-grid {
    display: grid; gap: 14px;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    margin-bottom: 28px;
}
.kpi {
    background: var(--bg-card); padding: 18px 20px;
    border: 1px solid var(--border); border-radius: 10px;
}
.kpi .num { font-size: 26px; font-weight: 700; color: var(--text); }
.kpi .lbl { color: var(--muted); font-size: 12px; margin-top: 4px; text-transform: uppercase; letter-spacing: 0.5px; }
.kpi.warn .num { color: var(--warning); }
.kpi-row { display: flex; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.kpi-mini {
    background: var(--bg-card); padding: 10px 16px;
    border: 1px solid var(--border); border-radius: 6px; font-size: 13px;
}
.kpi-mini b { color: var(--accent); margin-left: 4px; }

/* ===== CARD ===== */
.card {
    background: var(--bg-card); padding: 20px 24px;
    border: 1px solid var(--border); border-radius: 10px;
    margin-bottom: 24px;
}
.two-col {
    display: grid; gap: 24px; grid-template-columns: 1fr 1fr;
}
@media (max-width: 1000px) { .two-col { grid-template-columns: 1fr; } }

/* ===== TABLES ===== */
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
    text-align: left; padding: 10px 12px;
    color: var(--muted); font-weight: 500; font-size: 11px;
    text-transform: uppercase; letter-spacing: 0.5px;
    border-bottom: 1px solid var(--border);
}
td {
    padding: 10px 12px; border-bottom: 1px solid var(--border);
    vertical-align: top;
}
tr:hover td { background: var(--bg-hover); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.dense th, .dense td { padding: 6px 10px; font-size: 12px; }
.comment { color: var(--muted); max-width: 240px; }

/* kv table for detail page */
table.kv { width: auto; }
table.kv th { width: 200px; }

/* ===== BADGES / TAGS ===== */
.badge {
    display: inline-block; padding: 2px 8px;
    border-radius: 4px; font-size: 11px; font-weight: 600;
}
.badge.ok { background: rgba(76,175,109,0.2); color: var(--success); }
.badge.warn { background: rgba(224,164,88,0.2); color: var(--warning); }
.side-ours { background: rgba(127,109,242,0.18); color: var(--accent); }
.side-theirs { background: rgba(180,180,180,0.18); color: var(--muted); }
.kind {
    padding: 2px 7px; border-radius: 4px; font-size: 11px; font-weight: 500;
}
.kind-done { background: rgba(76,175,109,0.2); color: var(--success); }
.kind-in_progress { background: rgba(224,164,88,0.2); color: var(--warning); }
.kind-failed { background: rgba(226,106,106,0.2); color: var(--danger); }
.kind-cancelled { background: rgba(180,180,180,0.15); color: var(--muted); }
.kind-other { background: rgba(127,109,242,0.15); color: var(--accent); }

/* ===== FILTERS ===== */
.filters {
    display: flex; gap: 10px; flex-wrap: wrap;
    margin-bottom: 20px; align-items: center;
}
.filters input, .filters select {
    background: var(--bg-input); color: var(--text);
    border: 1px solid var(--border); padding: 8px 12px;
    border-radius: 6px; font-size: 13px;
}
.filters input { min-width: 220px; }
.filters input:focus, .filters select:focus { outline: 1px solid var(--accent); }
.filters button {
    background: var(--accent); color: white; border: 0;
    padding: 8px 18px; border-radius: 6px; cursor: pointer;
    font-size: 13px; font-weight: 500;
}
.filters button:hover { filter: brightness(1.1); }
.filters .reset {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px; border-radius: 6px;
    background: var(--bg-card); color: var(--muted);
}

/* ===== LOGIN ===== */
.login-box {
    max-width: 380px; margin: 100px auto;
    padding: 32px; background: var(--bg-card);
    border: 1px solid var(--border); border-radius: 12px;
    text-align: center;
}
.login-box h1 { font-size: 28px; margin: 0 0 4px; }
.login-box p { margin: 0 0 24px; }
.login-box input {
    width: 100%; background: var(--bg-input); color: var(--text);
    border: 1px solid var(--border); padding: 12px 16px;
    border-radius: 6px; font-size: 14px; margin-bottom: 12px;
}
.login-box input:focus { outline: 1px solid var(--accent); }
.login-box button {
    width: 100%; background: var(--accent); color: white;
    border: 0; padding: 12px; border-radius: 6px;
    font-size: 14px; font-weight: 500; cursor: pointer;
}
.login-box .big-btn {
    display: block; text-decoration: none;
    background: #229ED9; color: #fff;
    border-radius: 10px; padding: 14px 16px;
    font-size: 16px; font-weight: 600;
    margin: 8px 0 12px;
}
.login-box .big-btn:hover { background: #1c8abf; }

/* ===== ПЛАШКА «ВОЗМОЖНЫЕ ДУБЛИ» ===== */
.dup-warn {
    background: rgba(224,164,88,0.13);
    border: 1px solid var(--warning);
    border-left: 4px solid var(--warning);
    border-radius: 10px;
    padding: 12px 16px; margin: 0 0 16px;
}
.dup-warn b { color: var(--warning); }
.dup-warn ul { margin: 8px 0 6px; padding-left: 20px; }
.dup-warn li { margin: 3px 0; }
.dup-warn li b { color: var(--text); }
.dup-warn .note { color: var(--muted); font-size: 12px; margin-left: 8px; }
.dup-warn .hint { color: var(--muted); font-size: 12px; }
.login-box .or-sep {
    color: var(--muted); font-size: 13px;
    margin: 14px 0; opacity: .8;
}
.login-box .tg-widget-wrap { display: flex; justify-content: center; margin: 6px 0 14px; }
.login-box .hint {
    color: var(--muted); font-size: 12.5px;
    margin-top: 18px; line-height: 1.5;
}
.login-box .err {
    background: rgba(226,106,106,0.15); color: var(--danger);
    border-radius: 6px; padding: 8px 10px; font-size: 13px; margin: 8px 0 0;
}
.login-box code {
    background: var(--bg-input); padding: 2px 6px;
    border-radius: 4px; font-size: 13px;
}

/* ===== FLASH ===== */
.flash {
    padding: 12px 20px; margin: 12px auto; max-width: 600px;
    border-radius: 6px; text-align: center;
}
.flash.error { background: rgba(226,106,106,0.18); color: var(--danger); }

/* ===== МОБИЛЬНАЯ НИЖНЯЯ НАВИГАЦИЯ ===== */
.bottom-nav {
    display: none;
    position: fixed; bottom: 0; left: 0; right: 0;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border);
    padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
    z-index: 100;
}
.bottom-nav a {
    flex: 1; display: flex; flex-direction: column; align-items: center;
    gap: 2px; padding: 8px 4px; color: var(--muted);
    font-size: 11px; text-decoration: none;
}
.bottom-nav a .icon { font-size: 20px; }
.bottom-nav a.active { color: var(--accent); }

/* ===== РЕЖИМ TELEGRAM MINI APP ===== */
html.in-telegram .topbar { display: none; }
html.in-telegram .bottom-nav { display: flex; }
html.in-telegram main {
    padding: 14px 14px 80px;
    max-width: 100%;
}
html.in-telegram h1 { font-size: 18px; margin-bottom: 14px; }
html.in-telegram h2 { font-size: 14px; }
html.in-telegram .kpi-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}
html.in-telegram .kpi { padding: 12px 14px; }
html.in-telegram .kpi .num { font-size: 20px; }
html.in-telegram .kpi .lbl { font-size: 10px; }
html.in-telegram .two-col { grid-template-columns: 1fr; gap: 14px; }
html.in-telegram .card { padding: 14px; }
html.in-telegram table { font-size: 12px; }
html.in-telegram th, html.in-telegram td { padding: 6px 8px; }
html.in-telegram .dense th, html.in-telegram .dense td { padding: 4px 6px; font-size: 11px; }
html.in-telegram .filters { gap: 6px; }
html.in-telegram .filters input { min-width: 0; width: 100%; }
html.in-telegram .login-box { margin: 40px 20px; padding: 24px; }

/* На любом мобильном — нижняя навигация удобнее верхней */
@media (max-width: 720px) {
    .topbar { padding: 10px 14px; gap: 12px; }
    .topbar nav { display: none; }
    .topbar .rightside #globalSearch { width: 140px; }
    .bottom-nav { display: flex; }
    main { padding-bottom: 80px; }
}

/* ===== ПЛИТКИ БЫСТРЫХ ДЕЙСТВИЙ (главная) ===== */
.quick-tiles {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin: 8px 0 26px;
}
.tile {
    display: flex; flex-direction: column; align-items: center;
    text-align: center; gap: 4px;
    padding: 18px 12px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 14px;
    color: var(--text); text-decoration: none;
    transition: .15s;
}
.tile:hover {
    text-decoration: none;
    border-color: var(--accent);
    background: var(--bg-hover);
    transform: translateY(-2px);
}
.tile .t-ico { font-size: 30px; line-height: 1; }
.tile .t-name { font-weight: 600; font-size: 15px; }
.tile .t-sub { font-size: 11px; color: var(--muted); }

/* Поиск на главной */
.home-search { margin: 4px 0 20px; }
.home-search input {
    width: 100%;
    background: var(--bg-input);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 13px 16px;
    font-size: 15px;
}
.home-search input:focus { outline: 1px solid var(--accent); border-color: var(--accent); }

/* Подпись секции */
.section-label {
    font-size: 13px; text-transform: uppercase; letter-spacing: .5px;
    color: var(--muted); margin: 22px 0 10px;
}

/* В мини-аппе / на мобильном плитки — 2 в ряд и крупнее */
html.in-telegram .quick-tiles { grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (max-width: 720px) {
    .quick-tiles { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .tile { padding: 20px 12px; }
    .tile .t-ico { font-size: 32px; }
}

/* ===== ЗАМЕТНЕЕ АКТИВНЫЙ ПУНКТ НИЖНЕЙ НАВИГАЦИИ ===== */
.bottom-nav a {
    min-height: 52px; justify-content: center;
    position: relative; transition: .15s;
}
.bottom-nav a.active {
    color: var(--accent);
}
.bottom-nav a.active .icon {
    transform: scale(1.15);
}
.bottom-nav a.active::before {
    content: ""; position: absolute; top: 0; left: 50%;
    transform: translateX(-50%);
    width: 28px; height: 3px; border-radius: 0 0 3px 3px;
    background: var(--accent);
}
.bottom-nav a .lbl { font-weight: 500; }
