/* ==========================================================================
   components.css — reusable UI primitives. Every colour, size, radius and
   shadow comes from tokens (tokens.css / themes.css). Each interactive
   component defines default, hover, active, focus-visible and disabled.
   ========================================================================== */

/* ---------- base element reset ---------- */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }
html, body { height: 100%; }
body {
    font-family: var(--font-ui);
    font-size: var(--fs-base);
    line-height: var(--lh-base);
    color: var(--text-1);
    background: var(--surface-0);
    -webkit-font-smoothing: antialiased;
    overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.5; }
kbd, code { font-family: var(--font-mono); }

:focus { outline: none; }
:focus-visible {
    outline: var(--focus-width) solid var(--focus-ring, var(--accent));
    outline-offset: var(--focus-offset);
    border-radius: var(--radius-xs);
}

/* ---------- scrollbars ---------- */
* { scrollbar-width: thin; scrollbar-color: var(--scroll-thumb) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
    background: var(--scroll-thumb);
    border-radius: var(--radius-pill);
    border: 3px solid transparent;
    background-clip: padding-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--scroll-thumb-hover); background-clip: padding-box; }
::-webkit-scrollbar-corner { background: transparent; }

/* ---------- buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    height: var(--control-h-md);
    padding: 0 var(--space-3);
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--surface-3);
    color: var(--text-1);
    font-size: var(--fs-base);
    font-weight: var(--fw-medium);
    white-space: nowrap;
    user-select: none;
    transition: background var(--dur-fast) var(--ease-out),
                border-color var(--dur-fast) var(--ease-out),
                color var(--dur-fast) var(--ease-out),
                transform var(--dur-instant) var(--ease-out);
}
.btn:hover { background: var(--surface-4); border-color: var(--border-strong); }
.btn:active { transform: translateY(1px); background: var(--state-active); }
.btn:disabled:hover { background: var(--surface-3); border-color: var(--border); }

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--text-on-accent);
    font-weight: var(--fw-semibold);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-primary:active { background: var(--accent-hover); }

/* Run/loading state: the button's icon is swapped for a spinning ring. */
.btn.is-running { pointer-events: none; }
.btn.is-running > [data-icon] { display: none; }
.btn.is-running::before {
    content: '';
    flex: 0 0 auto;
    width: 12px; height: 12px;
    border: 2px solid currentColor;
    border-right-color: transparent;
    border-radius: 50%;
    animation: dp-spin 0.7s linear infinite;
}
@keyframes dp-spin { to { transform: rotate(360deg); } }

.btn-secondary { background: var(--surface-3); }

.btn-ghost {
    background: transparent;
    border-color: transparent;
    color: var(--text-2);
}
.btn-ghost:hover { background: var(--state-hover); border-color: transparent; color: var(--text-1); }
.btn-ghost:active { background: var(--state-active); }

.btn-danger {
    background: var(--error-soft);
    border-color: transparent;
    color: var(--error);
}
.btn-danger:hover { background: var(--error); border-color: var(--error); color: var(--text-on-status); }

.btn-sm { height: var(--control-h-sm); padding: 0 var(--space-2); font-size: var(--fs-sm); }

.btn-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--control-h-md);
    height: var(--control-h-md);
    padding: 0;
    border-radius: var(--radius-sm);
    border-color: transparent;
    background: transparent;
    color: var(--text-2);
}
.btn-icon:hover { background: var(--state-hover); color: var(--text-1); border-color: transparent; }
.btn-icon:active { background: var(--state-active); }
.btn-icon.is-active,
.btn-icon[aria-pressed="true"] { color: var(--accent); background: var(--accent-soft); }
.btn-icon.btn-sm { width: var(--control-h-sm); height: var(--control-h-sm); }

.btn-group {
    display: inline-flex;
    gap: 2px;
    padding: 2px;
    border-radius: var(--radius-md);
    background: var(--surface-0);
    border: 1px solid var(--border-subtle);
}
.btn-group > button {
    height: var(--control-h-sm);
    padding: 0 var(--space-2);
    border-radius: var(--radius-xs);
    color: var(--text-2);
    font-size: var(--fs-sm);
}
.btn-group > button:hover { color: var(--text-1); background: var(--state-hover); }
.btn-group > button[aria-pressed="true"],
.btn-group > button.active { background: var(--surface-4); color: var(--text-1); box-shadow: var(--shadow-1); }

