:root {
    --ink: #172033;
    --muted: #697386;
    --line: #e6e9ef;
    --panel: #ffffff;
    --bg: #f5f7fb;
    --nav: #111827;
    --blue: #2563eb;
    --blue-dark: #1d4ed8;
    --green: #15803d;
    --orange: #ea580c;
    --red: #dc2626;
    --shadow: 0 14px 40px rgba(22, 32, 51, .08);
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body {
    margin: 0;
    min-height: 100vh;
    color: var(--ink);
    background: var(--bg);
    font: 15px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; }
button { cursor: pointer; }

.app-shell { min-height: 100vh; display: grid; grid-template-columns: 238px minmax(0, 1fr); }
.sidebar {
    position: sticky; top: 0; height: 100vh; padding: 24px 18px;
    display: flex; flex-direction: column; color: #dbe4f2; background: var(--nav);
}
.logo { display: flex; align-items: center; gap: 11px; margin: 0 8px 34px; letter-spacing: .04em; }
.logo b { display: block; color: #fff; font-size: 18px; line-height: 1; }
.logo small { display: block; margin-top: 5px; color: #92a1b7; font-size: 9px; letter-spacing: .28em; }
.brand-mark {
    width: 58px; height: 58px; border-radius: 18px; display: grid; place-items: center;
    color: white; background: linear-gradient(145deg, #fb923c, #dc2626 68%, #7f1d1d);
    font-weight: 900; font-size: 29px; box-shadow: 0 12px 28px rgba(220, 38, 38, .24);
}
.brand-mark.small { width: 38px; height: 38px; border-radius: 12px; font-size: 19px; }
.sidebar nav { display: grid; gap: 8px; }
.sidebar nav a { padding: 12px 14px; border-radius: 12px; color: #aebacf; font-weight: 650; }
.sidebar nav a span { display: inline-block; width: 25px; color: #70819b; }
.sidebar nav a:hover, .sidebar nav a.active { color: white; background: #243047; }
.sidebar nav a.active span { color: #fb923c; }
.sidebar-foot { margin-top: auto; padding: 16px 12px 0; border-top: 1px solid #2a3548; }
.user-chip { color: #fff; font-weight: 700; }
.link-button { padding: 5px 0; border: 0; color: #96a4b9; background: transparent; }
.link-button:hover { color: #fff; }

.main { min-width: 0; padding: 30px 34px 54px; }
.topbar { display: flex; align-items: flex-start; justify-content: space-between; gap: 20px; margin-bottom: 24px; }
.topbar h1 { margin: 2px 0 0; font-size: clamp(26px, 3vw, 38px); letter-spacing: -.035em; }
.eyebrow { color: var(--orange); font-size: 10px; font-weight: 850; letter-spacing: .2em; }
.live-pill { margin-top: 8px; padding: 8px 12px; border: 1px solid #dce2ea; border-radius: 999px; color: #4b5563; background: #fff; font-size: 12px; }
.live-pill span { width: 7px; height: 7px; display: inline-block; margin-right: 7px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 4px #dcfce7; }

.alert { margin: 0 0 20px; padding: 13px 15px; border: 1px solid; border-radius: 12px; font-weight: 650; }
.alert.success { color: #166534; border-color: #bbf7d0; background: #f0fdf4; }
.alert.error { color: #991b1b; border-color: #fecaca; background: #fef2f2; }
.panel, .connection-card { border: 1px solid var(--line); border-radius: 17px; background: var(--panel); box-shadow: var(--shadow); }
.panel { padding: 22px; }
.panel h3 { margin: 0 0 16px; }
.button {
    min-height: 42px; padding: 9px 15px; border: 1px solid transparent; border-radius: 11px;
    display: inline-flex; align-items: center; justify-content: center; gap: 7px; font-weight: 750;
}
.button.primary { color: #fff; background: var(--blue); }
.button.primary:hover { background: var(--blue-dark); }
.button.secondary { color: #25324a; border-color: #d8dee9; background: #fff; }
.button.secondary:hover, .button.ghost:hover { background: #f1f5f9; }
.button.ghost { color: #536179; border-color: transparent; background: transparent; }
.button:disabled { opacity: .55; cursor: wait; }
.small-button { min-height: 36px; padding: 7px 11px; font-size: 13px; }
.button-row { display: flex; align-items: flex-start; gap: 8px; flex-wrap: wrap; }
.archive-banner {
    display: flex; align-items: center; justify-content: space-between; gap: 16px;
    margin: 0 0 16px; padding: 14px 16px; border: 1px solid #cbd5e1; border-radius: 13px;
    color: #334155; background: #f8fafc;
}
.archive-banner b, .archive-banner span { display: block; }
.archive-banner span { margin-top: 2px; color: var(--muted); font-size: 13px; }
.archive-actions { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--line); }
.archive-actions h3 { margin-bottom: 5px; }
.archive-actions p { margin: 0 0 12px; }

.dashboard-actions, .section-head { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 18px; }
.dashboard-actions p { margin: 0; color: var(--muted); }
.section-head h2 { margin: 0 0 3px; }
.section-head p { margin: 0; color: var(--muted); }
.stat-grid { display: grid; grid-template-columns: repeat(9, minmax(105px, 1fr)); gap: 13px; margin-bottom: 18px; }
.stat-card { padding: 17px; border: 1px solid var(--line); border-top: 4px solid #94a3b8; border-radius: 15px; background: white; box-shadow: 0 8px 24px rgba(22,32,51,.05); }
.stat-card span, .stat-card small { display: block; }
.stat-card span { min-height: 42px; color: #536179; font-size: 13px; font-weight: 750; }
.stat-card b { display: block; margin: 1px 0 3px; font-size: 30px; letter-spacing: -.04em; }
.stat-card small { color: #8b95a7; }
.stat-card.attention { border-top-color: #dc2626; }
.stat-card.created { border-top-color: #94a3b8; }
.stat-card.transit { border-top-color: #2563eb; }
.stat-card.department { border-top-color: #f59e0b; }
.stat-card.refusal { border-top-color: #ef4444; }
.stat-card.returning { border-top-color: #7c3aed; }
.stat-card.returned { border-top-color: #0f766e; }
.stat-card.redirected { border-top-color: #0891b2; }
.stat-card.received { border-top-color: #16a34a; }

.filters { display: grid; grid-template-columns: minmax(220px, 1fr) 220px auto auto; gap: 10px; margin-bottom: 18px; }
input, select, textarea {
    width: 100%; min-height: 43px; padding: 10px 12px; border: 1px solid #d6dce6; border-radius: 10px;
    color: var(--ink); background: #fff; outline: none;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: #60a5fa; box-shadow: 0 0 0 3px #dbeafe; }
label { display: grid; gap: 6px; color: #39465b; font-weight: 650; }
.checkbox { grid-template-columns: auto 1fr; align-items: center; }
.checkbox input { width: 18px; min-height: 18px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; min-width: 1120px; }
.archive-table { min-width: 850px; }
th { padding: 11px 12px; color: #7a8496; background: #f8fafc; font-size: 11px; text-align: left; text-transform: uppercase; letter-spacing: .07em; }
td { padding: 14px 12px; border-top: 1px solid #edf0f4; vertical-align: top; }
tbody tr:hover { background: #fbfcfe; }
tbody tr.attention-critical td:first-child { box-shadow: inset 4px 0 #dc2626; }
tbody tr.attention-urgent td:first-child { box-shadow: inset 4px 0 #f97316; }
tbody tr.attention-warning td:first-child { box-shadow: inset 4px 0 #facc15; }
tbody tr.attention-verify td:first-child { box-shadow: inset 4px 0 #3b82f6; }
td small { display: block; margin-top: 4px; color: #7a8496; line-height: 1.35; }
.ttn-link { color: #1d4ed8; font-weight: 800; }
.ttn-actions { display: flex; align-items: center; gap: 7px; }
.copy-ttn {
    width: 28px; height: 28px; padding: 0; border: 1px solid #dbe2ec; border-radius: 8px;
    color: #52617a; background: #fff; font-size: 16px; line-height: 1; opacity: 0;
    transform: translateY(2px); transition: opacity .15s ease, color .15s ease, background .15s ease;
}
tr:hover .copy-ttn, .copy-ttn:focus-visible, .copy-ttn.copied { opacity: 1; }
.copy-ttn:hover { color: #1d4ed8; background: #eff6ff; }
.copy-ttn.copied { color: #166534; border-color: #bbf7d0; background: #dcfce7; }
.copy-toast {
    position: fixed; right: 24px; bottom: 24px; z-index: 1000; max-width: min(360px, calc(100vw - 32px));
    padding: 11px 15px; border-radius: 11px; color: #fff; background: #172033;
    box-shadow: 0 14px 36px rgba(15, 23, 42, .25); font-weight: 700;
    opacity: 0; transform: translateY(12px); pointer-events: none;
    transition: opacity .18s ease, transform .18s ease;
}
.copy-toast.visible { opacity: 1; transform: translateY(0); }
.company-tag { display: inline-flex; align-items: center; gap: 6px; font-weight: 750; white-space: nowrap; }
.company-tag::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--company); }
.company-slotina { --company: #2563eb; }
.company-reshetnyk { --company: #7c3aed; }
.company-voskoboienko { --company: #0891b2; }
.company-gelas { --company: #ea580c; }
.company-finoshkina { --company: #16a34a; }
.status-badge { display: inline-block; padding: 5px 9px; border-radius: 999px; background: #eef2f7; font-size: 12px; font-weight: 800; white-space: nowrap; }
.status-badge.received { color: #166534; background: #dcfce7; }
.status-badge.in_department { color: #92400e; background: #fef3c7; }
.status-badge.refusal { color: #991b1b; background: #fee2e2; }
.status-badge.returning { color: #6b21a8; background: #f3e8ff; }
.status-badge.returned { color: #115e59; background: #ccfbf1; }
.status-badge.redirected { color: #155e75; background: #cffafe; }
.status-badge.redirected_received { color: #166534; background: #dcfce7; }
.status-badge.transit { color: #1e40af; background: #dbeafe; }
.status-note { max-width: 300px; }
.empty-state { padding: 50px 20px; text-align: center; }
.empty-state .brand-mark { margin: 0 auto 14px; }
.empty-state h3 { margin: 0 0 5px; }
.empty-state p { margin: 0 0 18px; color: var(--muted); }

.connection-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; }
.connection-card { padding: 19px; border-top: 4px solid var(--company); }
.connection-title { display: flex; align-items: center; gap: 10px; }
.connection-title h3 { margin: 0; }
.connection-title small { color: var(--muted); }
.company-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--company); }
.connection-state { margin-left: auto; padding: 5px 9px; border-radius: 999px; color: #64748b; background: #f1f5f9; font-size: 11px; font-weight: 800; }
.connection-state.connected { color: #166534; background: #dcfce7; }
.connection-state.error { color: #991b1b; background: #fee2e2; }
.secret-form { display: grid; grid-template-columns: 1fr auto; align-items: end; gap: 9px; margin-top: 17px; }
.key-safe { display: flex; justify-content: space-between; gap: 10px; margin: 18px 0 11px; padding: 10px 12px; border: 1px dashed #ccd4df; border-radius: 10px; color: #64748b; background: #f8fafc; }
.key-safe b { color: #166534; font-size: 12px; }
.connection-meta { display: flex; gap: 15px; flex-wrap: wrap; margin-bottom: 12px; color: var(--muted); font-size: 12px; }
.inline-secret-form { display: flex; gap: 7px; margin-top: 8px; }
details summary { list-style: none; cursor: pointer; }
.mini-error { margin: 0 0 10px; padding: 8px 10px; border-radius: 8px; color: #991b1b; background: #fef2f2; font-size: 12px; }
.info-box { margin-top: 18px; padding: 15px 17px; border: 1px solid #bfdbfe; border-radius: 12px; color: #1e3a8a; background: #eff6ff; }

.back-link { display: inline-block; margin: 0 0 15px; color: #475569; font-weight: 700; }
.detail-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); gap: 16px; margin-bottom: 16px; }
.detail-title { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.detail-title h2 { margin: 7px 0 0; font-size: 28px; }
.status-full { padding: 12px 14px; border-radius: 10px; background: #f8fafc; }
.detail-list { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); margin: 0; }
.detail-list div { padding: 12px 0; border-bottom: 1px solid #edf0f4; }
.detail-list dt { color: var(--muted); font-size: 12px; }
.detail-list dd { margin: 2px 0 0; font-weight: 700; }
.form-stack { display: grid; gap: 13px; }
.muted { color: var(--muted); }
.timeline { display: grid; gap: 0; }
.timeline > div { position: relative; display: grid; grid-template-columns: 18px 1fr; gap: 10px; }
.timeline > div > span { width: 10px; height: 10px; margin-top: 7px; border-radius: 50%; background: #3b82f6; box-shadow: 0 0 0 4px #dbeafe; }
.timeline > div:not(:last-child)::before { content: ""; position: absolute; left: 4px; top: 18px; bottom: -5px; width: 2px; background: #dbeafe; }
.timeline p { margin: 0 0 16px; }
.timeline small { display: block; color: var(--muted); }

.login-body, .install-body { display: grid; place-items: center; padding: 28px; background: radial-gradient(circle at 15% 15%, #e0f2fe, transparent 35%), radial-gradient(circle at 85% 85%, #ffedd5, transparent 36%), #f7f8fb; }
.login-shell, .install-shell { width: min(980px, 100%); display: grid; grid-template-columns: 1.1fr .9fr; gap: 26px; align-items: center; }
.login-brand, .install-brand { display: flex; align-items: center; gap: 18px; padding: 20px; }
.login-brand h1, .install-brand h1 { margin: 2px 0 5px; font-size: clamp(34px, 6vw, 55px); letter-spacing: -.055em; }
.login-brand p, .install-brand p { margin: 0; color: var(--muted); font-size: 17px; }
.login-card, .install-card { padding: 28px; border: 1px solid var(--line); border-radius: 20px; background: rgba(255,255,255,.96); box-shadow: var(--shadow); }
.login-card { display: grid; gap: 15px; }
.login-card h2, .install-card h2 { margin: 0 0 5px; }
.login-card small { color: var(--muted); }
.install-shell { grid-template-columns: 1fr; max-width: 760px; }
.install-brand { justify-content: center; }
.check-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.check-item { padding: 9px 11px; border-radius: 9px; background: #f8fafc; }
.check-item span { width: 23px; display: inline-block; font-weight: 900; }
.check-item.ok span { color: #16a34a; }
.check-item.bad { color: #991b1b; background: #fef2f2; }
.two-cols { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.divider { height: 1px; margin: 5px 0; background: var(--line); }
.success-card { text-align: center; }
.success-icon { width: 56px; height: 56px; margin: 0 auto 12px; display: grid; place-items: center; border-radius: 50%; color: #fff; background: #16a34a; font-size: 28px; }

@media (max-width: 1050px) {
    .stat-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
    .connection-grid { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
    .app-shell { display: block; }
    .sidebar { position: static; width: 100%; height: auto; padding: 14px 16px; flex-direction: row; align-items: center; gap: 14px; }
    .logo { margin: 0 auto 0 0; }
    .logo small { display: none; }
    .sidebar nav { display: flex; }
    .sidebar nav a { padding: 9px 10px; font-size: 13px; }
    .sidebar nav a span { display: none; }
    .sidebar-foot { display: none; }
    .main { padding: 22px 16px 40px; }
    .live-pill { display: none; }
    .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .copy-ttn { opacity: 1; }
    .filters { grid-template-columns: 1fr; }
    .detail-grid { grid-template-columns: 1fr; }
    .login-shell { grid-template-columns: 1fr; }
    .login-brand { justify-content: center; text-align: center; }
}
@media (max-width: 500px) {
    .sidebar nav a { font-size: 0; }
    .sidebar nav a::after { font-size: 12px; }
    .sidebar nav a:nth-child(1)::after { content: "Контроль"; }
    .sidebar nav a:nth-child(2)::after { content: "Ключі"; }
    .sidebar nav a:nth-child(3)::after { content: "Архів"; }
    .stat-grid { grid-template-columns: 1fr; }
    .stat-card span { min-height: auto; }
    .dashboard-actions, .section-head { align-items: stretch; flex-direction: column; }
    .secret-form, .two-cols, .check-grid { grid-template-columns: 1fr; }
    .detail-list { grid-template-columns: 1fr; }
    .login-body, .install-body { padding: 15px; }
    .login-brand, .install-brand { padding: 5px; }
    .login-brand .brand-mark, .install-brand .brand-mark { width: 48px; height: 48px; font-size: 23px; }
    .login-brand h1, .install-brand h1 { font-size: 32px; }
}
