:root {
    --bg: #f3f5f9;
    --surface: #ffffff;
    --text: #1f2937;
    --text-strong: #111827;
    --muted: #6b7280;
    --accent: #0f766e;
    --accent-hover: #0e6660;
    --danger: #b91c1c;
    --danger-hover: #991b1b;
    --border: #d1d5db;
    --header-bg: #0f172a;
    --header-panel: #1e293b;
    --header-active: #0f766e;
    --warning-bg: #fef3c7;
    --ok-bg: #dcfce7;
    --error-bg: #fee2e2;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    color: var(--text);
    background: radial-gradient(circle at top, #eaf8f8 0%, var(--bg) 55%);
}

.header {
    background: var(--header-bg);
    color: #fff;
    border-bottom: 3px solid var(--header-active);
}

.header-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 16px 16px 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
}

.brand-block {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.brand {
    color: #fff;
    text-decoration: none;
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: .4px;
}

.brand-subtitle {
    color: #a5b4fc;
    font-size: .83rem;
}

.session-meta {
    color: #cbd5e1;
    font-size: .9rem;
}

.session-meta strong {
    color: #f8fafc;
    font-weight: 700;
}

.menu-wrap {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 16px 14px;
    display: flex;
    align-items: stretch;
    gap: 12px;
}

.menu {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    align-items: stretch;
    gap: 12px;
    flex: 1;
}

.menu a {
    display: flex;
    flex-direction: column;
    gap: 3px;
    color: #e2e8f0;
    text-decoration: none;
    border: 1px solid #334155;
    background: linear-gradient(180deg, #1e293b 0%, #172033 100%);
    border-radius: 10px;
    padding: 10px 12px;
    transition: background-color .15s ease, border-color .15s ease, transform .15s ease;
}

.menu a:hover {
    border-color: #64748b;
    background: linear-gradient(180deg, #263246 0%, #1a2538 100%);
    transform: translateY(-1px);
}

.menu a.active {
    border-color: #2dd4bf;
    background: linear-gradient(180deg, #12433f 0%, #153b39 100%);
}

.menu-title {
    font-weight: 600;
    color: #f8fafc;
}

.menu-subtitle {
    font-size: .78rem;
    color: #9fb4cc;
}

.menu-logout {
    align-items: center;
    justify-content: center;
    min-width: 120px;
}

.link-btn {
    margin: 0;
    color: #f8fafc;
    text-decoration: none;
    background: #7f1d1d;
    border: 1px solid #991b1b;
    border-radius: 10px;
    padding: 10px 14px;
    cursor: pointer;
    font-size: .9rem;
    transition: background-color .15s ease;
}

.link-btn:hover {
    background: #991b1b;
}

.main {
    max-width: 1200px;
    margin: 24px auto;
    padding: 0 16px;
}

.panel {
    background: var(--surface);
    border-radius: 14px;
    border: 1px solid var(--border);
    padding: 20px;
    box-shadow: 0 8px 32px rgba(15, 23, 42, 0.05);
    overflow-x: auto;
}

.panel.narrow {
    max-width: 460px;
    margin: 0 auto;
}

label {
    display: block;
    font-weight: 600;
    margin-top: 12px;
    margin-bottom: 6px;
}

input,
textarea,
select,
button {
    font: inherit;
}

input,
textarea,
select {
    width: 100%;
    padding: 10px 12px;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: #fff;
}

textarea { resize: vertical; }

button {
    margin-top: 14px;
    background: var(--accent);
    color: white;
    border: 0;
    border-radius: 8px;
    padding: 10px 14px;
    cursor: pointer;
}

button:hover { background: var(--accent-hover); }

button.danger { background: var(--danger); }
button.danger:hover { background: var(--danger-hover); }
button.secondary {
    background: #e2e8f0;
    color: #0f172a;
}
button.secondary:hover {
    background: #cbd5e1;
}

.inline {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.inline button {
    margin-top: 0;
}

.inline-form {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 10px;
    align-items: end;
    margin-bottom: 16px;
}

.api-key-create-form {
    grid-template-columns: minmax(260px, 1fr) auto;
}

.api-key-create-form .field {
    min-width: 0;
}

.api-key-create-form label {
    margin-top: 0;
}

.api-key-create-form button {
    margin-top: 0;
    white-space: nowrap;
}

.helper {
    margin-top: 6px;
    font-size: .88rem;
    color: var(--muted);
}

.flash {
    border-radius: 8px;
    padding: 10px 12px;
    margin-bottom: 12px;
    border: 1px solid var(--border);
}

.flash-info { background: #e0f2fe; }
.flash-success { background: var(--ok-bg); }
.flash-warning { background: var(--warning-bg); }
.flash-error { background: var(--error-bg); }

.muted {
    color: var(--muted);
    font-size: .85rem;
}

code {
    word-break: break-all;
    font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: .85rem;
}

.table-tools {
    margin-top: 10px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    align-items: end;
    justify-content: space-between;
}

.table-tool {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin: 0;
    min-width: 180px;
}

.table-tool input,
.table-tool select {
    min-height: 40px;
}

.table-tool-compact {
    min-width: 110px;
}

.table-stats {
    color: var(--muted);
    font-size: .86rem;
    margin-left: auto;
}

.table-pagination {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.table-pagination button {
    margin-top: 0;
    min-width: 70px;
}

.table-page-indicator {
    min-width: 92px;
    text-align: center;
    color: var(--muted);
    font-size: .85rem;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 14px;
    font-size: .9rem;
    min-width: 720px;
}

th,
td {
    border-bottom: 1px solid #e5e7eb;
    text-align: left;
    padding: 8px;
    vertical-align: top;
}

th {
    font-size: .8rem;
    text-transform: uppercase;
    letter-spacing: .04em;
    color: var(--muted);
    white-space: nowrap;
}

th.sortable {
    cursor: pointer;
    position: relative;
    padding-right: 20px;
}

th.sortable::after {
    content: "↕";
    position: absolute;
    right: 6px;
    color: #94a3b8;
}

th.sortable[data-sort="asc"]::after {
    content: "↑";
    color: var(--accent);
}

th.sortable[data-sort="desc"]::after {
    content: "↓";
    color: var(--accent);
}

.status-pill {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    font-size: .77rem;
    font-weight: 600;
    padding: 3px 8px;
    border: 1px solid transparent;
}

.status-queued,
.status-retry {
    background: #ecfeff;
    color: #155e75;
    border-color: #a5f3fc;
}

.status-processing {
    background: #ede9fe;
    color: #5b21b6;
    border-color: #c4b5fd;
}

.status-sent {
    background: #dcfce7;
    color: #166534;
    border-color: #86efac;
}

.status-failed {
    background: #fee2e2;
    color: #991b1b;
    border-color: #fca5a5;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    z-index: 1000;
}

.modal-backdrop[hidden] {
    display: none;
}

.modal-window {
    width: min(760px, 100%);
    background: #fff;
    border-radius: 12px;
    border: 1px solid #cbd5e1;
    box-shadow: 0 20px 55px rgba(15, 23, 42, 0.3);
    padding: 16px;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
}

.modal-header h2 {
    margin: 0;
    font-size: 1.1rem;
}

.modal-header button {
    margin-top: 0;
}

.modal-message-id {
    margin-top: 0;
    margin-bottom: 10px;
}

.modal-message-text {
    margin: 0;
    max-height: 55vh;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    background: #f8fafc;
    padding: 12px;
    font-family: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: .87rem;
}

.queue-show-btn {
    margin-top: 0;
    margin-right: 6px;
}

body.modal-open {
    overflow: hidden;
}

.api-reference {
    border-top: 4px solid #0ea5e9;
}

.api-reference-note {
    margin: 0 0 16px;
    color: #334155;
    font-size: .92rem;
}

.api-reference p code,
.api-reference li code {
    color: #0f172a;
    background: #e0f2fe;
    border: 1px solid #bae6fd;
    border-radius: 6px;
    padding: 2px 6px;
    font-size: .85rem;
    word-break: normal;
}

.api-reference pre {
    background: #0b1020;
    color: #e2e8f0;
    border-radius: 10px;
    border: 1px solid #1e293b;
    padding: 12px;
    overflow-x: auto;
}

.api-reference pre code {
    color: #cbd5e1;
    background: transparent;
    border: 0;
    padding: 0;
    font-size: .86rem;
    word-break: normal;
}

@media (max-width: 640px) {
    .inline-form {
        grid-template-columns: 1fr;
    }

    .api-key-create-form button {
        width: 100%;
    }

    .header-inner {
        flex-direction: column;
        align-items: flex-start;
    }

    .menu-wrap {
        flex-direction: column;
    }

    .menu {
        grid-template-columns: 1fr;
    }

    .menu-logout {
        width: 100%;
    }

    .menu-logout .link-btn {
        width: 100%;
    }

    .table-tools {
        align-items: stretch;
    }

    .table-tool {
        width: 100%;
    }

    .table-stats {
        margin-left: 0;
    }

    .table-pagination {
        width: 100%;
        justify-content: space-between;
    }

    .table-pagination button {
        min-width: 88px;
    }
}