/* Legacy classes created by logic modules (app.js project dialog). */
.project-row button { height: var(--control-h-sm); padding: 0 var(--space-2); border-radius: var(--radius-xs);
    background: var(--surface-3); border: 1px solid var(--border); font-size: var(--fs-sm); }
.project-row button:hover { background: var(--surface-4); border-color: var(--border-strong); }
.project-row .btn-del { color: var(--error); }

/* ---------- inputs ---------- */
input[type="text"], input[type="password"], input[type="number"], input[type="search"],
select, textarea, .input {
    font: inherit;
    font-size: var(--fs-base);
    color: var(--text-1);
    background: var(--surface-0);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    height: var(--control-h-md);
    padding: 0 var(--space-3);
    transition: border-color var(--dur-fast) var(--ease-out), box-shadow var(--dur-fast) var(--ease-out);
}
textarea { height: auto; padding: var(--space-2) var(--space-3); resize: vertical; }
input::placeholder, textarea::placeholder { color: var(--text-3); }
input:hover, select:hover, textarea:hover { border-color: var(--border-strong); }
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
input:disabled, select:disabled, textarea:disabled { opacity: 0.5; }
select { padding-right: var(--space-6); appearance: none;
    background-image: linear-gradient(45deg, transparent 50%, var(--text-3) 50%),
                      linear-gradient(135deg, var(--text-3) 50%, transparent 50%);
    background-position: calc(100% - 13px) 50%, calc(100% - 9px) 50%;
    background-size: 4px 4px, 4px 4px;
    background-repeat: no-repeat; }

/* Custom checkbox: accent-filled rounded box with a drawn check mark.
   (Inputs inside .switch stay invisible — the switch styles win later.) */
input[type="checkbox"] {
    appearance: none; -webkit-appearance: none;
    flex: 0 0 auto;
    width: 15px; height: 15px; margin: 0;
    display: inline-grid; place-content: center;
    vertical-align: -2px;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xs);
    background: var(--surface-3);
    cursor: pointer;
    transition: background var(--dur-fast) var(--ease-out),
                border-color var(--dur-fast) var(--ease-out),
                box-shadow var(--dur-fast) var(--ease-out);
}
input[type="checkbox"]:hover { border-color: var(--accent); }
input[type="checkbox"]:focus-visible {
    outline: none; border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type="checkbox"]::after {
    content: '';
    width: 9px; height: 9px;
    background: var(--text-on-accent);
    clip-path: polygon(14% 44%, 0% 62%, 40% 100%, 100% 18%, 86% 2%, 41% 68%);
    transform: scale(0);
    transition: transform var(--dur-fast) var(--ease-out);
}
input[type="checkbox"]:checked { background: var(--accent); border-color: var(--accent); }
input[type="checkbox"]:checked::after { transform: scale(1); }
input[type="checkbox"]:disabled { opacity: 0.5; cursor: default; }

/* ---------- custom select dropdown (js/ui/selects.js) ---------- */
/* The enhanced <select> stays in the DOM for logic but is invisible. */
select.dp-native {
    position: absolute !important;
    width: 1px !important; height: 1px !important;
    padding: 0 !important; margin: 0 !important;
    opacity: 0; pointer-events: none;
}
.dp-select { position: relative; display: inline-flex; align-items: stretch; max-width: 100%; }
.dp-select-btn {
    display: inline-flex; align-items: center; gap: var(--space-2);
    width: 100%; height: 100%; min-height: var(--control-h-sm);
    margin: 0;
    padding: 0 var(--space-2) 0 var(--space-3);
    font: inherit; font-size: var(--fs-base); color: var(--text-1);
    background: var(--surface-3);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    cursor: pointer; white-space: nowrap;
    transition: border-color var(--dur-fast) var(--ease-out),
                box-shadow var(--dur-fast) var(--ease-out);
}
.dp-select-btn:hover { border-color: var(--border-strong); }
.dp-select-btn:focus-visible {
    outline: none; border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.dp-select.is-open .dp-select-btn { border-color: var(--accent); }
.dp-select-btn[disabled] { opacity: 0.5; cursor: default; }
.dp-select-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; text-align: left; }
.dp-select-btn .dp-select-arrow {
    width: 14px; height: 14px; flex: 0 0 auto;
    color: var(--text-3);
    transition: transform var(--dur-fast) var(--ease-out);
}
.dp-select.is-open .dp-select-arrow { transform: rotate(180deg); }

