/* Panel Studio — Kronaxis causal reasoning data platform */

:root {
    --bg: #0a0a0f;
    --surface: #12121a;
    --card: #1a1a26;
    --border: #2a2a3a;
    --text: #e0e0e8;
    --text-muted: #888898;
    --forge: #f59e0b;
    --panel: #6366f1;
    --soul: #a855f7;
    --data: #10b981;
    --danger: #ef4444;
    --info: #3b82f6;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    display: flex;
}

a { color: var(--panel); text-decoration: none; }
a:hover { text-decoration: underline; }

/* Sidebar */
.sidebar {
    width: 220px;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1.5rem 0;
    position: fixed;
    height: 100vh;
    overflow-y: auto;
}

.sidebar-brand {
    padding: 0 1.25rem 1.5rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 1rem;
}

.sidebar-brand h1 {
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--forge);
}

.sidebar-brand span {
    font-size: 0.75rem;
    color: var(--text-muted);
}

.sidebar-nav { list-style: none; }

.sidebar-nav li a {
    display: block;
    padding: 0.65rem 1.25rem;
    color: var(--text-muted);
    font-size: 0.9rem;
    transition: background 0.15s, color 0.15s;
}

.sidebar-nav li a:hover,
.sidebar-nav li a.active {
    background: var(--card);
    color: var(--text);
    text-decoration: none;
}

/* Main content */
.main {
    margin-left: 220px;
    flex: 1;
    padding: 2rem;
    max-width: 1400px;
}

h2 { font-size: 1.5rem; margin-bottom: 1rem; font-weight: 600; }
h3 { font-size: 1.15rem; margin-bottom: 0.75rem; font-weight: 600; }

/* Cards */
.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1.25rem;
    margin-bottom: 1rem;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.stat-card {
    text-align: center;
    padding: 1.5rem;
}

.stat-card .stat-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--forge);
}

.stat-card .stat-label {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 0.25rem;
}

/* Tables */
table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 0.65rem 0.85rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
}

th {
    font-size: 0.8rem;
    text-transform: uppercase;
    color: var(--text-muted);
    font-weight: 600;
}

td { font-size: 0.9rem; }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.15s;
}

