/* ui-polish.css — program-wide visual unification.
 *
 * Loaded last in head_part.html so it refines the base theme + design tokens.
 *
 * Two goals:
 *   1. One unified "card" treatment shared by `.section-card` (the detail-tab
 *      data cards) and the `.ddform-fieldset` form cards, so every declaration
 *      tab reads as the same family of cards instead of bare tables.
 *   2. A gentle, consistent button refinement across the whole program
 *      (radius, weight, resting shadow, hover lift, crisp focus ring).
 */

/* ============================================================
 * 1. Section card — the canonical content card
 *    Mirrors the chrome of .ddform-fieldset (border, radius, shadow,
 *    hover, top accent bar) so detail tabs match the header tab.
 * ============================================================ */
.section-card {
    background: var(--surface-1);
    border: 1px solid var(--border-subtle);
    border-radius: 12px;
    margin-bottom: 1rem;
    box-shadow: var(--shadow-sm);
    transition: box-shadow var(--duration-fast) var(--ease-out);
    position: relative;
    padding: 0;                 /* header + body own their padding */
}

.section-card:hover {
    box-shadow: var(--shadow-md);
}

/* Top accent bar — same visual cue as .ddform-fieldset cards. Uses a top
 * border-radius (not overflow:hidden) so TomSelect dropdowns inside the card
 * are never clipped. */
.section-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    border-radius: 12px 12px 0 0;
    background: linear-gradient(90deg, var(--st-signing), transparent);
    opacity: 0.5;
}

/* Per-purpose accent colours — keep the same palette as the form fieldsets. */
/* Decorative per-type accent colours intentionally collapsed to ONE uniform
 * app-blue line (see the uniform-accent override below). The --finance/docs/
 * items/history/comm modifiers stay valid class names (semantic in markup) but
 * no longer change the accent colour. */

/* Status emphasis — colours both the border and the accent bar. Generalises the
 * finance .is-emphasis-* pattern; used e.g. by confidence-graded review cards. */
.section-card--emphasis-success { border-color: var(--st-validated); }
.section-card--emphasis-warning { border-color: var(--st-awaiting); }
.section-card--emphasis-danger  { border-color: var(--st-failed); }
.section-card--emphasis-info    { border-color: var(--st-signing); }
.section-card--emphasis-success::before { background: linear-gradient(90deg, var(--st-validated), transparent); opacity: 0.8; }
.section-card--emphasis-warning::before { background: linear-gradient(90deg, var(--st-awaiting), transparent); opacity: 0.8; }
.section-card--emphasis-danger::before  { background: linear-gradient(90deg, var(--st-failed), transparent); opacity: 0.8; }
.section-card--emphasis-info::before     { background: linear-gradient(90deg, var(--st-signing), transparent); opacity: 0.8; }

/* Header row — icon + small-caps title, optional stats / actions on the right.
 * Typography matches the .ddform-fieldset <legend> chip so titles read the same
 * across every tab. */
.section-card__header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.7rem 1.1rem 0.6rem;
    border-bottom: 1px solid var(--border-subtle);
}

/* A header with no body after it (e.g. a tab-bar card) shouldn't draw a
 * redundant divider right at the card's bottom edge. */
.section-card__header:last-child {
    border-bottom: none;
}

.section-card__title {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    margin: 0;
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: #4b5563;
}

.section-card__title i {
    color: var(--st-signing);
    font-size: 0.95em;
}

.section-card__stats {
    margin-left: auto;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.2rem 0.85rem;
    font-size: 0.82rem;
    color: #6b7280;
}

.section-card__stats strong {
    color: #1f2937;
}

.section-card__actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.section-card__body {
    padding: 0.9rem 1.1rem;
}

/* When a section card has no header, the body's own padding is enough but the
 * top accent bar needs a little breathing room. */
.section-card > .section-card__body:first-child {
    padding-top: 1rem;
}

/* ── Formsets / tables sitting inside a section card become flush content:
 *    the card already provides the frame, so drop the formset's own box. ── */
.section-card .formset-header {
    background: transparent;
    border: 0;
    border-bottom: 2px solid var(--border-subtle);
    border-radius: 0;
    margin: 0 0 0.25rem;
    padding: 0 0.4rem 0.4rem;
}

.section-card [data-formset-prefix] {
    background: transparent;
    border: 0;
    border-radius: 0;
    margin: 0;
    padding: 0;
}

/* django-tables2 tables inside a card: edge-to-edge, no stray bottom margin. */
.section-card .table {
    margin-bottom: 0;
}

/* ── Converge .ddform-fieldset (declaration "header" tab crispy form cards) onto
 *    the .section-card look. They used a floating <legend> "chip"; this turns the
 *    legend into a full-width header BAR with a bottom divider — identical header
 *    + accent treatment to .section-card, so the header tab matches every other
 *    tab. CSS-only (no crispy/Python changes). The compact --filter search
 *    variant is excluded so it keeps its tight look. ── */
