/* ── Reset / base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --color-bg:       #f5f7fa;
    --color-surface:  #ffffff;
    --color-surface-2:#eef2f7;
    --color-border:   #dde1e7;
    --color-border-2: #c7cedb;
    --color-text:     #1a1d23;
    --color-muted:    #59606c;
    --color-primary:  #2563eb;
    --color-primary-h:#1d4ed8;
    --color-primary-bg:#eff5ff;
    --color-danger:   #dc2626;
    --color-danger-h: #b91c1c;
    --color-danger-bg:#fef2f2;
    --color-danger-border: #fecaca;
    --color-success:  #16a34a;
    --color-success-bg:#f0fdf4;
    --color-success-border: #bbf7d0;
    --color-warning:  #b45309;
    --color-warning-bg:#fffbeb;
    --color-warning-border: #fde68a;
    --color-info:      #1e40af;
    --color-info-bg:   #eff5ff;
    --color-info-border: #bfdbfe;
    --radius:         6px;
    --radius-sm:      4px;
    --shadow:         0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
    --shadow-lg:      0 16px 40px rgba(16,24,40,.18);

    /* Spacing scale */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;

    --sidebar-w: 236px;
    --focus-ring: 0 0 0 3px rgba(37,99,235,.35);
}

/* ── Dark theme (automatic — follows the OS/browser preference) ─────────────
   Same token names, re-mapped to a dark slate palette. Values were chosen
   and contrast-checked (WCAG) against both the page background and the
   surface color, not just picked by eye. */
@media (prefers-color-scheme: dark) {
    :root {
        --color-bg:        #0d131e;
        --color-surface:   #141c2b;
        --color-surface-2: #1b2536;
        --color-border:    #2a3648;
        --color-border-2:  #3b4a5f;
        --color-text:      #e8edf6;
        --color-muted:     #96a3b8;
        --color-primary:   #316fef;
        --color-primary-h: #5b93f5;
        --color-primary-bg:#16233a;
        --color-danger:    #e2413a;
        --color-danger-h:  #ef5350;
        --color-danger-bg: #2a1616;
        --color-danger-border: #5c2a2a;
        --color-success:   #2fbd63;
        --color-success-bg:#122a1f;
        --color-success-border: #1f5238;
        --color-warning:   #d97706;
        --color-warning-bg:#2b230f;
        --color-warning-border: #5a4a1f;
        --color-info:      #9dbdf7;
        --color-info-bg:   #16233a;
        --color-info-border: #2c4a7c;
        --shadow:    0 1px 3px rgba(0,0,0,.5), 0 1px 2px rgba(0,0,0,.4);
        --shadow-lg: 0 20px 50px rgba(0,0,0,.6);
        --focus-ring: 0 0 0 3px rgba(91,147,245,.45);
    }
}

html { -webkit-text-size-adjust: 100%; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    color: var(--color-text);
    background: var(--color-bg);
}

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

/* ── Focus visibility (WCAG 2.4.7) ───────────────────────────────────────── */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
    border-radius: var(--radius-sm);
}

/* ── App shell: sidebar + main (mobile-first) ────────────────────────────── */
.app-shell {
    min-height: 100vh;
}

.sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(78vw, var(--sidebar-w));
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    display: flex;
    flex-direction: column;
    z-index: 260;
    transform: translateX(-100%);
    transition: transform .2s ease;
    overflow-y: auto;
}
.sidebar.open { transform: translateX(0); box-shadow: var(--shadow-lg); }

.sidebar-brand {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: 1.1rem 1.25rem;
    border-bottom: 1px solid var(--color-border);
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--color-text);
}
.sidebar-brand .mark {
    width: 30px; height: 30px;
    border-radius: 8px;
    background: linear-gradient(150deg, var(--color-primary), var(--color-primary-h));
    color: #fff;
    display: grid;
    place-items: center;
    font-size: .8rem;
    font-weight: 800;
    flex: none;
}

.side-nav {
    list-style: none;
    padding: .75rem;
    display: flex;
    flex-direction: column;
    gap: .15rem;
    flex: 1;
}
.side-link {
    display: flex;
    align-items: center;
    gap: .65rem;
    padding: .55rem .75rem;
    border-radius: var(--radius);
    color: var(--color-muted);
    font-size: .92rem;
    font-weight: 500;
}
.side-link:hover { background: var(--color-bg); color: var(--color-text); text-decoration: none; }
.side-link.active { background: var(--color-primary-bg); color: var(--color-primary-h); font-weight: 700; }
.side-link .ic { width: 1.1rem; text-align: center; flex: none; }

