/* ---------- Design tokens (Folk-inspired) ---------- */
:root {
    --sidebar-bg: #1a1f36;
    --sidebar-bg-hover: #232944;
    --sidebar-bg-active: #2d3555;
    --sidebar-text: #e4e6ed;
    --sidebar-text-dim: #9ba3b8;
    --sidebar-text-label: #6d748a;
    --sidebar-border: #2d3350;
    --sidebar-accent: #5b7bd6;

    --bg: #ffffff;
    --surface: #ffffff;
    --surface-alt: #f6f7f9;
    --surface-hover: #f0f2f5;
    --border: #e4e7ec;
    --border-strong: #d0d5dd;

    --text: #101828;
    --text-muted: #667085;
    --text-subtle: #98a2b3;

    --accent: #5b7bd6;
    --accent-hover: #4a6bc3;
    --accent-fg: #ffffff;

    --success: #12b76a;
    --warning: #f79009;
    --danger: #f04438;
    --info: #2e90fa;

    --radius-sm: 4px;
    --radius: 6px;
    --radius-lg: 10px;

    --shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.05);
    --shadow: 0 1px 3px rgba(16, 24, 40, 0.1), 0 1px 2px rgba(16, 24, 40, 0.06);
    --shadow-lg: 0 8px 16px rgba(16, 24, 40, 0.08), 0 2px 4px rgba(16, 24, 40, 0.06);

    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;

    --sidebar-width: 248px;
    --right-panel-width: 320px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1114;
        --surface: #14171c;
        --surface-alt: #1a1d23;
        --surface-hover: #22262d;
        --border: #2a2e37;
        --border-strong: #3a3f4b;
        --text: #eef0f4;
        --text-muted: #9aa2b1;
        --text-subtle: #6c7584;
    }
}

/* ---------- Reset / base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    font-family: var(--font);
    font-size: 14px;
    line-height: 1.5;
    color: var(--text);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0 0 0.5rem; color: var(--text); letter-spacing: -0.01em; }
h1 { font-size: 1.5rem; font-weight: 600; }
h2 { font-size: 1.15rem; font-weight: 600; }
h3 { font-size: 1rem; font-weight: 600; }
p { margin: 0 0 0.75rem; }
code, pre, .mono { font-family: var(--mono); font-size: 0.875em; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.skip-link { position: absolute; left: -9999px; }
.skip-link:focus {
    left: 1rem; top: 1rem;
    background: var(--accent); color: var(--accent-fg);
    padding: 0.5rem 1rem; z-index: 1000; border-radius: var(--radius);
}
.visually-hidden {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

/* ---------- Layout ---------- */
.layout {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    min-height: 100vh;
}
.layout:has(.right-panel:not(:empty)) {
    grid-template-columns: var(--sidebar-width) 1fr var(--right-panel-width);
}

.main {
    background: var(--bg);
    padding: 1.5rem 2rem;
    min-width: 0;
    overflow-x: hidden;
}

.right-panel {
    background: var(--surface-alt);
    border-left: 1px solid var(--border);
    padding: 1rem;
    overflow-y: auto;
}
.right-panel:empty { display: none; }

/* ---------- Sidebar (dark navy, Folk-style) ---------- */
.sidebar {
    background: var(--sidebar-bg);
    color: var(--sidebar-text);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--sidebar-border);
    overflow-y: auto;
    position: sticky;
    top: 0;
    height: 100vh;
}

.sidebar-top {
    padding: 12px 10px;
    border-bottom: 1px solid var(--sidebar-border);
    display: flex;
    align-items: center;
    gap: 8px;
}
.sidebar-toggle {
    display: none;
    background: transparent;
    border: 0;
    color: var(--sidebar-text);
    font-size: 1.2rem;
    padding: 4px 8px;
    cursor: pointer;
    border-radius: var(--radius);
}
.sidebar-toggle:hover { background: var(--sidebar-bg-hover); }

.brand {
    color: var(--sidebar-text);
    font-weight: 600;
    font-size: 1rem;
    padding: 6px 10px;
    text-decoration: none;
}

