/* ==========================================================================
   Component library — buttons, forms, cards, tables, overlays, feedback
   ========================================================================== */

/* ── Buttons ───────────────────────────────────────────────────────────── */

.btn {
    --btn-bg: transparent;
    --btn-ink: var(--ink);
    --btn-line: var(--line-strong);

    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--sp-2);
    height: 36px;
    padding: 0 var(--sp-4);
    border: 1px solid var(--btn-line);
    border-radius: var(--r-md);
    background: var(--btn-bg);
    color: var(--btn-ink);
    font-size: var(--text-md);
    font-weight: 500;
    white-space: nowrap;
    user-select: none;
    transition: background var(--dur-fast) var(--ease),
                border-color var(--dur-fast) var(--ease),
                color var(--dur-fast) var(--ease),
                transform var(--dur-fast) var(--ease);
}

.btn:hover:not(:disabled) { background: var(--bg-raised); border-color: var(--ink-faint); }
.btn:active:not(:disabled) { transform: translateY(.5px); }
.btn:disabled { opacity: .48; cursor: not-allowed; }

.btn svg { width: 16px; height: 16px; flex: none; }

.btn-primary {
    --btn-bg: var(--brand);
    --btn-ink: var(--brand-ink);
    --btn-line: var(--brand);
    font-weight: 600;
}
.btn-primary:hover:not(:disabled) { background: var(--brand-hover); border-color: var(--brand-hover); }

.btn-soft {
    --btn-bg: var(--brand-soft);
    --btn-ink: var(--brand);
    --btn-line: var(--brand-line);
}
.btn-soft:hover:not(:disabled) { background: var(--brand-soft); border-color: var(--brand); }

.btn-ghost { --btn-line: transparent; --btn-ink: var(--ink-muted); }
.btn-ghost:hover:not(:disabled) { color: var(--ink-strong); background: var(--bg-raised); border-color: transparent; }

.btn-danger { --btn-bg: var(--danger-soft); --btn-ink: var(--danger); --btn-line: transparent; }
.btn-danger:hover:not(:disabled) { background: var(--danger); color: #fff; border-color: var(--danger); }

.btn-sm { height: 30px; padding: 0 var(--sp-3); font-size: var(--text-sm); }
.btn-lg { height: 44px; padding: 0 var(--sp-6); font-size: var(--text-lg); border-radius: var(--r-lg); }
.btn-icon { width: 36px; padding: 0; }
.btn-icon.btn-sm { width: 30px; }
.btn-block { width: 100%; }

.btn.is-loading { color: transparent !important; pointer-events: none; position: relative; }
.btn.is-loading::after {
    content: '';
    position: absolute;
    inset: 0;
    margin: auto;
    width: 15px; height: 15px;
    border: 2px solid currentColor;
    border-top-color: transparent;
    border-radius: 50%;
    color: var(--btn-ink);
    animation: tm-spin .6s linear infinite;
}

@keyframes tm-spin { to { transform: rotate(360deg); } }

/* ── Form controls ─────────────────────────────────────────────────────── */

.field { display: flex; flex-direction: column; gap: var(--sp-2); min-width: 0; }

.label {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--ink-muted);
    display: flex;
    align-items: center;
    gap: var(--sp-2);
}
.label .req { color: var(--danger); }

.input, .select, .textarea {
    width: 100%;
    min-height: 36px;
    padding: 7px var(--sp-3);
    background: var(--bg-inset);
    border: 1px solid var(--line-strong);
    border-radius: var(--r-md);
    color: var(--ink-strong);
    font-size: var(--text-md);
    transition: border-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}

.input::placeholder, .textarea::placeholder { color: var(--ink-faint); }

.input:focus, .select:focus, .textarea:focus {
    outline: none;
    border-color: var(--brand);
    box-shadow: var(--shadow-ring);
}

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

.textarea { min-height: 96px; resize: vertical; line-height: 1.5; }

.select {
    appearance: none;
    padding-right: var(--sp-8);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238f97a6' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right var(--sp-3) center;
}

