/* ==========================================================================
   components.css — buttons, forms, cards, badges, alerts, tables

   Ported verbatim from the shared "master-window" app-shell boilerplate
   (see docs/templates.md) — fully generic, driven entirely by design
   tokens from base.css.
   ========================================================================== */

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    font-family: var(--font-ui);
    font-size: var(--text-sm);
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    line-height: 1;
    padding: 0.625rem var(--space-5);
    border-radius: var(--radius-md);
    border: 1px solid transparent;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition:
        background var(--transition-fast),
        color var(--transition-fast),
        border-color var(--transition-fast),
        box-shadow var(--transition-fast),
        transform var(--transition-fast);
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.btn:active {
    transform: translateY(1px);
}

.btn:disabled,
.btn.is-disabled {
    opacity: 0.45;
    pointer-events: none;
    cursor: not-allowed;
}

/* Primary — brand-orange fill */
.btn--primary {
    background: var(--primary-fill);
    color: var(--text-on-primary);
    border-color: var(--primary-fill);
}

.btn--primary:hover {
    background: var(--primary-fill-hi);
    border-color: var(--primary-fill-hi);
    color: var(--text-on-primary);
    box-shadow: var(--shadow-primary);
}

/* Secondary — outlined */
.btn--secondary {
    background: transparent;
    color: var(--primary);
    border-color: var(--primary);
}

.btn--secondary:hover {
    background: var(--primary-wash);
    color: var(--primary-hi);
    border-color: var(--primary-hi);
}

/* Ghost — subtle */
.btn--ghost {
    background: transparent;
    color: var(--text-2);
    border-color: var(--border);
}

.btn--ghost:hover {
    background: var(--surface-hi);
    color: var(--text);
    border-color: var(--border-hi);
}

/* Danger */
.btn--danger {
    background: var(--red);
    color: #fff;
    border-color: var(--red);
}

.btn--danger:hover {
    background: #a81640;
    border-color: #a81640;
    color: #fff;
}

.btn--danger-ghost {
    background: transparent;
    color: var(--red);
    border-color: var(--red);
}

.btn--danger-ghost:hover {
    background: var(--red-wash);
}

/* Light — white fill, brand-color text — for use on dark/secondary section backgrounds */
.btn--light {
    background: #fff;
    color: var(--primary-fill);
    border-color: #fff;
}

.btn--light:hover {
    background: rgba(255, 255, 255, 0.88);
    border-color: rgba(255, 255, 255, 0.88);
    color: var(--primary-fill);
}

/* Sizes */
.btn--xs {
    font-size: 11px;
    padding: 0.2rem var(--space-3);
    letter-spacing: 0.05em;
}

.btn--sm {
    font-size: var(--text-xs);
    padding: 0.375rem var(--space-4);
    letter-spacing: 0.06em;
}

.btn--lg {
    font-size: var(--text-base);
    padding: 0.875rem var(--space-8);
}

.btn--full {
    width: 100%;
}

.btn--icon {
    padding: 0.625rem;
    aspect-ratio: 1;
}

/* --------------------------------------------------------------------------
   Forms
   -------------------------------------------------------------------------- */

.form-group {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-bottom: var(--space-5);
    position: relative;
}

.form-label {
    font-family: var(--font-ui);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-2);
}

.form-label--required::after {
    content: ' *';
    color: var(--primary);
}

.form-hint {
    font-size: var(--text-xs);
    color: var(--text-3);
    margin-top: var(--space-1);
    line-height: 1.5;
}

/* Two-column side-by-side field layout */
.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-4);
}

@media (max-width: 480px) {
    .form-row { grid-template-columns: 1fr; }
}

/* Inline row for checkbox + label pairs */
.form-group--inline {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
}

/* Centers a lone unlabeled control (e.g. the Turnstile widget) instead of
   the default left/stretch alignment meant for label+input pairs */
.form-group--center {
    align-items: center;
}

/* Label row with a side action button */
.form-label-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: var(--space-1);
}

.form-label-row .form-label {
    margin-bottom: 0;
}