.ws-pill {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: var(--radius);
    color: var(--sidebar-text);
    text-decoration: none;
    flex: 1;
    min-width: 0;
    transition: background 0.12s;
}
.ws-pill:hover { background: var(--sidebar-bg-hover); text-decoration: none; color: var(--sidebar-text); }

.ws-avatar {
    width: 24px; height: 24px; border-radius: 6px;
    background: var(--sidebar-accent);
    color: #fff; display: grid; place-items: center;
    font-weight: 600; font-size: 12px;
    flex-shrink: 0;
}
.ws-name { font-weight: 600; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ws-caret { color: var(--sidebar-text-dim); font-size: 0.75rem; }

.sidebar-nav {
    padding: 8px 6px;
    flex: 1;
    overflow-y: auto;
}
.nav-group { display: flex; flex-direction: column; gap: 1px; margin-bottom: 8px; }
.nav-section-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--sidebar-text-label);
    padding: 12px 10px 4px;
    font-weight: 600;
}
.sidebar-nav a,
.sidebar-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border-radius: var(--radius);
    color: var(--sidebar-text);
    text-decoration: none;
    font-size: 0.875rem;
    transition: background 0.12s, color 0.12s;
}
.sidebar-nav a:hover,
.sidebar-item:hover { background: var(--sidebar-bg-hover); color: var(--sidebar-text); text-decoration: none; }
.sidebar-nav a.is-active,
.sidebar-nav a[aria-current="page"] {
    background: var(--sidebar-bg-active);
    color: #fff;
    font-weight: 500;
}
.sidebar-nav a .icon,
.sidebar-item .icon {
    width: 18px; text-align: center;
    font-size: 0.875rem;
    opacity: 0.85;
    flex-shrink: 0;
}

.sidebar-custom { padding: 8px 6px; border-top: 1px solid var(--sidebar-border); }

.sidebar-footer {
    border-top: 1px solid var(--sidebar-border);
    padding: 10px;
    font-size: 0.8rem;
}
.user-row { display: flex; align-items: center; gap: 8px; }
.user-avatar {
    width: 28px; height: 28px; border-radius: 50%;
    background: var(--sidebar-bg-active); color: #fff;
    display: grid; place-items: center; font-weight: 600; font-size: 12px;
    flex-shrink: 0;
}
.user-email {
    flex: 1; color: var(--sidebar-text-dim);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
    font-size: 0.75rem;
}
.user-row .linklike {
    background: transparent; border: 0; color: var(--sidebar-text-dim); cursor: pointer;
    font-size: 1rem; padding: 4px 6px; border-radius: var(--radius);
}
.user-row .linklike:hover { background: var(--sidebar-bg-hover); color: #fff; }
.user-row .inline { margin: 0; display: inline; }

/* ---------- Page header / tabs ---------- */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.25rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}
.page-title { font-size: 1.375rem; font-weight: 600; margin: 0; }
.page-actions { display: flex; gap: 0.5rem; align-items: center; }

.tabs {
    display: flex;
    gap: 0;
    border-bottom: 1px solid var(--border);
    margin: -0.5rem 0 1.25rem;
    overflow-x: auto;
}
.tabs a, .tab {
    padding: 0.5rem 0.875rem;
    color: var(--text-muted);
    border-bottom: 2px solid transparent;
    font-size: 0.875rem;
    font-weight: 500;
    text-decoration: none;
    white-space: nowrap;
}
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.is-active,
.tab.is-active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

/* ---------- Forms (aligned grid) ---------- */
.form-grid {
    display: grid;
    grid-template-columns: 180px 1fr;
    gap: 12px 16px;
    align-items: center;
    max-width: 640px;
}
.form-grid > label {
    font-size: 0.875rem;
    color: var(--text-muted);
    font-weight: 500;
}
.form-grid > input,
.form-grid > select,
.form-grid > textarea { width: 100%; }
.form-grid .form-actions {
    grid-column: 2;
    margin-top: 0.5rem;
    display: flex;
    gap: 0.5rem;
}
.form-grid .form-help {
    grid-column: 2;
    font-size: 0.8rem;
    color: var(--text-subtle);
    margin-top: -6px;
}
.form-grid .full-row { grid-column: 1 / -1; }

.form-page {
    max-width: 680px;
    background: var(--surface);
    padding: 1.5rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
}