.field.has-error .input,
.field.has-error .select,
.field.has-error .textarea { border-color: var(--danger); }

.field-error { font-size: var(--text-xs); color: var(--danger); }
.field-hint  { font-size: var(--text-xs); color: var(--ink-faint); }

.input-group { position: relative; display: flex; align-items: center; }
.input-group .input { padding-left: var(--sp-8); }
.input-group > svg {
    position: absolute;
    left: var(--sp-3);
    width: 15px; height: 15px;
    color: var(--ink-faint);
    pointer-events: none;
}

.checkbox, .radio {
    display: inline-flex;
    align-items: center;
    gap: var(--sp-2);
    cursor: pointer;
    font-size: var(--text-md);
    user-select: none;
}
.checkbox input, .radio input { width: 16px; height: 16px; accent-color: var(--brand); cursor: pointer; }

.switch { position: relative; display: inline-flex; align-items: center; gap: var(--sp-3); cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
    width: 38px; height: 22px;
    background: var(--line-strong);
    border-radius: var(--r-full);
    position: relative;
    transition: background var(--dur) var(--ease);
    flex: none;
}
.switch-track::after {
    content: '';
    position: absolute;
    top: 3px; left: 3px;
    width: 16px; height: 16px;
    background: #fff;
    border-radius: 50%;
    transition: transform var(--dur) var(--ease);
}
.switch input:checked + .switch-track { background: var(--brand); }
.switch input:checked + .switch-track::after { transform: translateX(16px); }
.switch input:focus-visible + .switch-track { box-shadow: var(--shadow-ring); }

/* ── Surfaces ──────────────────────────────────────────────────────────── */

.card {
    background: var(--bg-surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    overflow: hidden;
}

.card-pad { padding: var(--sp-5); }

.card-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--sp-3);
    padding: var(--sp-4) var(--sp-5);
    border-bottom: 1px solid var(--line);
}
.card-head h3, .card-head h4 { font-size: var(--text-lg); }

.card-body { padding: var(--sp-5); }
.card-foot {
    padding: var(--sp-4) var(--sp-5);
    border-top: 1px solid var(--line);
    background: var(--bg-inset);
}

.panel {
    background: var(--bg-surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    padding: var(--sp-4);
}

/* ── Stat tile ─────────────────────────────────────────────────────────── */

.stat {
    background: var(--bg-surface);
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    padding: var(--sp-4) var(--sp-5);
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    min-width: 0;
}
.stat-label {
    font-size: var(--text-xs);
    color: var(--ink-muted);
    text-transform: uppercase;
    letter-spacing: .06em;
    font-weight: 600;
}
.stat-value {
    font-family: var(--font-display);
    font-size: var(--text-3xl);
    font-weight: 700;
    color: var(--ink-strong);
    font-variant-numeric: tabular-nums;
    line-height: 1.1;
}
.stat-meta { font-size: var(--text-xs); color: var(--ink-faint); }
.stat-delta { display: inline-flex; align-items: center; gap: 4px; font-weight: 600; }
.stat-delta.up { color: var(--ok); }
.stat-delta.down { color: var(--danger); }

/* ── Badges & pills ────────────────────────────────────────────────────── */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 21px;
    padding: 0 8px;
    border-radius: var(--r-full);
    font-size: var(--text-xs);
    font-weight: 600;
    line-height: 1;
    white-space: nowrap;
    background: var(--bg-raised);
    color: var(--ink-muted);
    border: 1px solid var(--line);
}
.badge-ok     { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge-warn   { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge-info   { background: var(--info-soft); color: var(--info); border-color: transparent; }
.badge-brand  { background: var(--brand-soft); color: var(--brand); border-color: transparent; }

.badge-dot::before {
    content: '';
    width: 6px; height: 6px;
    border-radius: 50%;
    background: currentColor;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 28px;
    padding: 0 var(--sp-3);
    border-radius: var(--r-full);
    border: 1px solid var(--line-strong);
    background: var(--bg-surface);
    color: var(--ink-muted);
    font-size: var(--text-sm);
    cursor: pointer;
    transition: all var(--dur-fast) var(--ease);
}
.chip:hover { color: var(--ink-strong); border-color: var(--ink-faint); }
.chip.is-active { background: var(--brand-soft); color: var(--brand); border-color: var(--brand-line); }

/* ── Tables ────────────────────────────────────────────────────────────── */

.table-wrap {
    border: 1px solid var(--line);
    border-radius: var(--r-lg);
    background: var(--bg-surface);
    overflow-x: auto;
}

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

.table thead th {
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--bg-surface);
    text-align: left;
    font-size: var(--text-xs);
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: .06em;
    color: var(--ink-faint);
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--line);
    white-space: nowrap;
}