.sidebar-foot {
    padding: .85rem 1.25rem;
    border-top: 1px solid var(--color-border);
    font-size: .78rem;
    color: var(--color-muted);
}

.drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15,20,30,.45);
    z-index: 250;
}
.drawer-overlay[hidden] { display: none; }

/* ── Topbar ──────────────────────────────────────────────────────────────── */
.app-main { min-width: 0; }

.topbar {
    display: flex;
    align-items: center;
    gap: .75rem;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    padding: .65rem 1rem;
    position: sticky;
    top: 0;
    z-index: 100;
}

.drawer-toggle {
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    width: 34px; height: 34px;
    font-size: 1rem;
    cursor: pointer;
    color: var(--color-text);
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.drawer-toggle:hover { background: var(--color-bg); }

/* ── Breadcrumb ──────────────────────────────────────────────────────────── */
.breadcrumb {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: .35rem;
    font-size: .85rem;
    color: var(--color-muted);
    min-width: 0;
    flex: 1;
}
.breadcrumb a { color: var(--color-muted); }
.breadcrumb a:hover { color: var(--color-primary); text-decoration: none; }
.breadcrumb .sep { opacity: .55; }
.breadcrumb .current { color: var(--color-text); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── User dropdown ───────────────────────────────────────────────────────── */
.user-menu { position: relative; margin-left: auto; flex: none; }

.user-toggle {
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: .3rem .75rem;
    cursor: pointer;
    font-size: .88rem;
    color: var(--color-text);
    display: flex;
    align-items: center;
    gap: .4rem;
    max-width: 40vw;
}
.user-toggle span.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-toggle:hover { background: var(--color-bg); }

.user-menu .dropdown {
    display: none;
    position: absolute;
    right: 0;
    top: calc(100% + 4px);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    list-style: none;
    min-width: 200px;
    z-index: 200;
}
.user-menu.open .dropdown { display: block; }
.dropdown li a,
.dropdown li button {
    display: block;
    width: 100%;
    padding: .5rem 1rem;
    text-align: left;
    background: none;
    border: none;
    cursor: pointer;
    font-size: .9rem;
    color: var(--color-text);
}
.dropdown li a:hover,
.dropdown li button:hover { background: var(--color-bg); text-decoration: none; }

.inline-form { display: contents; }

/* ── Layout ──────────────────────────────────────────────────────────────── */
.container {
    max-width: 960px;
    margin: 0 auto;
    padding: var(--space-5) var(--space-4) var(--space-6);
}

.page-header {
    margin-bottom: var(--space-5);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    flex-wrap: wrap;
}
.page-header h1 { font-size: 1.5rem; font-weight: 700; text-wrap: balance; }
.page-header .subtitle { color: var(--color-muted); margin-top: .25rem; }
.page-header .head-text { min-width: 0; }

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.section-header h2 { font-size: 1.1rem; font-weight: 600; }

.form-page { max-width: 640px; }
.form-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: var(--space-5);
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: .4rem;
    padding: .5rem 1.1rem;
    border-radius: var(--radius);
    font-size: .9rem;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background .15s, border-color .15s, color .15s;
    text-decoration: none;
    line-height: 1.2;
}
.btn:hover { text-decoration: none; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary  { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-h); }
.btn-secondary { background: var(--color-surface); color: var(--color-text); border-color: var(--color-border); }
.btn-secondary:hover { background: var(--color-bg); border-color: var(--color-border-2); }
.btn-ghost { background: none; color: var(--color-muted); border-color: transparent; }
.btn-ghost:hover { background: var(--color-surface-2); color: var(--color-text); }
.btn-danger   { background: var(--color-danger); color: #fff; }
.btn-danger:hover { background: var(--color-danger-h); }

.btn-sm {
    padding: .3rem .7rem;
    font-size: .82rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text);
    font-weight: 600;
    display: inline-flex;
    align-items: center;
    gap: .3rem;
}
.btn-sm:hover { background: var(--color-bg); text-decoration: none; }
.btn-sm.btn-edit { border-color: var(--color-primary); color: var(--color-primary-h); }
.btn-sm.btn-danger { border-color: var(--color-danger); color: var(--color-danger); background: none; }

.btn-icon {
    background: none;
    border: none;
    cursor: pointer;
    padding: .2rem .4rem;
    font-size: 1rem;
    color: var(--color-muted);
    border-radius: var(--radius-sm);
}
.btn-icon:hover { color: var(--color-text); }

/* ── Badges ──────────────────────────────────────────────────────────────── */
.badge {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    font-size: .76rem;
    font-weight: 700;
    letter-spacing: .02em;
    padding: .18rem .55rem;
    border-radius: 999px;
    border: 1px solid;
    white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.badge-ok      { background: var(--color-success-bg); color: var(--color-success); border-color: var(--color-success-border); }
.badge-warn    { background: var(--color-warning-bg); color: var(--color-warning); border-color: var(--color-warning-border); }
.badge-crit    { background: var(--color-danger-bg);  color: var(--color-danger);  border-color: var(--color-danger-border); }
.badge-neutral { background: var(--color-surface-2);  color: var(--color-muted);   border-color: var(--color-border); }

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.table {
    width: 100%;
    min-width: 560px;
    border-collapse: collapse;
    background: var(--color-surface);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: var(--shadow);
}
.table th, .table td {
    padding: .65rem 1rem;
    text-align: left;
    border-bottom: 1px solid var(--color-border);
    font-size: .9rem;
}
.table th { background: var(--color-bg); font-weight: 600; color: var(--color-muted); font-size: .82rem; text-transform: uppercase; letter-spacing: .04em; }
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover td { background: var(--color-primary-bg); }
.url-cell { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.masked-key { letter-spacing: .1em; color: var(--color-muted); }
.actions { white-space: nowrap; }
.actions-inner { display: inline-flex; gap: .4rem; vertical-align: middle; }

.table--guide { min-width: 0; }
.table--guide th { width: 160px; vertical-align: top; }

/* Card-collapse for data-dense tables on narrow screens (mobile-first: this
   is the base layout; the real <table> layout is restored at 641px+). */
.table--responsive { min-width: 0; box-shadow: none; background: none; }
.table--responsive thead { display: none; }
.table--responsive, .table--responsive tbody, .table--responsive tr, .table--responsive td { display: block; width: 100%; }
.table--responsive tr {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: var(--space-3);
    padding: .3rem var(--space-3);
}
.table--responsive tr:last-child { margin-bottom: 0; }
.table--responsive td {
    border-bottom: 1px solid var(--color-border);
    padding: .55rem 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    text-align: right;
}
.table--responsive td:last-child { border-bottom: none; }
.table--responsive td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--color-muted);
    text-align: left;
    font-size: .74rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    flex: none;
}
.table--responsive .url-cell {
    max-width: 60%;
    overflow: visible;
    white-space: normal;
    word-break: break-all;
    text-align: right;
}
.table--responsive td.actions { justify-content: flex-end; }
.table--responsive td.actions::before { content: none; }

@media (min-width: 641px) {
    .table--responsive { box-shadow: var(--shadow); background: var(--color-surface); }
    .table--responsive thead { display: table-header-group; }
    .table--responsive, .table--responsive tbody { display: table; width: 100%; }
    .table--responsive tr { display: table-row; border: none; box-shadow: none; border-radius: 0; margin: 0; padding: 0; }
    .table--responsive td {
        display: table-cell;
        border-bottom: 1px solid var(--color-border);
        padding: .65rem 1rem;
        text-align: left;
    }
    .table--responsive td::before { content: none; }
    .table--responsive tr:last-child td { border-bottom: none; }
    .table--responsive .url-cell {
        max-width: 280px;
        overflow: hidden;
        white-space: nowrap;
        word-break: normal;
        text-align: left;
    }
}

/* ── Stats dashboard ─────────────────────────────────────────────────────── */
.stats-grid { display: grid; grid-template-columns: 1fr; gap: 1rem; margin-bottom: 2rem; }
.stat-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 1.1rem 1.4rem;
    display: flex;
    flex-direction: column;
    gap: .25rem;
    box-shadow: var(--shadow);
}
.stat-number { font-size: 2rem; font-weight: 700; color: var(--color-primary); font-variant-numeric: tabular-nums; }
.stat-label  { font-size: .85rem; color: var(--color-muted); }
.stat-url    { font-size: .82rem; word-break: break-all; }