.form-input,
.form-select,
.form-textarea {
    width: 100%;
    background: var(--bg-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: var(--space-3) var(--space-4);
    color: var(--text);
    font-family: var(--font-body);
    font-size: var(--text-base);
    line-height: 1.5;
    transition:
        border-color var(--transition-fast),
        box-shadow var(--transition-fast);
    -webkit-appearance: none;
    appearance: none;
}

.form-input::placeholder,
.form-textarea::placeholder {
    color: var(--text-3);
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-wash);
}

.form-input:disabled,
.form-select:disabled,
.form-textarea:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.form-input--error,
.form-select--error,
.form-textarea--error {
    border-color: var(--red);
    box-shadow: 0 0 0 3px var(--red-wash);
}

.form-textarea {
    min-height: 100px;
    resize: vertical;
}

.form-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' fill='none'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238E9AAA' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--space-4) center;
    padding-right: var(--space-10);
    cursor: pointer;
}

.form-error {
    font-size: var(--text-xs);
    color: var(--red);
    margin-top: var(--space-1);
    font-family: var(--font-ui);
    font-weight: 500;
}

/* Prefixed input */
.input-prefix-wrap {
    display: flex;
    align-items: stretch;
    background: var(--bg-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    overflow: hidden;
    transition: border-color var(--transition-fast);
}

.input-prefix-wrap:focus-within {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-wash);
}

.input-prefix {
    flex: 0 0 auto;
    padding: 0 var(--space-3);
    font-family: var(--font-ui);
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-3);
    background: var(--surface-hi);
    border-right: 1px solid var(--border);
    display: flex;
    align-items: center;
}

.input-prefix-wrap__input {
    flex: 1 1 auto;
    border: none !important;
    border-radius: 0 !important;
    background: transparent !important;
    box-shadow: none !important;
}

.input-prefix-wrap__input:focus {
    outline: none;
    box-shadow: none !important;
}

/* Light theme form adjustments */
[data-theme="light"] .form-input,
[data-theme="light"] .form-select,
[data-theme="light"] .form-textarea {
    background: var(--surface);
}

/* --------------------------------------------------------------------------
   Flash messages / toasts
   -------------------------------------------------------------------------- */

.flash {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-md);
    border: 1px solid;
    font-family: var(--font-ui);
    font-size: var(--text-sm);
    font-weight: 500;
    line-height: 1.5;
    transition: opacity 0.4s ease, transform 0.4s ease;
}

.flash.is-hiding {
    opacity: 0;
    transform: translateX(8px);
    pointer-events: none;
}

.flash__text {
    flex: 1 1 auto;
}

.flash__close {
    flex-shrink: 0;
    background: none;
    border: none;
    cursor: pointer;
    color: inherit;
    opacity: 0.6;
    font-size: 1.1rem;
    line-height: 1;
    padding: 0 var(--space-1);
    transition: opacity var(--transition-fast);
}

.flash__close:hover {
    opacity: 1;
}

.flash--error {
    background: var(--red-wash);
    border-color: var(--red);
    color: var(--red);
}

.flash--success {
    background: var(--green-wash);
    border-color: var(--green);
    color: var(--green);
}

.flash--info {
    background: var(--blue-wash);
    border-color: var(--blue);
    color: var(--blue);
}

.flash--warning {
    background: var(--amber-wash);
    border-color: var(--amber);
    color: var(--amber);
}

.flash-stack {
    position: fixed;
    top: var(--space-4);
    right: var(--space-5);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    max-width: 360px;
    width: calc(100vw - var(--space-10));
}

/* --------------------------------------------------------------------------
   Cards
   -------------------------------------------------------------------------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.card:hover {
    border-color: var(--border-hi);
    box-shadow: var(--shadow-md);
}

.card__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: var(--space-5) var(--space-6);
    border-bottom: 1px solid var(--border);
}

.card__title {
    font-family: var(--font-display);
    font-size: var(--text-base);
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text);
}

.card__body {
    padding: var(--space-6);
}

.card__footer {
    padding: var(--space-4) var(--space-6);
    border-top: 1px solid var(--border);
    background: var(--bg-alt);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
}

/* --------------------------------------------------------------------------
   Badges / tags
   -------------------------------------------------------------------------- */