.table tbody td {
    padding: var(--sp-3) var(--sp-4);
    border-bottom: 1px solid var(--line);
    vertical-align: middle;
    color: var(--ink);
}

.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr { transition: background var(--dur-fast) var(--ease); }
.table tbody tr:hover { background: var(--bg-raised); }
.table tbody tr.is-clickable { cursor: pointer; }

.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.table .actions { text-align: right; white-space: nowrap; }

.table-empty {
    padding: var(--sp-12) var(--sp-6);
    text-align: center;
    color: var(--ink-faint);
}

/* ── Empty & loading states ────────────────────────────────────────────── */

.empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--sp-3);
    padding: var(--sp-16) var(--sp-6);
    text-align: center;
    color: var(--ink-muted);
}
.empty-icon {
    width: 46px; height: 46px;
    display: grid; place-items: center;
    border-radius: var(--r-lg);
    background: var(--bg-raised);
    color: var(--ink-faint);
}
.empty-icon svg { width: 22px; height: 22px; }
.empty h3 { font-size: var(--text-lg); }
.empty p { max-width: 380px; font-size: var(--text-sm); }

.skeleton {
    background: linear-gradient(90deg, var(--bg-raised) 25%, var(--line) 50%, var(--bg-raised) 75%);
    background-size: 200% 100%;
    animation: tm-shimmer 1.4s ease-in-out infinite;
    border-radius: var(--r-sm);
}
@keyframes tm-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

.skeleton-text { height: 12px; margin-block: 5px; }
.skeleton-card { height: 140px; border-radius: var(--r-lg); }

.spinner {
    width: 18px; height: 18px;
    border: 2px solid var(--line-strong);
    border-top-color: var(--brand);
    border-radius: 50%;
    animation: tm-spin .6s linear infinite;
}

/* ── Tabs & segmented control ──────────────────────────────────────────── */

.tabs {
    display: flex;
    gap: var(--sp-1);
    border-bottom: 1px solid var(--line);
    overflow-x: auto;
}
.tab {
    position: relative;
    padding: var(--sp-3) var(--sp-4);
    background: none;
    border: 0;
    color: var(--ink-muted);
    font-size: var(--text-md);
    font-weight: 500;
    white-space: nowrap;
    transition: color var(--dur-fast) var(--ease);
}
.tab::after {
    content: '';
    position: absolute;
    left: var(--sp-3); right: var(--sp-3); bottom: -1px;
    height: 2px;
    background: transparent;
    border-radius: 2px 2px 0 0;
    transition: background var(--dur-fast) var(--ease);
}
.tab:hover { color: var(--ink-strong); }
.tab.is-active { color: var(--ink-strong); }
.tab.is-active::after { background: var(--brand); }

.segmented {
    display: inline-flex;
    padding: 3px;
    gap: 2px;
    background: var(--bg-inset);
    border: 1px solid var(--line);
    border-radius: var(--r-md);
}
.segmented button {
    height: 28px;
    padding: 0 var(--sp-3);
    border: 0;
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--ink-muted);
    font-size: var(--text-sm);
    font-weight: 500;
    transition: all var(--dur-fast) var(--ease);
}
.segmented button:hover { color: var(--ink-strong); }
.segmented button.is-active { background: var(--bg-raised); color: var(--ink-strong); box-shadow: var(--shadow-sm); }

