/* ---------- Основа ---------- */
:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --border: #e2e5ea;
    --text: #1c2530;
    --text-soft: #5f6b7a;
    --text-faint: #94a0ae;
    --primary: #1f6feb;
    --primary-dark: #1a5fca;
    --marked: #f0a020;
    --marked-bg: #fff8ec;
    --danger: #d1394a;
    --ok: #1f9d55;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(20,30,45,.06), 0 1px 2px rgba(20,30,45,.04);
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Шапка ---------- */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.topbar-inner {
    max-width: 1080px; margin: 0 auto; padding: 12px 20px;
    display: flex; align-items: center; justify-content: space-between;
}
.topbar-brand { display: flex; align-items: center; gap: 10px; color: var(--text); font-weight: 600; }
.topbar-brand:hover { text-decoration: none; }
.brand-mark {
    display: inline-flex; align-items: center; justify-content: center;
    width: 32px; height: 32px; border-radius: 8px;
    background: var(--primary); color: #fff; font-weight: 700; font-size: 13px;
}
.brand-name { font-size: 16px; }
.topbar-nav { display: flex; align-items: center; gap: 14px; }
.topbar-user { color: var(--text-soft); font-size: 14px; }

/* ---------- Контейнер ---------- */
.container { max-width: 1080px; margin: 0 auto; padding: 24px 20px 60px; }
.container-narrow { max-width: 720px; }

.page-head { display: flex; align-items: center; gap: 12px; margin: 0 0 20px; }
.page-head h1 { font-size: 22px; margin: 0; font-weight: 650; }
.count-badge {
    background: var(--border); color: var(--text-soft);
    padding: 2px 10px; border-radius: 20px; font-size: 13px; font-weight: 600;
}

/* ---------- Фильтры ---------- */
.filters {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 16px; margin-bottom: 20px;
    box-shadow: var(--shadow);
}
.filter-row {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 12px; margin-bottom: 12px;
}
.filter { display: flex; flex-direction: column; gap: 4px; }
.filter span { font-size: 12px; color: var(--text-soft); font-weight: 500; }
.filter-check { flex-direction: row; align-items: center; gap: 8px; align-self: end; padding-bottom: 8px; }
.filter-check span { font-size: 14px; }
.filter-actions { display: flex; gap: 10px; }

input[type="text"], input[type="password"], input[type="date"], input[type="number"], textarea, select {
    width: 100%; padding: 9px 11px;
    border: 1px solid var(--border); border-radius: 8px;
    font-size: 14px; font-family: inherit; color: var(--text);
    background: #fff; transition: border-color .15s, box-shadow .15s;
}
input:focus, textarea:focus, select:focus {
    outline: none; border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(31,111,235,.12);
}
textarea { resize: vertical; }