/* Popup list — appended to <body> and fixed-positioned so dialogs and
   scrollable panels can't clip it. Sits above the modal layer. */
.dp-menu {
    position: fixed;
    z-index: calc(var(--z-modal) + 10);
    min-width: 140px; max-width: min(320px, 92vw); max-height: 260px;
    overflow-y: auto;
    padding: var(--space-1);
    background: var(--surface-4);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-3);
}
.dp-option {
    display: flex; align-items: center; gap: var(--space-2);
    width: 100%; margin-top: 0;
    padding: var(--space-2) var(--space-3);
    border: 0; border-radius: var(--radius-xs);
    background: transparent; color: var(--text-2);
    font: inherit; font-size: var(--fs-base); text-align: left;
    cursor: pointer; white-space: nowrap;
}
.dp-option:hover, .dp-option:focus-visible {
    background: var(--state-hover); color: var(--text-1); outline: none;
}
.dp-option[aria-selected="true"] { color: var(--text-1); }
.dp-option-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.dp-option .icon { width: 14px; height: 14px; flex: 0 0 auto; color: var(--accent); opacity: 0; }
.dp-option[aria-selected="true"] .icon { opacity: 1; }
.dp-option[disabled] { color: var(--text-disabled); cursor: default; }
.dp-option[disabled]:hover { background: transparent; color: var(--text-disabled); }

/* Toggle switch: <label class="switch"><input type="checkbox"><span></span></label> */
.switch { position: relative; display: inline-flex; width: 32px; height: 18px; flex: 0 0 auto; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch span {
    position: absolute; inset: 0; border-radius: var(--radius-pill);
    background: var(--surface-4); border: 1px solid var(--border-strong);
    transition: background var(--dur-base) var(--ease-out), border-color var(--dur-base) var(--ease-out);
    pointer-events: none;
}
.switch span::after {
    content: ""; position: absolute; top: 2px; left: 2px; width: 12px; height: 12px;
    border-radius: 50%; background: var(--text-2);
    transition: transform var(--dur-base) var(--ease-spring), background var(--dur-base) var(--ease-out);
}
.switch input:checked + span { background: var(--accent); border-color: var(--accent); }
.switch input:checked + span::after { transform: translateX(14px); background: var(--text-on-accent); }
.switch input:focus-visible + span { outline: var(--focus-width) solid var(--focus-ring, var(--accent)); outline-offset: var(--focus-offset); }
.switch input:disabled + span { opacity: 0.5; }

/* ---------- tabs (WAI-ARIA tablist) ---------- */
.tablist {
    display: flex;
    align-items: stretch;
    gap: var(--space-1);
    border-bottom: 1px solid var(--border-subtle);
}
.tablist > [role="tab"],
.tab-btn {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    height: var(--tabbar-h);
    padding: 0 var(--space-3);
    color: var(--text-3);
    font-size: var(--fs-sm);
    font-weight: var(--fw-medium);
    border-radius: 0;
    transition: color var(--dur-fast) var(--ease-out), background var(--dur-fast) var(--ease-out);
}
.tablist > [role="tab"]:hover { color: var(--text-1); background: var(--state-hover); }
.tablist > [role="tab"][aria-selected="true"] { color: var(--text-1); }
.tablist > [role="tab"][aria-selected="true"]::after {
    content: ""; position: absolute; left: var(--space-2); right: var(--space-2); bottom: -1px;
    height: 2px; border-radius: 2px; background: var(--accent);
    box-shadow: 0 0 10px -1px var(--accent-glow);
}

/* ---------- tooltip (JS-driven, see js/ui/tooltip.js) ---------- */
/* The tip lives on <body> with position: fixed so it is never clipped
   by an overflow:hidden panel. data-tip-pos="top" | "right" picks a side. */
.owlet-tip {
    position: fixed;
    display: none;
    padding: 4px 8px;
    background: var(--surface-4);
    color: var(--text-1);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-xs);
    font-size: var(--fs-xs);
    font-weight: var(--fw-medium);
    white-space: nowrap;
    box-shadow: var(--shadow-2);
    pointer-events: none;
    z-index: var(--z-popover);
}