.btn:hover { opacity: 0.85; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-primary { background: var(--forge); color: #000; }
.btn-panel { background: var(--panel); color: #fff; }
.btn-data { background: var(--data); color: #000; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
}

/* Forms */
.form-group { margin-bottom: 1rem; }

.form-group label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
}

input[type="text"],
input[type="number"],
textarea,
select {
    width: 100%;
    padding: 0.5rem 0.75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text);
    font-size: 0.9rem;
}

textarea { resize: vertical; min-height: 80px; }

input:focus, textarea:focus, select:focus {
    outline: none;
    border-color: var(--panel);
}

/* Conversation thread */
.conversation-thread {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.stimulus-bubble {
    background: var(--forge);
    color: #000;
    padding: 0.75rem 1rem;
    border-radius: 12px 12px 4px 12px;
    max-width: 70%;
    margin-left: auto;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

.response-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px 12px 12px 4px;
    padding: 1rem;
    max-width: 85%;
    margin-bottom: 1rem;
}

.response-card .sentiment-bar {
    display: flex;
    height: 6px;
    border-radius: 3px;
    overflow: hidden;
    margin: 0.5rem 0;
}

.sentiment-positive { background: var(--data); }
.sentiment-negative { background: var(--danger); }
.sentiment-neutral { background: var(--text-muted); }
.sentiment-mixed { background: var(--soul); }

/* Conversation input */
.conversation-input { flex-direction: column;
    display: flex;
    gap: 0.75rem;
    padding: 1rem;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.conversation-input textarea {
    flex: 1;
    min-height: 60px;
    max-height: 200px;
}

/* Individual response viewer */
.response-viewer {
    width: 400px;
    border-left: 1px solid var(--border);
    overflow-y: auto;
    padding: 1rem;
    background: var(--surface);
}

.persona-response {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.85rem;
    margin-bottom: 0.75rem;
}

.persona-response .persona-name {
    font-weight: 600;
    font-size: 0.85rem;
}

.persona-response .persona-meta {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.persona-response .persona-text {
    font-size: 0.85rem;
    line-height: 1.5;
}

.reasoning-trace {
    margin-top: 0.5rem;
    padding: 0.5rem;
    background: var(--bg);
    border-radius: 4px;
    font-size: 0.8rem;
    color: var(--text-muted);
}

/* Badge */
.badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 4px;
    font-size: 0.7rem;
    font-weight: 600;
    text-transform: uppercase;
}

.badge-active { background: var(--data); color: #000; }
.badge-complete { background: var(--panel); color: #fff; }
.badge-archived { background: var(--text-muted); color: #000; }
.badge-positive { background: var(--data); color: #000; }
.badge-negative { background: var(--danger); color: #fff; }
.badge-neutral { background: var(--text-muted); color: #000; }
.badge-mixed { background: var(--soul); color: #fff; }

/* Progress bar */
.progress {
    height: 8px;
    background: var(--surface);
    border-radius: 4px;
    overflow: hidden;
    margin: 0.5rem 0;
}

.progress-bar {
    height: 100%;
    background: var(--forge);
    border-radius: 4px;
    transition: width 0.3s;
}

/* Bar chart (CSS) */
.bar-chart { margin: 0.5rem 0; }

.bar-row {
    display: flex;
    align-items: center;
    margin-bottom: 0.35rem;
    font-size: 0.8rem;
}

.bar-label {
    width: 100px;
    color: var(--text-muted);
    flex-shrink: 0;
}

.bar-track {
    flex: 1;
    height: 16px;
    background: var(--surface);
    border-radius: 3px;
    overflow: hidden;
}

.bar-fill {
    height: 100%;
    background: var(--panel);
    border-radius: 3px;
}

.bar-value {
    width: 50px;
    text-align: right;
    color: var(--text-muted);
    margin-left: 0.5rem;
}

/* Split layout for conversation */
.split-layout {
    display: flex;
    height: calc(100vh - 4rem);
    background: var(--card);
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
}

.split-left {
    flex: 1;
    display: flex;
    flex-direction: column;
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 100;
    align-items: center;
    justify-content: center;
}

.modal-overlay.active { display: flex; }

.modal {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.5rem;
    width: 90%;
    max-width: 500px;
    max-height: 80vh;
    overflow-y: auto;
}

.modal h3 { margin-bottom: 1rem; }

/* Loading */
.loading {
    text-align: center;
    padding: 2rem;
    color: var(--text-muted);
}

.loading::after {
    content: "";
    display: block;
    width: 32px;
    height: 32px;
    margin: 0.75rem auto 0;
    border: 3px solid var(--border);
    border-top-color: var(--forge);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Focus group chat bubbles */
.fg-chat {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    padding: 0.5rem;
    max-height: 65vh;
    overflow-y: auto;
}

.fg-bubble-row {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    max-width: 85%;
}

.fg-bubble-row.left { align-self: flex-start; }
.fg-bubble-row.right { align-self: flex-end; flex-direction: row-reverse; }

.fg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.7rem;
    font-weight: 700;
    color: #fff;
    flex-shrink: 0;
}

.fg-bubble-wrap {
    display: flex;
    flex-direction: column;
}

.fg-bubble-row.right .fg-bubble-wrap { align-items: flex-end; }

.fg-speaker-name {
    font-size: 0.7rem;
    font-weight: 600;
    margin-bottom: 0.15rem;
    padding: 0 0.4rem;
}

.fg-bubble {
    padding: 0.55rem 0.85rem;
    border-radius: 18px;
    font-size: 0.85rem;
    line-height: 1.45;
    word-wrap: break-word;
    max-width: 420px;
}

.fg-bubble-row.left .fg-bubble {
    background: var(--card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    color: var(--text);
}

.fg-bubble-row.right .fg-bubble {
    border-bottom-right-radius: 4px;
}

/* Same-speaker continuation: hide avatar and name, tighten gap */
.fg-bubble-row.continuation { margin-top: -0.15rem; }
.fg-bubble-row.continuation .fg-avatar { visibility: hidden; }
.fg-bubble-row.continuation .fg-speaker-name { display: none; }

.fg-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 0.5rem;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.5rem;
}

.fg-header h3 {
    margin: 0;
    font-size: 0.95rem;
}

.fg-footer {
    padding: 0.5rem;
    font-size: 0.75rem;
    color: var(--text-muted);
    text-align: center;
    border-top: 1px solid var(--border);
    margin-top: 0.5rem;
}

/* Pulsing activity dot */
.pulse-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--forge);
    display: inline-block;
    animation: pulse-glow 1.5s ease-in-out infinite;
}

@keyframes pulse-glow {
    0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.6); }
    50% { opacity: 0.6; box-shadow: 0 0 8px 4px rgba(245, 158, 11, 0.3); }
}

/* Badge variants */
.badge-pending { background: var(--forge); color: #000; }
.badge-generating, .badge-sampling, .badge-importing { background: var(--info); color: #fff; }
.badge-failed { background: var(--danger); color: #fff; }
.badge-cancelled { background: var(--text-muted); color: #000; }
.badge-paused { background: var(--soul); color: #fff; }

/* =====================================================================
   TOAST NOTIFICATION SYSTEM
   ===================================================================== */

#ps-toast-container {
    position: fixed;
    top: 1rem;
    right: 1rem;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    max-width: 400px;
    pointer-events: none;
}

.ps-toast {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    padding: 0.75rem 1rem;
    border-radius: 8px;
    background: var(--card);
    border: 1px solid var(--border);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
    font-size: 0.85rem;
    line-height: 1.4;
    pointer-events: auto;
    transform: translateX(120%);
    opacity: 0;
    transition: transform 0.3s ease, opacity 0.3s ease;
}

.ps-toast--visible {
    transform: translateX(0);
    opacity: 1;
}

.ps-toast--exit {
    transform: translateX(120%);
    opacity: 0;
}

.ps-toast--success { border-left: 3px solid var(--data); }
.ps-toast--warning { border-left: 3px solid var(--forge); }
.ps-toast--error { border-left: 3px solid var(--danger); }
.ps-toast--info { border-left: 3px solid var(--info); }

.ps-toast--success .ps-toast__icon { color: var(--data); }
.ps-toast--warning .ps-toast__icon { color: var(--forge); }
.ps-toast--error .ps-toast__icon { color: var(--danger); }
.ps-toast--info .ps-toast__icon { color: var(--info); }

.ps-toast__icon {
    flex-shrink: 0;
    margin-top: 1px;
}

.ps-toast__message {
    flex: 1;
    color: var(--text);
}

.ps-toast__close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    flex-shrink: 0;
}

.ps-toast__close:hover {
    color: var(--text);
}

/* =====================================================================
   LOADING SPINNER (reusable)
   ===================================================================== */

.ps-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    gap: 0.75rem;
}

.ps-loading__spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--forge);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

.ps-loading__text {
    font-size: 0.85rem;
    color: var(--text-muted);
}

/* Button inline spinner */
.ps-btn-spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-top-color: currentColor;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
    vertical-align: middle;
}

/* =====================================================================
   EMPTY STATE
   ===================================================================== */

.ps-empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
    text-align: center;
    color: var(--text-muted);
}

.ps-empty-state__message {
    font-size: 0.9rem;
    margin-bottom: 1rem;
    max-width: 400px;
    line-height: 1.5;
}

.ps-empty-state__action {
    font-size: 0.85rem;
}

/* =====================================================================
   RETRY ERROR
   ===================================================================== */

.ps-retry-error {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 2rem;
    text-align: center;
}

.ps-retry-error__btn {
    margin-top: 0.5rem;
}

/* =====================================================================
   CONTEXTUAL HELP TOOLTIPS
   ===================================================================== */

.ps-help-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--border);
    color: var(--text-muted);
    font-size: 0.6rem;
    font-weight: 700;
    cursor: help;
    vertical-align: middle;
    margin-left: 4px;
    flex-shrink: 0;
    transition: background 0.15s, color 0.15s;
    line-height: 1;
    user-select: none;
}

.ps-help-icon:hover,
.ps-help-icon:focus {
    background: var(--panel);
    color: #fff;
    outline: none;
}

.ps-tooltip {
    position: fixed;
    z-index: 10001;
    max-width: 300px;
    padding: 0.6rem 0.85rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 0.8rem;
    line-height: 1.45;
    color: var(--text);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    opacity: 0;
    transform: translateY(4px);
    transition: opacity 0.2s, transform 0.2s;
    pointer-events: none;
}

.ps-tooltip--visible {
    opacity: 1;
    transform: translateY(0);
}

/* =====================================================================
   ONBOARDING TOUR
   ===================================================================== */

.ps-onboard-overlay {
    position: fixed;
    inset: 0;
    z-index: 9999;
    background: rgba(0, 0, 0, 0.75);
    opacity: 0;
    transition: opacity 0.4s ease;
}

.ps-onboard-overlay--visible {
    opacity: 1;
}

.ps-onboard-spotlight {
    position: fixed;
    border-radius: 8px;
    box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.75);
    border: 2px solid var(--forge);
    transition: all 0.4s ease;
    z-index: 9999;
    pointer-events: none;
}

.ps-onboard-card {
    position: fixed;
    z-index: 10000;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.5rem;
    width: 380px;
    max-width: calc(100vw - 40px);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.6);
    animation: ps-onboard-fadein 0.3s ease;
}

@keyframes ps-onboard-fadein {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.ps-onboard-card__header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.ps-onboard-card__step {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--forge);
    font-weight: 600;
}

.ps-onboard-card__skip {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    padding: 0;
}

.ps-onboard-card__skip:hover {
    color: var(--text);
}

.ps-onboard-card__title {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    color: var(--text);
}

.ps-onboard-card__body {
    font-size: 0.85rem;
    line-height: 1.6;
    color: var(--text-muted);
    margin-bottom: 1rem;
}

.ps-onboard-card__links {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

.ps-onboard-card__nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.ps-onboard-card__dots {
    display: flex;
    gap: 6px;
}

.ps-onboard-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--border);
    transition: background 0.2s;
}