@media (min-width: 560px) {
    .stats-grid { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
}

/* ── Empty state ─────────────────────────────────────────────────────────── */
.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--color-muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    background: var(--color-surface);
    border: 1px dashed var(--color-border);
    border-radius: var(--radius);
}

/* ── Profiles summary ────────────────────────────────────────────────────── */
.profiles-summary { margin-top: 2rem; }

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
}
.modal[hidden] { display: none; }

.modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,.45);
}
.modal-box {
    position: relative;
    background: var(--color-surface);
    border-radius: var(--radius);
    padding: var(--space-5);
    width: 100%;
    max-width: 520px;
    box-shadow: var(--shadow-lg);
    z-index: 1;
    max-height: 90vh;
    overflow-y: auto;
}
.modal-box--sm { max-width: 380px; }
.modal-box h2 { font-size: 1.15rem; font-weight: 700; margin-bottom: 1.25rem; }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.form-group { display: flex; flex-direction: column; gap: .35rem; margin-bottom: 1rem; }
.form-group label { font-size: .88rem; font-weight: 600; }
.form-group input[type="text"],
.form-group input[type="email"],
.form-group input[type="number"],
.form-group input[type="date"],
.form-group input[type="url"],
.form-group input[type="password"],
.form-group select {
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: .55rem .75rem;
    font-size: .9rem;
    width: 100%;
    color: var(--color-text);
    background: var(--color-surface);
    transition: border-color .15s, box-shadow .15s;
}
.form-group input:focus, .form-group select:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: var(--focus-ring);
}
.input-group { display: flex; gap: .4rem; }
.input-group input, .input-group select { flex: 1; }
.hint { color: var(--color-muted); font-size: .82rem; font-weight: 400; }