/* ---------- Кнопки ---------- */
.btn {
    display: inline-flex; align-items: center; justify-content: center;
    padding: 9px 16px; border-radius: 8px; font-size: 14px; font-weight: 500;
    border: 1px solid var(--border); background: #fff; color: var(--text);
    cursor: pointer; transition: background .15s, border-color .15s; text-decoration: none;
}
.btn:hover { text-decoration: none; background: #f7f8fa; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn-ghost { background: transparent; }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-block { width: 100%; }

/* ---------- Таблица тендеров ---------- */
.table-wrap {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
}
table.tenders { width: 100%; border-collapse: collapse; }
table.tenders th {
    text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .03em;
    color: var(--text-faint); font-weight: 600; padding: 12px 10px;
    border-bottom: 1px solid var(--border); background: #fafbfc;
}
table.tenders td { padding: 14px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.tenders tr:last-child td { border-bottom: none; }
.row-marked { background: var(--marked-bg); }

.col-mark { width: 36px; text-align: center; padding-left: 14px; }
.col-amount { white-space: nowrap; font-variant-numeric: tabular-nums; padding-left: 24px; }
.col-source { white-space: nowrap; padding-left: 16px; }
.col-title { padding-right: 20px; word-break: break-word; }
.col-date { white-space: nowrap; color: var(--text-soft); padding-left: 16px; }
.col-actions { white-space: nowrap; padding-right: 12px; padding-left: 16px; }

/* Иконки-действия в строке */
.actions { display: flex; align-items: center; gap: 4px; justify-content: flex-end; }
.actions .del-form { margin: 0; display: inline-flex; }
.icon-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 34px; height: 34px; border-radius: 8px;
    border: 1px solid var(--border); background: #fff; color: var(--text-soft);
    cursor: pointer; transition: background .15s, color .15s, border-color .15s;
    padding: 0; text-decoration: none;
}
.icon-btn:hover { background: #f2f4f7; color: var(--primary); border-color: #d6dbe2; text-decoration: none; }
.icon-btn-danger:hover { color: var(--danger); border-color: #f0b6bd; background: #fdf0f1; }

/* Бейджи источников */
.src-badge {
    display: inline-block; padding: 2px 8px; border-radius: 6px;
    font-size: 11px; font-weight: 600; white-space: nowrap;
}
.src-prozorro { background: #e3f0ff; color: #1f6feb; }
.src-zakupivli { background: #e6f7ee; color: #1f9d55; }
.src-smarttender { background: #fdeede; color: #c9760f; }

.t-title { font-weight: 550; color: var(--text); display: block; }
.t-customer { color: var(--text-soft); font-size: 13px; margin-top: 3px; }
.t-number { color: var(--text-faint); font-size: 12px; margin-top: 3px; font-variant-numeric: tabular-nums; }

.star {
    background: none; border: none; cursor: pointer;
    font-size: 20px; line-height: 1; color: var(--text-faint); padding: 2px;
}
.star-on { color: var(--marked); }
.mark-form { margin: 0; }

/* ---------- Пагинация ---------- */
.pagination { display: flex; align-items: center; justify-content: center; gap: 16px; margin-top: 24px; }
.page-info { color: var(--text-soft); font-size: 14px; }

/* ---------- Детали тендера ---------- */
.back-link { display: inline-block; margin-bottom: 16px; color: var(--text-soft); font-size: 14px; }
.detail {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 4px 20px; margin: 0 0 20px; box-shadow: var(--shadow);
    display: grid; grid-template-columns: 180px 1fr;
}
.detail dt { padding: 12px 0; color: var(--text-soft); font-size: 13px; border-bottom: 1px solid var(--border); }
.detail dd {
    padding: 12px 0; margin: 0; border-bottom: 1px solid var(--border);
    word-break: break-word;
}
.detail dt:last-of-type, .detail dd:last-of-type { border-bottom: none; }

.tag { padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.tag-marked { background: var(--marked-bg); color: var(--marked); border: 1px solid #f2d08a; }

/* ---------- Редактирование ---------- */
.edit-block {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); margin-bottom: 20px; box-shadow: var(--shadow);
}
.edit-block summary {
    padding: 14px 20px; cursor: pointer; font-weight: 550; user-select: none;
}
.edit-form { padding: 4px 20px 20px; display: flex; flex-direction: column; gap: 14px; }
.edit-form label { display: flex; flex-direction: column; gap: 5px; }
.edit-form label > span { font-size: 12px; color: var(--text-soft); font-weight: 500; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.check-inline { flex-direction: row !important; align-items: center; gap: 8px; }
.check-inline span { font-size: 14px !important; }

.delete-form { margin-top: 8px; }

/* ---------- Алерты ---------- */
.alert { padding: 12px 16px; border-radius: 8px; margin-bottom: 18px; font-size: 14px; }
.alert-error { background: #fdecee; color: var(--danger); border: 1px solid #f5c2c8; }
.alert-ok { background: #e9f7ef; color: var(--ok); border: 1px solid #b7e4c7; }

/* ---------- Пусто ---------- */
.empty {
    background: var(--surface); border: 1px dashed var(--border);
    border-radius: var(--radius); padding: 48px 20px; text-align: center; color: var(--text-soft);
}
.empty-hint { font-size: 13px; color: var(--text-faint); margin-top: 4px; }

/* ---------- AI-анализ и документы ---------- */
.ai-block, .docs-block {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: var(--radius); padding: 18px 20px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.ai-block h2, .docs-block h2 {
    font-size: 14px; text-transform: uppercase; letter-spacing: .03em;
    color: var(--text-faint); margin: 0 0 12px; font-weight: 600;
}
.ai-text { white-space: pre-wrap; line-height: 1.6; color: var(--text); }
.ai-empty { color: var(--text-soft); margin: 0 0 12px; }
.ai-form { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.ai-hint { font-size: 13px; color: var(--text-faint); }

/* Спиннер на кнопке анализа */
#aiBtn { display: inline-flex; align-items: center; gap: 8px; }
.ai-spinner {
    display: none; width: 15px; height: 15px;
    border: 2px solid rgba(255,255,255,.4); border-top-color: #fff;
    border-radius: 50%; animation: aiSpin .7s linear infinite;
}
#aiBtn.loading .ai-spinner { display: inline-block; }
#aiBtn.loading { opacity: .85; cursor: default; }
@keyframes aiSpin { to { transform: rotate(360deg); } }

.docs-list { list-style: none; margin: 0; padding: 0; }
.docs-list li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.docs-list li:last-child { border-bottom: none; }
.docs-list a { font-size: 14px; word-break: break-word; }

/* ---------- Модальне вікно ---------- */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(20, 30, 45, .5);
    display: none; align-items: center; justify-content: center;
    padding: 20px; z-index: 100;
}
.modal-overlay.open { display: flex; }
.modal {
    background: #fff; border-radius: 14px; padding: 24px;
    max-width: 420px; width: 100%;
    box-shadow: 0 12px 40px rgba(20, 30, 45, .25);
    animation: modalIn .18s ease-out;
}
@keyframes modalIn {
    from { opacity: 0; transform: translateY(10px) scale(.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}
.modal h3 { margin: 0 0 8px; font-size: 18px; color: var(--text); }
.modal-text { margin: 0 0 20px; color: var(--text-soft); font-size: 14px; line-height: 1.5; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* ---------- Логин ---------- */
.login-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 14px; padding: 32px; width: 100%; max-width: 380px; box-shadow: var(--shadow);
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; }
.login-brand .brand-mark { width: 42px; height: 42px; font-size: 15px; border-radius: 10px; }
.brand-title { font-weight: 650; font-size: 18px; }
.brand-sub { color: var(--text-soft); font-size: 13px; }
.login-form { display: flex; flex-direction: column; gap: 16px; }
.login-form label { display: flex; flex-direction: column; gap: 6px; }
.login-form label span { font-size: 13px; color: var(--text-soft); }

/* ---------- Адаптив: таблица → карточки ---------- */
@media (max-width: 680px) {
    .topbar-inner { padding: 10px 16px; }
    .container { padding: 16px 14px 48px; }
    .filter-row { grid-template-columns: 1fr 1fr; }
    .form-grid { grid-template-columns: 1fr; }
    .detail { grid-template-columns: 1fr; padding: 12px 16px; }
    .detail dt { padding: 10px 0 2px; border-bottom: none; }
    .detail dd { padding: 0 0 10px; border-bottom: 1px solid var(--border); }

    /* превращаем таблицу в карточки */
    table.tenders thead { display: none; }
    table.tenders, table.tenders tbody, table.tenders tr, table.tenders td { display: block; width: 100%; }
    table.tenders tr {
        border-bottom: 1px solid var(--border); padding: 6px 4px; position: relative;
    }
    table.tenders td { border: none; padding: 4px 14px; }
    .col-mark { position: absolute; top: 12px; right: 8px; width: auto; }
    .col-amount, .col-date { width: auto; display: inline-block; }
    .col-amount { font-weight: 600; }
    .col-date::before { content: "до "; color: var(--text-faint); }
    .col-source { width: auto; padding-top: 6px; }
    .col-actions { width: auto; text-align: left; padding-top: 10px; padding-left: 14px; }
    /* Иконки крупнее и с большими отступами для пальца */
    .actions { justify-content: flex-start; gap: 10px; }
    .icon-btn { width: 42px; height: 42px; }
}

@media (max-width: 400px) {
    .filter-row { grid-template-columns: 1fr; }
}