.ps-onboard-dot--active {
    background: var(--forge);
}

.ps-onboard-card__buttons {
    display: flex;
    gap: 0.5rem;
}

/* =====================================================================
   HAMBURGER MENU (mobile sidebar toggle)
   ===================================================================== */

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 201;
    width: 40px;
    height: 40px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 4px;
    padding: 0;
}

.sidebar-toggle span {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--text);
    border-radius: 1px;
    transition: transform 0.2s, opacity 0.2s;
}

.sidebar-toggle.open span:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
}

.sidebar-toggle.open span:nth-child(2) {
    opacity: 0;
}

.sidebar-toggle.open span:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
}

/* Sidebar mobile overlay */
.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 199;
}

/* Table scroll wrapper for mobile */
.table-scroll-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* =====================================================================
   RESPONSIVE: TABLET (768px - 1024px)
   ===================================================================== */

@media (max-width: 1024px) {
    .sidebar {
        width: 200px;
    }

    .main {
        margin-left: 200px;
        padding: 1.5rem;
    }

    .response-viewer {
        width: 320px;
    }

    .card-grid {
        grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    }

    /* Two-column grids become single column */
    [style*="grid-template-columns:1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr"] {
        grid-template-columns: 1fr !important;
    }
}

/* =====================================================================
   RESPONSIVE: PHONE (< 768px)
   ===================================================================== */