/* ── Modal ─────────────────────────────────────────────────────────────── */

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: var(--bg-overlay);
    backdrop-filter: blur(3px);
    display: grid;
    place-items: center;
    padding: var(--sp-5);
    animation: tm-fade var(--dur) var(--ease);
}

@keyframes tm-fade { from { opacity: 0; } }
@keyframes tm-pop { from { opacity: 0; transform: translateY(10px) scale(.985); } }

.modal {
    width: min(560px, 100%);
    max-height: min(86vh, 900px);
    display: flex;
    flex-direction: column;
    background: var(--bg-surface);
    border: 1px solid var(--line-strong);
    border-radius: var(--r-xl);
    box-shadow: var(--shadow-lg);
    animation: tm-pop var(--dur) var(--ease);
}
.modal-lg { width: min(860px, 100%); }
.modal-xl { width: min(1120px, 100%); }

.modal-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--sp-4);
    padding: var(--sp-5) var(--sp-5) var(--sp-4);
    border-bottom: 1px solid var(--line);
}
.modal-title { font-size: var(--text-xl); }
.modal-sub { font-size: var(--text-sm); color: var(--ink-muted); margin-top: 3px; }
.modal-body { padding: var(--sp-5); overflow-y: auto; flex: 1; }
.modal-foot {
    display: flex;
    justify-content: flex-end;
    gap: var(--sp-2);
    padding: var(--sp-4) var(--sp-5);
    border-top: 1px solid var(--line);
    background: var(--bg-inset);
}

/* ── Drawer ────────────────────────────────────────────────────────────── */

.drawer-backdrop {
    position: fixed; inset: 0; z-index: 190;
    background: var(--bg-overlay);
    animation: tm-fade var(--dur) var(--ease);
}
.drawer {
    position: fixed;
    top: 0; right: 0; bottom: 0;
    z-index: 191;
    width: min(520px, 100%);
    background: var(--bg-surface);
    border-left: 1px solid var(--line-strong);
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    animation: tm-slide var(--dur-slow) var(--ease);
}
@keyframes tm-slide { from { transform: translateX(24px); opacity: 0; } }

/* ── Toasts ────────────────────────────────────────────────────────────── */

.toasts {
    position: fixed;
    bottom: var(--sp-5);
    right: var(--sp-5);
    z-index: 300;
    display: flex;
    flex-direction: column;
    gap: var(--sp-2);
    max-width: min(400px, calc(100vw - 32px));
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    background: var(--bg-raised);
    border: 1px solid var(--line-strong);
    border-left: 3px solid var(--ink-faint);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-md);
    pointer-events: auto;
    animation: tm-toast var(--dur) var(--ease);
}
@keyframes tm-toast { from { opacity: 0; transform: translateX(18px); } }

.toast.is-leaving { opacity: 0; transform: translateX(18px); transition: all var(--dur) var(--ease); }
.toast-success { border-left-color: var(--ok); }
.toast-error   { border-left-color: var(--danger); }
.toast-warning { border-left-color: var(--warn); }
.toast-info    { border-left-color: var(--info); }
.toast svg { width: 17px; height: 17px; flex: none; margin-top: 1px; }
.toast-success svg { color: var(--ok); }
.toast-error svg   { color: var(--danger); }
.toast-warning svg { color: var(--warn); }
.toast-info svg    { color: var(--info); }
.toast-close { background: none; border: 0; color: var(--ink-faint); padding: 0; line-height: 1; }
.toast-close:hover { color: var(--ink-strong); }

/* ── Alert / banner ────────────────────────────────────────────────────── */

.alert {
    display: flex;
    align-items: flex-start;
    gap: var(--sp-3);
    padding: var(--sp-3) var(--sp-4);
    border-radius: var(--r-md);
    border: 1px solid var(--line);
    background: var(--bg-raised);
    font-size: var(--text-md);
}
.alert svg { width: 17px; height: 17px; flex: none; margin-top: 2px; }
.alert-warn   { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.alert-danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }
.alert-ok     { background: var(--ok-soft); border-color: transparent; color: var(--ok); }
.alert-info   { background: var(--info-soft); border-color: transparent; color: var(--info); }
.alert strong { color: inherit; }