.ddform-fieldset:not(.ddform-fieldset--filter) {
    padding: 0 1.1rem 0.75rem;     /* drop top padding — the legend becomes the header bar */
}
.ddform-fieldset:not(.ddform-fieldset--filter) > legend {
    position: relative;             /* hosts the accent bar (::before) */
    display: flex;
    align-items: center;
    gap: 0.45rem;
    box-sizing: border-box;
    /* <legend> shrink-wraps to its text by default; force it to span the full
       card width so the header bar, divider and accent are full-width. */
    width: calc(100% + 2.2rem);     /* card content width + 2×1.1rem padding breakout */
    margin: 0 -1.1rem 0.9rem;       /* break out to the card's side borders */
    padding: 0.7rem 1.1rem 0.6rem;  /* re-inset text to body alignment (matches .section-card__header) */
    background: transparent;
    border-bottom: 1px solid var(--border-subtle);
    border-radius: 0;
    /* typography (0.78rem / 700 / uppercase / 0.06em / #4b5563) inherited from
       declaration-form.css so it already matches .section-card__title */
}
/* Crispy Fieldset legends bake `me-2` (0.5rem) onto their icon while hand-written
 * legends use a literal space; both made the icon sit farther from the title than
 * in .section-card__title. Neutralise so spacing is the flex `gap` only, and match
 * the icon size — making legend titles pixel-identical to .section-card__title. */
.ddform-fieldset:not(.ddform-fieldset--filter) > legend i {
    margin: 0 !important;
    font-size: 0.95em;
}
/* The fieldset's OWN ::before mis-positions below the <legend> (a fieldset/legend
 * rendering quirk — its content box starts under the legend). Hide it and render
 * the accent on the legend instead, where it sits at the true top of the card. */
.ddform-fieldset:not(.ddform-fieldset--filter)::before {
    display: none;
}
.ddform-fieldset:not(.ddform-fieldset--filter) > legend::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    border-radius: 12px 12px 0 0;
    background: linear-gradient(90deg, var(--st-signing), transparent);
    opacity: 0.5;
}

/* ── Uniform accent — ONE app-blue line on every content card. Overrides the
 *    declaration-form.css per-section decorative modifiers. Functional STATUS
 *    accents are deliberately preserved (.section-card--emphasis-*,
 *    .finance-card.is-emphasis-*, .kpi-card colours). The fieldset accent is the
 *    legend::before above (already app-blue). ── */
.section-card:not([class*="--emphasis-"])::before {
    background: linear-gradient(90deg, var(--st-signing), transparent) !important;
}

@media (max-width: 768px) {
    .ddform-fieldset:not(.ddform-fieldset--filter) {
        padding: 0 1rem 0.4rem;
    }
    .ddform-fieldset:not(.ddform-fieldset--filter) > legend {
        width: calc(100% + 2rem);
        margin: 0 -1rem 0.75rem;
        padding: 0.6rem 1rem 0.5rem;
    }
}

/* ============================================================
 * 2. Buttons — gentle program-wide refinement
 * ============================================================ */
.btn {
    font-weight: 500;
    border-radius: 8px;
    transition: color var(--duration-fast) var(--ease-out),
                background-color var(--duration-fast) var(--ease-out),
                border-color var(--duration-fast) var(--ease-out),
                box-shadow var(--duration-fast) var(--ease-out),
                transform var(--duration-fast) var(--ease-out);
}

.btn-sm { border-radius: 6px; }
.btn-lg { border-radius: 10px; }

/* Resting shadow on solid + outline variants. */
.btn-primary, .btn-secondary, .btn-success, .btn-danger, .btn-warning,
.btn-info, .btn-dark, .btn-light,
.btn-outline-primary, .btn-outline-secondary, .btn-outline-success,
.btn-outline-danger, .btn-outline-warning, .btn-outline-info,
.btn-outline-dark {
    box-shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
}

/* Subtle hover lift. */
.btn-primary:hover, .btn-secondary:hover, .btn-success:hover, .btn-danger:hover,
.btn-warning:hover, .btn-info:hover, .btn-dark:hover, .btn-light:hover,
.btn-outline-primary:hover, .btn-outline-secondary:hover, .btn-outline-success:hover,
.btn-outline-danger:hover, .btn-outline-warning:hover, .btn-outline-info:hover,
.btn-outline-dark:hover {
    box-shadow: 0 4px 10px -2px rgba(16, 24, 40, 0.18);
    transform: translateY(-1px);
}

.btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px rgba(16, 24, 40, 0.10);
}

/* Grouped / sidebar buttons stay flush — a lift looks wrong there. */
.btn-group .btn:hover,
.input-group .btn:hover,
.sidebar .btn:hover {
    transform: none;
}
.sidebar .btn:hover {
    box-shadow: none;
}

/* Crisp, accessible focus ring. */
.btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.35);
}