.form-error {
    background: var(--color-danger-bg);
    border: 1px solid var(--color-danger-border);
    border-radius: var(--radius);
    padding: .6rem .8rem;
    font-size: .88rem;
    color: var(--color-danger);
    margin-bottom: .75rem;
}
.form-actions { display: flex; gap: .75rem; justify-content: flex-end; margin-top: 1.25rem; flex-wrap: wrap; }

/* ── API key display ─────────────────────────────────────────────────────── */
.api-key-display {
    display: flex;
    align-items: center;
    gap: .5rem;
    background: var(--color-bg);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: .5rem .75rem;
    margin: .75rem 0 1.25rem;
    word-break: break-all;
}
.api-key-display code { font-family: 'SFMono-Regular', Consolas, monospace; font-size: .85rem; flex: 1; }

/* ── Guide ───────────────────────────────────────────────────────────────── */
.guide-section { margin-bottom: 2.5rem; }
.guide-section h2 { font-size: 1.1rem; font-weight: 600; margin-bottom: .75rem; border-bottom: 1px solid var(--color-border); padding-bottom: .4rem; }
.steps { padding-left: 1.5rem; display: flex; flex-direction: column; gap: .6rem; }
.steps li { font-size: .92rem; }

/* Intentionally a fixed dark "terminal" look in both themes, like a code
   block on GitHub — it does not follow --color-surface/--color-text. */
.code-block {
    background: #1e293b;
    color: #e2e8f0;
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    overflow-x: auto;
    font-size: .85rem;
    line-height: 1.6;
    margin: .75rem 0;
}
.code-block code { font-family: 'SFMono-Regular', Consolas, monospace; }

.info-box {
    background: var(--color-info-bg);
    border: 1px solid var(--color-info-border);
    border-radius: var(--radius);
    padding: .75rem 1rem;
    font-size: .88rem;
    color: var(--color-info);
    margin-top: .75rem;
}

/* ── Toasts ──────────────────────────────────────────────────────────────── */
.toast-container {
    position: fixed;
    bottom: var(--space-4);
    right: var(--space-4);
    left: var(--space-4);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: .5rem;
    z-index: 500;
    pointer-events: none;
}
.toast {
    pointer-events: auto;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-left: 4px solid var(--color-primary);
    box-shadow: var(--shadow-lg);
    border-radius: var(--radius);
    padding: .7rem 1rem;
    font-size: .88rem;
    max-width: min(360px, 100%);
    display: flex;
    align-items: flex-start;
    gap: .6rem;
    animation: toast-in .18s ease-out;
}
.toast.success { border-left-color: var(--color-success); }
.toast.error   { border-left-color: var(--color-danger); }
.toast .toast-close { margin-left: auto; background: none; border: none; cursor: pointer; color: var(--color-muted); font-size: .95rem; line-height: 1; flex: none; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@media (prefers-reduced-motion: reduce) { .toast { animation: none; } }

/* ── Error page ──────────────────────────────────────────────────────────── */
.error-page {
    text-align: center;
    padding-top: 4rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}
.error-page h1 { font-size: 4.5rem; font-weight: 800; color: var(--color-border); }
.error-page p  { color: var(--color-muted); }

/* ── Desktop layout: permanent sidebar ───────────────────────────────────── */
@media (min-width: 768px) {
    .app-shell { display: flex; }
    .sidebar {
        position: sticky;
        top: 0;
        height: 100vh;
        transform: none;
        flex: none;
        box-shadow: none;
    }
    .drawer-overlay { display: none; }
    .drawer-toggle { display: none; }
    .app-main { flex: 1; min-width: 0; }
    .container { padding: var(--space-6) var(--space-5) var(--space-6); }
}