@media (max-width: 768px) {
    /* Show hamburger menu */
    .sidebar-toggle {
        display: flex;
    }

    /* Sidebar: hidden by default, slides in on toggle */
    .sidebar {
        width: 260px;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 200;
    }

    .sidebar.mobile-open {
        transform: translateX(0);
    }

    .sidebar-backdrop.active {
        display: block;
    }

    /* Main content: full width */
    .main {
        margin-left: 0;
        padding: 1rem;
        padding-top: 3.5rem;
        max-width: 100%;
    }

    /* Response viewer: hidden on mobile */
    .response-viewer {
        display: none;
    }

    /* Card grid: single column */
    .card-grid {
        grid-template-columns: 1fr;
    }

    /* Stat cards: smaller on mobile */
    .stat-card .stat-value {
        font-size: 1.5rem;
    }

    /* Conversation layout: stack vertically */
    .split-layout {
        flex-direction: column;
        height: auto;
        min-height: calc(100vh - 4rem);
    }

    .split-left {
        min-height: 0;
    }

    .conversation-thread {
        min-height: 200px;
        max-height: 50vh;
    }

    /* Conversation input: stack vertically */
    .conversation-input { flex-direction: column;
        flex-direction: column;
        gap: 0.5rem;
    }

    .conversation-input textarea {
        min-height: 50px;
    }

    .conversation-input > div {
        flex-wrap: wrap;
        gap: 0.35rem;
    }

    /* Stimulus bubble: wider on mobile */
    .stimulus-bubble {
        max-width: 95%;
    }

    .response-card {
        max-width: 100%;
    }

    /* Focus group bubbles */
    .fg-bubble {
        max-width: 280px;
    }

    .fg-bubble-row {
        max-width: 95%;
    }

    /* Modals: full screen on mobile */
    .modal-overlay {
        align-items: stretch;
        padding: 0;
    }

    .modal {
        width: 100%;
        max-width: 100%;
        max-height: 100vh;
        border-radius: 0;
        height: 100vh;
    }

    /* Full-screen modals for inline style modals too */
    [style*="position:fixed"][style*="background:rgba"] > .card,
    [style*="position: fixed"][style*="background:rgba"] > .card {
        width: 100% !important;
        max-height: 100vh !important;
        border-radius: 0 !important;
    }

    /* Tables: allow horizontal scroll */
    table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
    }

    thead, tbody, tr, th, td {
        display: revert;
    }

    th, td {
        padding: 0.5rem 0.65rem;
        font-size: 0.8rem;
    }

    /* Forms: full width sliders and inputs */
    input[type="range"] {
        min-height: 44px;
    }

    input[type="text"],
    input[type="number"],
    textarea,
    select {
        font-size: 16px; /* Prevent iOS zoom on focus */
    }

    /* Card headers: stack when needed */
    .card-header {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    /* Headings */
    h2 {
        font-size: 1.25rem;
    }

    h3 {
        font-size: 1rem;
    }

    /* Two-column layouts */
    [style*="grid-template-columns"],
    [style*="display:grid"] {
        grid-template-columns: 1fr !important;
    }

    /* Flex layouts that should stack */
    .sandbox-grid {
        grid-template-columns: 1fr !important;
    }

    /* Toast notifications */
    #ps-toast-container {
        left: 0.5rem;
        right: 0.5rem;
        max-width: 100%;
    }

    /* Onboarding card */
    .ps-onboard-card {
        width: calc(100vw - 32px);
        left: 16px !important;
        right: 16px !important;
        transform: none !important;
    }

    /* Build page columns */
    [style*="grid-template-columns:1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="grid-template-columns:2fr 1fr"],
    [style*="grid-template-columns: 2fr 1fr"],
    [style*="grid-template-columns: 320px 1fr"],
    [style*="grid-template-columns:320px 1fr"] {
        grid-template-columns: 1fr !important;
    }

    /* Checkbox grid */
    .checkbox-grid {
        grid-template-columns: 1fr !important;
    }

    /* Bar chart labels */
    .bar-label {
        width: 70px;
        font-size: 0.7rem;
    }

    /* Buttons: larger touch targets on mobile */
    .btn {
        min-height: 38px;
        padding: 0.5rem 0.85rem;
    }
}

