/* =====================================================================
   QSD shared component layer
   ---------------------------------------------------------------------
   The canonical implementation of every repeated UI primitive. Pages
   compose these; page stylesheets hold layout only.

   Rules:
   - Colors, sizes, radii, shadows and z-indexes come from base.css /
     typography.css tokens. No hex/rgba literals here or in page CSS.
   - A second implementation of anything in this file is a bug.
   - Interactive text never renders below --font-size-xs (11px).
   ===================================================================== */

/* ---------- Buttons -------------------------------------------------
   Four variants, two sizes. One primary per view; danger only for
   destructive or live-money actions; everything else secondary/ghost. */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font-family: var(--font-family-primary);
    font-size: var(--font-size-sm);
    font-weight: 500;
    line-height: 1;
    padding: 8px 14px;
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    cursor: pointer;
    white-space: nowrap;
    transition: background var(--transition-fast), border-color var(--transition-fast), color var(--transition-fast);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

.btn-primary {
    background: var(--color-brand);
    color: #FFFFFF;
}

.btn-primary:hover:not(:disabled) {
    background: var(--color-brand-dark);
}

.btn-secondary {
    background: var(--color-surface);
    color: var(--color-text-primary);
    border-color: var(--color-border-strong);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--color-bg-secondary);
}

.btn-danger {
    background: var(--color-negative);
    color: #FFFFFF;
}

.btn-danger:hover:not(:disabled) {
    background: var(--color-negative-dark);
}

.btn-ghost {
    background: transparent;
    color: var(--color-text-secondary);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--color-bg-secondary);
    color: var(--color-text-primary);
}

.btn-sm {
    font-size: var(--font-size-data);
    padding: 5px 10px;
    border-radius: 6px;
}

/* ---------- Inputs & selects ---------------------------------------- */
.field {
    font-family: var(--font-family-primary);
    font-size: var(--font-size-sm);
    color: var(--color-text-primary);
    background: var(--color-input-bg);
    border: 1px solid var(--color-input-border);
    border-radius: var(--radius-sm);
    padding: 7px 10px;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.field:focus {
    outline: none;
    border-color: var(--color-brand);
    box-shadow: var(--focus-ring);
}

.field::placeholder {
    color: var(--color-text-tertiary);
}

.field-sm {
    font-size: var(--font-size-data);
    padding: 4px 8px;
    border-radius: 6px;
}

/* ---------- Toggle switch -------------------------------------------
   <label class="toggle"><input type="checkbox"><i></i></label>        */
.toggle {
    position: relative;
    display: inline-block;
    width: 36px;
    height: 20px;
    flex-shrink: 0;
    cursor: pointer;
}

.toggle input {
    position: absolute;
    opacity: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    cursor: pointer;
}

.toggle i {
    position: absolute;
    inset: 0;
    background: var(--color-border-strong);
    border-radius: var(--radius-pill);
    transition: background var(--transition-fast);
}

.toggle i::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #FFFFFF;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
    transition: transform var(--transition-fast);
}

.toggle input:checked + i {
    background: var(--color-brand);
}

.toggle input:checked + i::after {
    transform: translateX(16px);
}

.toggle input:focus-visible + i {
    box-shadow: var(--focus-ring);
}

/* ---------- Segmented control ----------------------------------------
   <div class="seg"><button class="on">A</button><button>B</button></div> */
.seg {
    display: inline-flex;
    gap: 2px;
    padding: 2px;
    background: var(--color-bg-secondary);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
}

.seg > button {
    font-family: var(--font-family-primary);
    font-size: var(--font-size-data);
    font-weight: 500;
    color: var(--color-text-secondary);
    background: transparent;
    border: none;
    border-radius: 6px;
    padding: 4px 12px;
    cursor: pointer;
    transition: background var(--transition-fast), color var(--transition-fast);
}

.seg > button:hover {
    color: var(--color-text-primary);
}

.seg > button.on {
    background: var(--color-surface);
    color: var(--color-text-primary);
    font-weight: 600;
    box-shadow: var(--shadow-sm);
}

/* ---------- Underline tabs -------------------------------------------
   <nav class="tabbar"><button class="tabbar-tab on">…</button>…</nav>  */
.tabbar {
    display: flex;
    gap: 2px;
    border-bottom: 2px solid var(--color-border);
}

.tabbar-tab {
    font-family: var(--font-family-primary);
    font-size: var(--font-size-base);
    font-weight: 500;
    color: var(--color-text-secondary);
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    padding: 9px 16px;
    cursor: pointer;
    transition: color var(--transition-fast), border-color var(--transition-fast);
}

.tabbar-tab:hover {
    color: var(--color-text-primary);
}

.tabbar-tab.on {
    color: var(--color-text-primary);
    font-weight: 600;
    border-bottom-color: var(--color-brand);
}

.tabbar-tab .tabbar-badge {
    margin-left: 6px;
    font-size: var(--font-size-xs);
    font-weight: 600;
    padding: 1px 7px;
    border-radius: var(--radius-pill);
    background: var(--color-bg-secondary);
    color: var(--color-text-secondary);
}

.tabbar-tab.on .tabbar-badge {
    background: var(--color-brand-tint);
    color: var(--color-brand);
}