/* ============================================================
 * 3. Light card + nav-pill polish (program-wide, low risk)
 * ============================================================ */
.card {
    border-color: var(--border-subtle);
    border-radius: 12px;
}

.card.shadow {
    box-shadow: 0 2px 10px -2px rgba(16, 24, 40, 0.08),
                0 1px 3px -1px rgba(16, 24, 40, 0.06) !important;
}

.card > .card-header {
    background: var(--surface-2);
    border-bottom-color: var(--border-subtle);
}

/* Declaration detail tabs (and any nav-pills) — rounder, with a clear active
 * pill. Inactive colour is left to the theme so the warning "Подготовка" link
 * keeps its accent. */
.nav-pills .nav-link {
    border-radius: 8px;
    transition: background-color var(--duration-fast) var(--ease-out),
                color var(--duration-fast) var(--ease-out),
                box-shadow var(--duration-fast) var(--ease-out);
}

.nav-pills .nav-link:hover {
    background-color: var(--surface-3);
}

.nav-pills .nav-link.active {
    background-color: var(--app-accent);
    box-shadow: 0 2px 6px -1px rgba(79, 110, 247, 0.45);
}

/* ============================================================
 * 4. KPI tile — token-driven dashboard stat card
 *    Replaces the ad-hoc `.card.shadow.border-{color}` + bare
 *    `.text-{color}` KPI blocks on the coordinator/portal dashboards
 *    with a single accented tile (mirrors finance.css .finance-kpi
 *    but on the global design tokens).
 * ============================================================ */
.kpi-card {
    position: relative;
    overflow: hidden;
    height: 100%;
    background: var(--surface-1);
    border: 1px solid var(--border-subtle);
    border-radius: 12px;
    box-shadow: var(--shadow-sm);
    padding: 0.7rem 1rem 0.7rem 1.15rem;
    transition: transform 100ms var(--ease-out), box-shadow var(--duration-fast) var(--ease-out);
}

.kpi-card:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.kpi-card::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 4px;
    background: var(--st-signing);
}

.kpi-card--awaiting::before { background: var(--st-awaiting); }
.kpi-card--success::before  { background: var(--st-validated); }
.kpi-card--danger::before   { background: var(--st-failed); }
.kpi-card--info::before     { background: var(--st-signing); }
.kpi-card--primary::before  { background: var(--st-submitting); }
.kpi-card--muted::before    { background: var(--st-created); }

.kpi-card__label {
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: #6b7280;
}

.kpi-card--awaiting .kpi-card__label { color: var(--st-awaiting); }
.kpi-card--success  .kpi-card__label { color: var(--st-validated); }
.kpi-card--danger   .kpi-card__label { color: var(--st-failed); }
.kpi-card--info     .kpi-card__label { color: var(--st-signing); }
.kpi-card--primary  .kpi-card__label { color: var(--st-submitting); }

.kpi-card__value {
    font-size: 1.6rem;
    font-weight: 600;
    line-height: 1.2;
    color: #1f2937;
    margin-top: 0.1rem;
}

.kpi-card__hint {
    font-size: 0.75rem;
    color: #6b7280;
    margin-top: 0.2rem;
}

/* ============================================================
 * 5. Global HTMX loading bar — thin progress strip at viewport top.
 *    Driven by htmx:beforeRequest / htmx:afterRequest (see
 *    modals_and_scripts_part.html). Gives feedback on every tab swap /
 *    inline action without per-component spinners.
 * ============================================================ */
#htmx-progress {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0;
    z-index: 2000;
    background: linear-gradient(90deg, var(--app-accent), #36b9cc);
    box-shadow: 0 0 8px rgba(78, 115, 223, 0.6);
    opacity: 0;
    transition: width 0.2s ease, opacity 0.3s ease;
    pointer-events: none;
}
#htmx-progress.is-active { opacity: 1; }

/* ============================================================
 * 6. Scroll-to-top floating button — appears on long pages.
 * ============================================================ */
#scrollTopBtn {
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    z-index: 1040;
    width: 2.6rem;
    height: 2.6rem;
    border: none;
    border-radius: 50%;
    background: var(--app-accent);
    color: #fff;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
    cursor: pointer;
    opacity: 0;
    transform: translateY(0.5rem);
    transition: opacity 0.2s ease, transform 0.2s ease, background 0.15s ease;
    pointer-events: none;
}
#scrollTopBtn.is-visible {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
#scrollTopBtn:hover { background: var(--app-accent-strong); }
@media print { #scrollTopBtn, #htmx-progress { display: none !important; } }

/* ============================================================
 * 7. Print hygiene — drop the screen-only decorations on paper
 * ============================================================ */
@media print {
    .section-card::before,
    .finance-card::before,
    .ddform-fieldset::before,
    .kpi-card::before {
        display: none !important;
    }
    .section-card,
    .finance-card,
    .ddform-fieldset,
    .kpi-card,
    .card {
        box-shadow: none !important;
    }
}