/* ---------- small dialog helpers still used by JS ---------- */
.dialog-empty { color: var(--text-3); padding: var(--space-3) 0; }
.project-row .btn-del:hover { color: var(--error); border-color: var(--error); }
.prompt-desc { color: var(--text-2); font-size: var(--fs-base); margin: 0 0 var(--space-3); overflow-wrap: anywhere; }
.prompt-input { width: 100%; }
.prompt-input.is-invalid { border-color: var(--error); }
.prompt-error { color: var(--error); font-size: var(--fs-sm); margin: var(--space-2) 0 0; }

/* ---------- command palette ---------- */
.palette-box { width: 520px; padding: var(--space-2) 0 var(--space-2); overflow: hidden; }
#palette-input {
    width: 100%;
    padding: var(--space-3) var(--space-4);
    border: 0;
    border-bottom: 1px solid var(--border);
    border-radius: 0;
    background: transparent;
    color: var(--text-1);
    font-size: var(--fs-md);
    outline: none;
}
/* The palette is a search field, not a form input — no accent highlight. */
#palette-input:focus { border-bottom-color: var(--border); box-shadow: none; }
#palette-results { max-height: 320px; overflow: auto; padding: var(--space-1) 0 0; }
.palette-empty { padding: var(--space-4) var(--space-4); color: var(--text-3); font-size: var(--fs-sm); }
.palette-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-2) var(--space-4);
    font-size: var(--fs-base);
    cursor: pointer;
}
.palette-item .palette-hint { flex: 0 0 auto; color: var(--text-3); font-family: var(--font-mono); font-size: var(--fs-xs); }
.palette-item:hover,
.palette-item.selected { background: var(--accent); color: var(--text-on-accent); }
.palette-item:hover .palette-hint,
.palette-item.selected .palette-hint { color: var(--text-on-accent); opacity: 0.85; }

/* ---------- saved projects list ---------- */
.project-row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    margin-bottom: var(--space-2);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    background: var(--surface-3);
    font-size: var(--fs-base);
}
.project-row:last-child { margin-bottom: 0; }
.project-row .project-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: var(--fw-semibold); }
.project-row button { margin-top: 0; height: 24px; padding: 0 var(--space-2); font-size: var(--fs-xs); }
.project-row .btn-load:hover { color: var(--accent); border-color: var(--accent); }

/* ---------- help desk dialog rows ---------- */
.help-links { display: flex; flex-direction: column; gap: var(--space-1); }
.help-link {
    display: flex; align-items: center; gap: var(--space-3);
    width: 100%; padding: var(--space-3); margin-top: 0;
    border: 1px solid var(--border-subtle); border-radius: var(--radius-md);
    background: var(--surface-2); color: var(--text-1); text-decoration: none;
    font-size: var(--fs-base); text-align: left; cursor: pointer;
}
.help-link:hover { background: var(--state-hover); border-color: var(--border-strong); }
.help-link-name { font-weight: var(--fw-semibold); }
.help-link-desc { color: var(--text-3); font-size: var(--fs-sm); }