/* =====================================================================
   RESPONSIVE: SMALL PHONE (< 480px)
   ===================================================================== */

@media (max-width: 480px) {
    .main {
        padding: 0.75rem;
        padding-top: 3.5rem;
    }

    .card {
        padding: 0.85rem;
    }

    .card-grid {
        gap: 0.75rem;
    }

    .stat-card {
        padding: 1rem;
    }

    .stat-card .stat-value {
        font-size: 1.25rem;
    }

    .ps-onboard-card {
        padding: 1rem;
    }

    .ps-onboard-card__title {
        font-size: 1rem;
    }

    .ps-onboard-card__body {
        font-size: 0.8rem;
    }

    .ps-topbar {
        flex-direction: column;
        gap: 0.5rem;
    }

    .ps-search-wrap {
        max-width: 100%;
    }

    .ps-search-dropdown {
        max-width: 100%;
    }

    .ps-notif-dropdown {
        right: -60px;
        width: 300px;
    }
}

/* =====================================================================
   TOP BAR: SEARCH + NOTIFICATIONS
   ===================================================================== */

.ps-topbar {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-bottom: 1.25rem;
    position: relative;
}

.ps-search-wrap {
    flex: 1;
    max-width: 500px;
    position: relative;
}

.ps-search-form {
    display: flex;
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0 0.75rem;
    transition: border-color 0.15s;
}

.ps-search-form:focus-within {
    border-color: var(--panel);
}

.ps-search-icon {
    color: var(--text-muted);
    flex-shrink: 0;
}

.ps-search-form input[type="text"] {
    border: none;
    background: transparent;
    padding: 0.5rem 0.5rem;
    font-size: 0.85rem;
    color: var(--text);
    width: 100%;
    outline: none;
}

.ps-search-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 0 0 8px 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    max-height: 400px;
    overflow-y: auto;
}

.ps-search-cat {
    padding: 0.4rem 0.75rem 0.2rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    font-weight: 600;
    border-top: 1px solid var(--border);
}

.ps-search-cat:first-child {
    border-top: none;
}

.ps-search-item {
    display: block;
    padding: 0.45rem 0.75rem;
    font-size: 0.85rem;
    color: var(--text);
    text-decoration: none;
    cursor: pointer;
    transition: background 0.1s;
}

.ps-search-item:hover {
    background: var(--surface);
    text-decoration: none;
}