input[type="text"], input[type="email"], input[type="password"], input[type="url"],
input[type="tel"], input[type="number"], input[type="date"], input[type="datetime-local"],
input[type="search"], input[type="time"], select, textarea {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius);
    padding: 7px 10px;
    width: 100%;
    transition: border-color 0.12s, box-shadow 0.12s;
}
input:focus, select:focus, textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in oklch, var(--accent) 20%, transparent);
    outline: none;
}
textarea { min-height: 80px; font-family: inherit; resize: vertical; }
input[type="checkbox"], input[type="radio"] { width: auto; }

label { font-size: 0.875rem; font-weight: 500; color: var(--text); }

/* ---------- Buttons ---------- */
button, .btn, input[type="submit"] {
    font: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    padding: 7px 14px;
    border-radius: var(--radius);
    border: 1px solid var(--border-strong);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    transition: background 0.12s, border-color 0.12s;
}
button:hover, .btn:hover { background: var(--surface-hover); text-decoration: none; }
button:disabled { opacity: 0.6; cursor: not-allowed; }

button.primary, .btn.primary, input[type="submit"] {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-fg);
}
button.primary:hover, .btn.primary:hover, input[type="submit"]:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}
button.danger, .btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.ghost, .btn.ghost { background: transparent; border-color: transparent; }
button.ghost:hover, .btn.ghost:hover { background: var(--surface-hover); }
button.linklike { background: transparent; border: 0; padding: 0; color: var(--accent); }
button.linklike:hover { background: transparent; text-decoration: underline; }