/* ── Dropdown menu ─────────────────────────────────────────────────────── */

.menu {
    min-width: 190px;
    padding: var(--sp-1);
    background: var(--bg-raised);
    border: 1px solid var(--line-strong);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-lg);
    animation: tm-pop var(--dur-fast) var(--ease);
}
.menu-item {
    display: flex;
    align-items: center;
    gap: var(--sp-3);
    width: 100%;
    padding: 7px var(--sp-3);
    border: 0;
    border-radius: var(--r-sm);
    background: none;
    color: var(--ink);
    font-size: var(--text-md);
    text-align: left;
    transition: background var(--dur-fast) var(--ease);
}
.menu-item:hover { background: var(--bg-surface); color: var(--ink-strong); }
.menu-item.is-danger { color: var(--danger); }
.menu-item svg { width: 15px; height: 15px; color: var(--ink-faint); }
.menu-item.is-danger svg { color: var(--danger); }
.menu-sep { height: 1px; background: var(--line); margin: var(--sp-1) 0; }
.menu-label {
    padding: var(--sp-2) var(--sp-3) 4px;
    font-size: var(--text-2xs);
    text-transform: uppercase;
    letter-spacing: .07em;
    color: var(--ink-faint);
    font-weight: 600;
}

/* ── Progress & meters ─────────────────────────────────────────────────── */

.meter { height: 6px; background: var(--bg-inset); border-radius: var(--r-full); overflow: hidden; }
.meter-fill { height: 100%; background: var(--brand); border-radius: inherit; transition: width var(--dur-slow) var(--ease); }
.meter-fill.is-warn { background: var(--warn); }
.meter-fill.is-danger { background: var(--danger); }

/* ── Avatar ────────────────────────────────────────────────────────────── */

.avatar {
    width: 32px; height: 32px;
    border-radius: var(--r-full);
    background: var(--brand-soft);
    color: var(--brand);
    display: grid;
    place-items: center;
    font-size: var(--text-sm);
    font-weight: 600;
    flex: none;
    overflow: hidden;
    user-select: none;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar-lg { width: 44px; height: 44px; font-size: var(--text-lg); }
.avatar-sm { width: 24px; height: 24px; font-size: var(--text-xs); }

/* ── Tooltip ───────────────────────────────────────────────────────────── */

[data-tip] { position: relative; }
[data-tip]::after {
    content: attr(data-tip);
    position: absolute;
    bottom: calc(100% + 7px);
    left: 50%;
    transform: translateX(-50%);
    padding: 5px 9px;
    background: var(--bg-raised);
    border: 1px solid var(--line-strong);
    border-radius: var(--r-sm);
    box-shadow: var(--shadow-md);
    font-size: var(--text-xs);
    color: var(--ink-strong);
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur-fast) var(--ease);
    z-index: 50;
}
[data-tip]:hover::after { opacity: 1; }

/* ── Pagination ────────────────────────────────────────────────────────── */

.pagination { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); padding-top: var(--sp-4); }
.pagination-pages { display: flex; gap: var(--sp-1); }
.pagination-pages button {
    min-width: 32px; height: 32px;
    padding: 0 var(--sp-2);
    border: 1px solid transparent;
    border-radius: var(--r-sm);
    background: transparent;
    color: var(--ink-muted);
    font-size: var(--text-sm);
    font-variant-numeric: tabular-nums;
}
.pagination-pages button:hover:not(:disabled) { background: var(--bg-raised); color: var(--ink-strong); }
.pagination-pages button.is-active { background: var(--brand-soft); color: var(--brand); border-color: var(--brand-line); }
.pagination-pages button:disabled { opacity: .4; cursor: default; }

/* A badge is an inline pill: inside a column flex container the default
   `align-items: stretch` would blow it out to the full column width. */
.stack > .badge,
.stack-sm > .badge,
.stack-lg > .badge,
.stat > .badge { align-self: flex-start; }

/* Star rating, used by the tour editor and by the public tour page. */
.stars { color: var(--accent); letter-spacing: 1px; font-size: var(--text-sm); }