.ps-search-viewall {
    text-align: center;
    color: var(--panel);
    border-top: 1px solid var(--border);
    font-weight: 500;
}

/* ---- Notifications ---- */

.ps-topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.ps-notif-wrap {
    position: relative;
}

.ps-notif-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-muted);
    position: relative;
    transition: border-color 0.15s, color 0.15s;
}

.ps-notif-btn:hover {
    border-color: var(--panel);
    color: var(--text);
}

.ps-notif-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: var(--danger);
    color: #fff;
    font-size: 0.6rem;
    font-weight: 700;
    min-width: 16px;
    height: 16px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    line-height: 1;
}

.ps-notif-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    width: 360px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    margin-top: 4px;
    max-height: 460px;
    overflow-y: auto;
}

.ps-notif-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.9rem;
    font-weight: 600;
}

.ps-notif-mark-all {
    background: none;
    border: none;
    color: var(--panel);
    font-size: 0.75rem;
    cursor: pointer;
    padding: 0;
}

.ps-notif-mark-all:hover {
    text-decoration: underline;
}

.ps-notif-list {
    max-height: 380px;
    overflow-y: auto;
}

.ps-notif-item {
    padding: 0.65rem 1rem;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.1s;
}

.ps-notif-item:hover {
    background: var(--surface);
}

.ps-notif-item.read {
    opacity: 0.6;
}

.ps-notif-title {
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 0.15rem;
}

.ps-notif-msg {
    font-size: 0.8rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.ps-notif-time {
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 0.2rem;
}

/* =====================================================================
   ACCESSIBILITY (WCAG 2.1 AA)
   ===================================================================== */

/* Skip to main content link: hidden until focused */
.ps-skip-link {
    position: absolute;
    top: -100px;
    left: 0;
    background: var(--forge);
    color: #000;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    z-index: 100000;
    text-decoration: none;
    border-radius: 0 0 6px 0;
    transition: top 0.2s;
}

.ps-skip-link:focus {
    top: 0;
    outline: 3px solid var(--panel);
    outline-offset: 2px;
}

/* Focus indicators: visible outline on all interactive elements */
*:focus-visible {
    outline: 2px solid var(--panel);
    outline-offset: 2px;
}

/* Remove default outline on non-keyboard focus */
*:focus:not(:focus-visible) {
    outline: none;
}

/* Buttons: ensure focus visibility */
.btn:focus-visible {
    outline: 2px solid var(--forge);
    outline-offset: 2px;
    box-shadow: 0 0 0 4px rgba(245, 158, 11, 0.2);
}

/* Sidebar nav links: focus visibility */
.sidebar-nav li a:focus-visible {
    outline: 2px solid var(--forge);
    outline-offset: -2px;
    background: var(--card);
    color: var(--text);
}

/* Form inputs: focus ring */
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
    outline: 2px solid var(--panel);
    outline-offset: 1px;
    border-color: var(--panel);
}

/* Modal close button: visible focus */
.modal .btn:focus-visible,
.modal-overlay .btn:focus-visible {
    outline: 2px solid var(--forge);
    outline-offset: 2px;
}

/* =====================================================================
   REDUCED MOTION: disable animations when preference set
   ===================================================================== */

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }

    .loading::after,
    .ps-loading__spinner,
    .ps-btn-spinner {
        animation: none;
    }

    .pulse-dot {
        animation: none;
    }

    .ps-toast {
        transition: none;
    }
}

/* Class-based reduced motion (for manual toggle via settings) */
body.reduced-motion *,
body.reduced-motion *::before,
body.reduced-motion *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
}

body.reduced-motion .loading::after,
body.reduced-motion .ps-loading__spinner,
body.reduced-motion .ps-btn-spinner,
body.reduced-motion .pulse-dot {
    animation: none;
}

/* =====================================================================
   FOOTER (accessibility link + high contrast toggle)
   ===================================================================== */

.ps-footer {
    font-family: inherit;
}

.ps-footer a:hover {
    color: var(--text);
}

/* =====================================================================
   LANGUAGE SELECTOR
   ===================================================================== */

.ps-lang-btn:hover {
    border-color: var(--text-muted);
    color: var(--text);
}

.ps-lang-btn:focus-visible {
    outline: 2px solid var(--forge);
    outline-offset: 2px;
}

#ps-lang-dropdown.active {
    display: block !important;
}

#ps-lang-dropdown a:hover {
    text-decoration: none;
}

#ps-lang-dropdown a:focus-visible {
    outline: 2px solid var(--forge);
    outline-offset: -2px;
}