/* ---------- Tables ---------- */
.data-table, table.data {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow: hidden;
}
.data-table th, table.data th {
    text-align: left;
    padding: 10px 14px;
    font-size: 0.75rem;
    font-weight: 500;
    color: var(--text-muted);
    background: var(--surface-alt);
    border-bottom: 1px solid var(--border);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.data-table td, table.data td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.data-table tbody tr:last-child td,
table.data tbody tr:last-child td { border-bottom: 0; }
.data-table tbody tr:hover,
table.data tbody tr:hover { background: var(--surface-alt); }

/* ---------- Pills / chips ---------- */
.pill, .chip, .badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    font-size: 0.75rem;
    font-weight: 500;
    border-radius: 999px;
    background: var(--surface-alt);
    color: var(--text);
    border: 1px solid var(--border);
    white-space: nowrap;
}
.pill.success, .chip.success { background: #e7f8ee; color: #027a48; border-color: #a6f4c5; }
.pill.warning, .chip.warning { background: #fef4e2; color: #b54708; border-color: #fedf89; }
.pill.danger, .chip.danger { background: #fee4e2; color: #b42318; border-color: #fda29b; }
.pill.info, .chip.info { background: #e7f0ff; color: #0850c4; border-color: #b2ccff; }
.pill.neutral, .chip.neutral { background: #f2f4f7; color: #344054; border-color: #e4e7ec; }

/* ---------- Cards ---------- */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1rem 1.25rem;
    margin-bottom: 1rem;
}
.card h2, .card h3 { margin-top: 0; }

.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-muted);
    border: 1px dashed var(--border);
    border-radius: var(--radius-lg);
    background: var(--surface-alt);
}

/* ---------- Filter bar ---------- */
.filter-bar {
    display: flex;
    gap: 8px;
    align-items: center;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.filter-bar input[type="search"],
.filter-bar input[type="text"] { max-width: 320px; }

/* ---------- Kanban ---------- */
.kanban {
    display: flex;
    gap: 1rem;
    overflow-x: auto;
    padding-bottom: 1rem;
}
.kanban-col {
    min-width: 280px;
    background: var(--surface-alt);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 10px;
    flex-shrink: 0;
}
.kanban-col h3 {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-muted);
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
}
.kanban-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 10px;
    margin-bottom: 8px;
    box-shadow: var(--shadow-sm);
    cursor: grab;
}
.kanban-card:hover { border-color: var(--border-strong); }
.kanban-card.dragging { opacity: 0.5; }
.kanban-card.picked { outline: 2px dashed var(--accent); }
.kanban-col.drop-target { border-color: var(--accent); background: color-mix(in oklch, var(--accent) 10%, var(--surface-alt)); }

/* ---------- Thread list ---------- */
.thread-list { list-style: none; padding: 0; margin: 0; border: 1px solid var(--border); border-radius: var(--radius-lg); background: var(--surface); overflow: hidden; }
.thread-list li { padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer; display: flex; gap: 12px; align-items: start; }
.thread-list li:last-child { border-bottom: 0; }
.thread-list li:hover { background: var(--surface-alt); }
.thread-list li.unread { background: #f0f5ff; }
.thread-list li.is-selected { background: #e7f0ff; border-left: 3px solid var(--accent); padding-left: 13px; }
.thread-preview { color: var(--text-muted); font-size: 0.8rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thread-meta { font-size: 0.75rem; color: var(--text-subtle); }

/* ---------- Auth pages ---------- */
.auth-layout {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 2rem;
    background: linear-gradient(135deg, #f6f7f9, #eef1f5);
}
.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    padding: 2rem;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
}
.auth-card h1 { text-align: center; margin-bottom: 1.5rem; }
.auth-card .form-grid {
    grid-template-columns: 1fr;
    gap: 0.5rem;
    align-items: stretch;
}
.auth-card .form-grid > label { margin-top: 0.5rem; }
.auth-card .form-actions { grid-column: 1; }
.auth-card .form-actions button { flex: 1; justify-content: center; }

/* ---------- Dialog / palette ---------- */
dialog { border: 0; border-radius: var(--radius-lg); padding: 0; box-shadow: var(--shadow-lg); max-width: 90vw; }
dialog::backdrop { background: rgba(16, 24, 40, 0.45); }
.palette-dialog { width: 600px; }
.palette-dialog input { border: 0; border-bottom: 1px solid var(--border); border-radius: 0; padding: 14px 16px; font-size: 1rem; }
.palette-dialog input:focus { box-shadow: none; border-bottom-color: var(--accent); }
.palette-results { max-height: 360px; overflow-y: auto; }
.palette-result { padding: 10px 16px; cursor: pointer; }
.palette-result:hover, .palette-result.is-selected { background: var(--surface-alt); }

/* ---------- Widgets ---------- */
.widget-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1rem;
}
.widget {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1rem;
}
.widget .widget-title { font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 0.5rem; }
.widget .widget-value { font-size: 1.75rem; font-weight: 600; color: var(--text); }

/* ---------- Sequence step builder ---------- */
.step-row {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px;
    margin-bottom: 10px;
    display: grid;
    grid-template-columns: 28px 1fr auto;
    gap: 10px;
    align-items: center;
}
.step-number { width: 28px; height: 28px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 600; font-size: 0.8rem; }

/* ---------- Utility ---------- */
.flex { display: flex; gap: 0.5rem; }
.flex-between { display: flex; justify-content: space-between; align-items: center; }
.flex-col { display: flex; flex-direction: column; gap: 0.5rem; }
.stack { display: flex; flex-direction: column; gap: 1rem; }
.inline { display: inline; margin: 0; }
.muted { color: var(--text-muted); }
.subtle { color: var(--text-subtle); }
.small { font-size: 0.8rem; }
.mt-1 { margin-top: 0.5rem; } .mt-2 { margin-top: 1rem; } .mt-3 { margin-top: 1.5rem; }
.mb-1 { margin-bottom: 0.5rem; } .mb-2 { margin-bottom: 1rem; }

/* ---------- Accessibility ---------- */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
    .layout { grid-template-columns: 1fr !important; }
    .sidebar {
        position: fixed; top: 0; left: 0; bottom: 0; z-index: 100;
        width: min(280px, 85vw);
        transform: translateX(-100%);
        transition: transform 0.2s;
    }
    .sidebar.is-open { transform: translateX(0); box-shadow: 4px 0 16px rgba(0,0,0,0.2); }
    .sidebar-toggle { display: inline-flex; }
    .right-panel { display: none; }
    .main { padding: 1rem; }
    .form-grid { grid-template-columns: 1fr; }
    .form-grid > label { margin-top: 0.75rem; }
    .form-grid .form-actions { grid-column: 1; }
    .page-header { flex-direction: column; align-items: flex-start; }
    body.sidebar-open { overflow: hidden; }
}