/* ---------- Pills / badges -------------------------------------------
   One geometry; color states are semantic only.                        */
.pill {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: var(--font-size-xs);
    font-weight: 600;
    line-height: 1.4;
    padding: 2px 9px;
    border-radius: var(--radius-pill);
    white-space: nowrap;
    background: var(--color-bg-secondary);
    color: var(--color-text-secondary);
}

.pill-brand { background: var(--color-brand-tint); color: var(--color-brand); }
.pill-pos   { background: var(--color-positive-tint); color: var(--color-positive); }
.pill-neg   { background: var(--color-negative-tint); color: var(--color-negative); }
.pill-warn  { background: var(--color-warning-tint); color: var(--color-warning); }
.pill-info  { background: var(--color-info-tint); color: var(--color-info); }

/* ---------- Status dot ------------------------------------------------
   <span class="sdot sdot-on"></span> on/warn/down/off                  */
.sdot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--color-border-strong);
    flex-shrink: 0;
}

.sdot-on   { background: var(--color-positive); }
.sdot-warn { background: var(--color-warning); }
.sdot-down { background: var(--color-negative); }
.sdot-off  { background: var(--color-border-strong); }

/* ---------- Card ----------------------------------------------------- */
.card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
}

.card-pad {
    padding: var(--spacing-md) var(--spacing-lg);
}

/* ---------- Page header ----------------------------------------------
   <div class="page-head">
     <div><h1 class="page-head-title">…</h1><p class="page-head-sub>…</p></div>
     <div class="page-head-actions">buttons</div>
   </div>                                                                */
.page-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--spacing-md);
    flex-wrap: wrap;
    margin-bottom: var(--spacing-lg);
}

.page-head-title {
    font-size: var(--font-size-2xl);
    font-weight: 600;
    letter-spacing: -0.015em;
    line-height: 1.2;
    margin: 0;
    color: var(--color-text-primary);
}

.page-head-sub {
    font-size: var(--font-size-sm);
    color: var(--color-text-secondary);
    margin: 3px 0 0;
}

.page-head-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

/* ---------- Table skin ------------------------------------------------
   <div class="tbl-wrap"><table class="tbl">…</table></div>
   Numeric cells: add class "num" (right-aligned tabular figures).      */
.tbl-wrap {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
    overflow-x: auto;
}

.tbl {
    width: 100%;
    border-collapse: collapse;
    font-size: var(--font-size-sm);
    font-variant-numeric: tabular-nums;
}

.tbl th {
    text-align: left;
    font-size: var(--font-size-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--color-text-secondary);
    background: var(--color-table-header);
    padding: 8px 12px;
    border-bottom: 1px solid var(--color-border);
    white-space: nowrap;
}

.tbl td {
    padding: 7px 12px;
    border-bottom: 1px solid var(--color-border);
    color: var(--color-text-primary);
}

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

.tbl tbody tr:hover td {
    background: var(--color-table-row-hover);
}

.tbl .num {
    text-align: right;
}

.tbl-dense th { padding: 6px 8px; }
.tbl-dense td { padding: 4px 8px; font-size: var(--font-size-data); }

.tbl th.sort {
    cursor: pointer;
    user-select: none;
}

.tbl th.sort:hover {
    background: var(--color-bg-secondary);
}

.tbl th.sort-asc::after  { content: " \25B2"; font-size: 9px; color: var(--color-brand); }
.tbl th.sort-desc::after { content: " \25BC"; font-size: 9px; color: var(--color-brand); }

/* ---------- Modal -----------------------------------------------------
   <div class="modal-overlay"><div class="modal-card">…</div></div>     */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: var(--z-modal);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-lg);
}

.modal-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
    max-width: 640px;
    width: 100%;
    max-height: 82vh;
    overflow-y: auto;
    padding: var(--spacing-lg) var(--spacing-xl);
}

.modal-title {
    font-size: var(--font-size-lg);
    font-weight: 600;
    margin: 0 0 var(--spacing-sm);
    color: var(--color-text-primary);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: var(--spacing-lg);
}

/* ---------- Tooltip ----------------------------------------------------
   <span data-tip="Explanation">…</span> — for hints that deserve better
   than a native title attribute. Keep text to a couple of lines.       */
[data-tip] {
    position: relative;
}

[data-tip]:hover::after {
    content: attr(data-tip);
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--color-text-primary);
    color: var(--color-bg-primary);
    font-size: var(--font-size-data);
    font-weight: 400;
    line-height: 1.45;
    padding: 6px 10px;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    width: max-content;
    max-width: 280px;
    white-space: normal;
    text-align: left;
    z-index: var(--z-popover);
    pointer-events: none;
}

/* ---------- Empty state ---------------------------------------------- */
.empty-state {
    text-align: center;
    color: var(--color-text-tertiary);
    font-size: var(--font-size-sm);
    padding: var(--spacing-lg) 0;
}

/* ---------- Semantic text helpers ------------------------------------ */
.txt-pos  { color: var(--color-positive); }
.txt-neg  { color: var(--color-negative); }
.txt-warn { color: var(--color-warning); }
.txt-mut  { color: var(--color-text-secondary); }
.txt-mono { font-family: var(--font-family-mono); }