/* ---------- toasts ---------- */
.toast-root {
    position: fixed;
    right: var(--space-4);
    bottom: calc(var(--statusbar-h) + var(--space-4));
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    z-index: var(--z-toast);
    pointer-events: none;
}
.toast {
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    width: 340px;
    max-width: calc(100vw - 32px);
    padding: var(--space-3) var(--space-4);
    background: var(--surface-4);
    border: 1px solid var(--border-strong);
    border-left: 3px solid var(--toast-color, var(--info));
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-3);
    font-size: var(--fs-base);
    color: var(--text-1);
    overflow: hidden;
    pointer-events: auto;
    animation: toast-in var(--dur-slow) var(--ease-spring);
}
.toast.is-leaving { animation: toast-out var(--dur-base) var(--ease-out) forwards; }
.toast--success { --toast-color: var(--success); }
.toast--warn { --toast-color: var(--warn); }
.toast--error { --toast-color: var(--error); }
.toast--info { --toast-color: var(--info); }
.toast-icon { color: var(--toast-color); flex: 0 0 auto; margin-top: 1px; }
.toast-body { flex: 1; min-width: 0; }
.toast-action {
    margin-top: var(--space-2);
    color: var(--accent);
    font-weight: var(--fw-semibold);
    font-size: var(--fs-sm);
}
.toast-action:hover { color: var(--accent-hover); text-decoration: underline; }
.toast-close { color: var(--text-3); padding: 2px; border-radius: var(--radius-xs); }
.toast-close:hover { color: var(--text-1); background: var(--state-hover); }
.toast-progress {
    position: absolute; left: 0; bottom: 0; height: 2px; width: 100%;
    background: var(--toast-color, var(--info));
    transform-origin: left center;
    animation: toast-progress linear forwards;
    animation-duration: var(--toast-duration, 4000ms);
}
@keyframes toast-in {
    from { opacity: 0; transform: translateX(16px) scale(0.98); }
    to   { opacity: 1; transform: none; }
}
@keyframes toast-out {
    to { opacity: 0; transform: translateX(16px); }
}
@keyframes toast-progress { from { transform: scaleX(1); } to { transform: scaleX(0); } }

/* ---------- badges, kbd, dividers, skeletons, empty states ---------- */
.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 var(--space-1);
    border-radius: var(--radius-pill);
    background: var(--surface-4);
    color: var(--text-2);
    font-size: var(--fs-2xs);
    font-weight: var(--fw-semibold);
    font-variant-numeric: tabular-nums;
    line-height: 1;
}
.badge--error { background: var(--error-soft); color: var(--error); }
.badge--warn { background: var(--warn-soft); color: var(--warn); }
.badge--info { background: var(--info-soft); color: var(--info); }
.badge--accent { background: var(--accent-soft); color: var(--accent); }
.badge:empty { display: none; }

kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 24px;
    height: 20px;
    padding: 0 6px;
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: var(--radius-xs);
    background: var(--surface-2);
    color: var(--text-2);
    font-family: var(--font-mono);
    font-size: var(--fs-2xs);
    line-height: 1;
}

.divider-line { height: 1px; background: var(--border-subtle); margin: var(--space-2) 0; }

.skeleton {
    position: relative;
    overflow: hidden;
    border-radius: var(--radius-xs);
    background: var(--surface-3);
}
.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    background: linear-gradient(90deg, transparent, var(--state-active), transparent);
    transform: translateX(-100%);
    animation: shimmer 1.4s var(--ease-in-out) infinite;
}
@keyframes shimmer { to { transform: translateX(100%); } }

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-8) var(--space-4);
    text-align: center;
    color: var(--text-3);
}
.empty-state .empty-icon {
    width: 40px; height: 40px;
    display: grid; place-items: center;
    border-radius: var(--radius-lg);
    background: var(--surface-3);
    color: var(--text-2);
}
.empty-state h4 { color: var(--text-1); font-size: var(--fs-md); font-weight: var(--fw-semibold); margin: 0; }
.empty-state p { font-size: var(--fs-base); max-width: 280px; }
.empty-state .btn { margin-top: var(--space-2); }

/* ---------- icons ---------- */
.icon {
    width: var(--icon-size);
    height: var(--icon-size);
    flex: 0 0 auto;
    display: inline-block;
    vertical-align: middle;
    fill: none;
    stroke: currentColor;
    stroke-width: var(--icon-stroke);
    stroke-linecap: round;
    stroke-linejoin: round;
}
.icon-sm { width: var(--icon-size-sm); height: var(--icon-size-sm); }
/* Hydrated [data-icon] spans wrap an svg; give them the icon box and let the svg fill it. */
[data-icon] {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--icon-size);
    height: var(--icon-size);
    flex: 0 0 auto;
}
[data-icon] > .icon { width: 100%; height: 100%; }