.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    font-family: var(--font-ui);
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    padding: 2px var(--space-2);
    border-radius: var(--radius-sm);
    line-height: 1.6;
    white-space: nowrap;
}

.badge--primary { background: var(--primary-wash);  color: var(--primary);  border: 1px solid var(--primary-dim); }
.badge--accent  { background: rgba(232, 23, 122, 0.10); color: var(--accent); border: 1px solid var(--accent); }
.badge--green   { background: var(--green-wash);  color: var(--green);  border: 1px solid var(--green); }
.badge--red     { background: var(--red-wash);    color: var(--red);    border: 1px solid var(--red); }
.badge--amber   { background: var(--amber-wash);  color: var(--amber);  border: 1px solid var(--amber); }
.badge--blue    { background: var(--blue-wash);   color: var(--blue);   border: 1px solid var(--blue); }
.badge--muted   { background: var(--surface-hi);  color: var(--text-3); border: 1px solid var(--border); }

/* --------------------------------------------------------------------------
   Tables
   -------------------------------------------------------------------------- */

.table-wrap {
    width: 100%;
    overflow-x: auto;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
}

.table {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--text-sm);
}

.table th {
    font-family: var(--font-ui);
    font-size: var(--text-xs);
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--text-3);
    padding: var(--space-3) var(--space-4);
    text-align: left;
    background: var(--bg-alt);
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.table td {
    padding: var(--space-3) var(--space-4);
    border-bottom: 1px solid var(--border);
    color: var(--text-2);
    vertical-align: middle;
}

.table tr:last-child td {
    border-bottom: none;
}

.table tr:hover td {
    background: var(--primary-wash);
    color: var(--text);
}

.table .col-amount {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: var(--text-base);
    color: var(--text);
    white-space: nowrap;
}

.table .col-status {
    white-space: nowrap;
}

/* --------------------------------------------------------------------------
   Divider
   -------------------------------------------------------------------------- */

.divider {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    margin: var(--space-6) 0;
    color: var(--text-3);
    font-family: var(--font-ui);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.divider::before,
.divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

/* --------------------------------------------------------------------------
   Empty state
   -------------------------------------------------------------------------- */

.empty-state {
    text-align: center;
    padding: var(--space-16) var(--space-8);
}

.empty-state__icon {
    font-size: 2.5rem;
    margin-bottom: var(--space-4);
    opacity: 0.3;
}

.empty-state__title {
    font-family: var(--font-display);
    font-size: var(--text-lg);
    font-weight: 600;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--text-2);
    margin-bottom: var(--space-2);
}

.empty-state__text {
    font-size: var(--text-sm);
    color: var(--text-3);
    max-width: 300px;
    margin-inline: auto;
    margin-bottom: var(--space-6);
}

/* --------------------------------------------------------------------------
   Theme toggle
   -------------------------------------------------------------------------- */

.theme-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: var(--radius-md);
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text-2);
    cursor: pointer;
    transition: all var(--transition-fast);
    padding: 0;
}

.theme-toggle:hover {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-wash);
}

.theme-toggle svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 1.75;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Sun icon — shown in dark mode */
.icon-sun { display: block; }
.icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }

/* --------------------------------------------------------------------------
   Link-style buttons (global)
   -------------------------------------------------------------------------- */

.btn-link {
    background: none;
    border: none;
    cursor: pointer;
    font-family: var(--font-ui);
    font-size: var(--text-sm);
    padding: 0;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.btn-link--subtle {
    color: var(--text-3);
}

.btn-link--subtle:hover {
    color: var(--text-2);
}

.btn-link--danger {
    color: var(--red);
    font-size: var(--text-xs);
}

.btn-link--danger:hover {
    opacity: 0.8;
}

/* --------------------------------------------------------------------------
   Server status badge (templates/_server_status.html)

   Two variants off one block: --compact for chrome (public header, sidebar
   footer) and --full for the landing hero. State lives in a modifier class
   rather than inline colour so the dot, ring and text stay in step.
   -------------------------------------------------------------------------- */

.server-status {
    --status-color: var(--text-3);
    --status-ring: rgba(148, 138, 153, 0.22);
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    font-family: var(--font-ui);
    color: var(--text-2);
    white-space: nowrap;
}

/* Ring colours are spelled out rather than mixed from --status-color:
   color-mix() appears nowhere else in this codebase, and the status hues
   are fixed in both themes. */
.server-status--online  { --status-color: var(--green); --status-ring: rgba(45, 158, 90, 0.22); }
.server-status--offline { --status-color: var(--red);   --status-ring: rgba(200, 30, 75, 0.22); }
.server-status--stale   { --status-color: var(--amber); --status-ring: rgba(232, 155, 10, 0.22); }
.server-status--unknown { --status-color: var(--text-3); }

.server-status__dot {
    width: 8px;
    height: 8px;
    border-radius: var(--radius-pill);
    background: var(--status-color);
    flex-shrink: 0;
    /* Ring instead of a pulse animation: this sits in the header of every
       page, and a permanent animation there is a distraction, not a signal. */
    box-shadow: 0 0 0 3px var(--status-ring);
}

.server-status--compact {
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    padding: var(--space-1) var(--space-3);
    border: 1px solid var(--border);
    border-radius: var(--radius-pill);
    background: var(--surface);
}

/* In the public header the surface is the dark brand bar, not a page. */
.public-header .server-status--compact {
    background: rgba(255, 255, 255, 0.06);
    border-color: rgba(255, 255, 255, 0.14);
    color: rgba(255, 255, 255, 0.78);
}

/* Sidebar footer: match the chips it sits above, and collapse to just the
   dot when the rail is narrow (.sidebar-label is hidden by nav.css). */
#sidebar-footer .server-status--compact {
    display: flex;
    justify-content: flex-start;
    gap: var(--space-3);
    width: 100%;
    padding: var(--space-2) var(--space-3);
    margin-bottom: var(--space-2);
    background: transparent;
}

/* Collapsed rail: the label is already hidden by nav.css, so only the
   padding needs to come in to keep the dot on the icons' centre line. */
body[data-sidebar="collapsed"] #sidebar-footer .server-status--compact {
    gap: 0;
    padding-inline: 11px;
}

.server-status--full {
    margin-top: var(--space-6);
    justify-content: center;
    font-size: var(--text-sm);
    white-space: normal;
    text-align: left;
}

/* Mobile app shell: the sidebar copy is behind the drawer, so the brand
   strip carries one. Pushed to the trailing edge, and never a tap target of
   its own — the strip is one link. */
#mobile-brand .server-status--compact {
    margin-left: auto;
    background: transparent;
    border-color: var(--border);
    color: var(--text-2);
    font-size: 10px;
    padding: 3px var(--space-2);
}

.server-status--full .server-status__text strong {
    color: var(--status-color);
    font-weight: 700;
}

@media (max-width: 700px) {
    /* The public header is tight on phones — the brand wraps if this keeps
       its label — so the dot alone carries the state there. The landing hero
       repeats it in words a few hundred pixels below, and the app shell's
       copy lives in the brand strip, which has the room. */
    .public-header .server-status__text { display: none; }
    /* Down to just the dot: the pill's border and padding cost ~30px of a
       390px header, which is the difference between the wordmark fitting on
       one line and wrapping. */
    .public-header .server-status--compact {
        padding: 0;
        gap: 0;
        background: none;
        border-color: transparent;
    }

    /* Centre with the rest of the stacked hero, and drop the hostname
       (display:none, so it leaves the a11y tree too rather than being read
       aloud but invisible): the count is the part worth the line width. */
    .server-status--full {
        margin-top: var(--space-5);
        text-align: center;
        align-items: flex-start;
    }
    .server-status--full .server-status__dot { margin-top: 6px; }
    .server-status__host { display: none; }
}

@media (max-width: 360px) {
    #mobile-brand .server-status__text { display: none; }
}
