/* Dark theme is the factory default; light theme via html[data-theme="light"] */
:root {
    --bg: #0f172a;
    --card-bg: #1e293b;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --border: #334155;
    --input-bg: #0f172a;
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --success-bg: #064e3b;
    --success-text: #6ee7b7;
    --error-bg: #450a0a;
    --error-text: #fca5a5;
}

html[data-theme="light"] {
    --bg: #f4f5f7;
    --card-bg: #ffffff;
    --text: #1f2933;
    --muted: #6b7280;
    --border: #d1d5db;
    --input-bg: #ffffff;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success-bg: #ecfdf5;
    --success-text: #047857;
    --error-bg: #fef2f2;
    --error-text: #b91c1c;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    transition: background 0.2s, color 0.2s;
}

/* Links always use the theme accent - kills the unreadable purple :visited */
a {
    color: var(--primary);
}

main {
    display: flex;
    justify-content: center;
    padding: 3rem 1rem;
}

/* Authenticated layout: fixed sidebar on the left, content beside it */

.layout {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    width: 210px;
    flex-shrink: 0;
    position: sticky;
    top: 0;
    height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 1.5rem 1rem;
    background: var(--card-bg);
    border-right: 1px solid var(--border);
}

.sidebar .brand {
    font-size: 1.05rem;
    font-weight: 700;
    padding: 0 0.75rem;
    margin-bottom: 1.5rem;
    white-space: nowrap;
}

.sidebar nav {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    flex: 1;
}

.sidebar nav a {
    color: var(--text);
    text-decoration: none;
    font-size: 0.95rem;
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    transition: background 0.15s;
}

.sidebar nav a:hover {
    background: var(--bg);
}

.sidebar nav a.active {
    background: var(--primary);
    color: #fff;
}

/* .sidebar prefix: must out-rank the generic button[type="submit"] rule */
.sidebar .logout-button {
    width: 100%;
    margin-top: 0;
    padding: 0.5rem 0.75rem;
    font-size: 0.9rem;
    font-weight: 400;
    font-family: inherit;
    text-align: left;
    color: var(--muted);
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
}

.sidebar .logout-button:hover {
    color: var(--text);
    background: var(--bg);
}

.layout main {
    flex: 1;
    align-items: flex-start;
}

/* Small screens: sidebar becomes a top bar */
@media (max-width: 720px) {
    .layout {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        position: static;
        flex-direction: row;
        align-items: center;
        gap: 0.5rem;
        padding: 0.6rem 1rem;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }

    .sidebar .brand {
        margin-bottom: 0;
        padding: 0;
        font-size: 0.95rem;
    }

    .sidebar nav {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0;
    }

    .sidebar nav a {
        font-size: 0.85rem;
        padding: 0.4rem 0.5rem;
    }

    .logout-button {
        width: auto;
        font-size: 0.85rem;
        padding: 0.35rem 0.6rem;
    }

    /* Tables get their own horizontal scroll instead of breaking the page */
    table.apps {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
    }
}

.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 2rem;
    width: 100%;
    max-width: 520px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.card.narrow {
    max-width: 360px;
}

.card.wide {
    max-width: 760px;
}

h1 {
    margin: 0 0 1.5rem;
    font-size: 1.4rem;
}

.header-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

label {
    display: block;
    margin: 1rem 0 0.3rem;
    font-weight: 600;
    font-size: 0.9rem;
}

input,
textarea {
    width: 100%;
    padding: 0.55rem 0.7rem;
    font-size: 1rem;
    font-family: inherit;
    color: var(--text);
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
}

input:focus,
textarea:focus {
    outline: 2px solid var(--primary);
    outline-offset: -1px;
}

textarea {
    resize: vertical;
}

button[type="submit"] {
    margin-top: 1.25rem;
    width: 100%;
    padding: 0.6rem;
    font-size: 1rem;
    font-weight: 600;
    color: #fff;
    background: var(--primary);
    border: none;
    border-radius: 6px;
    cursor: pointer;
}

button[type="submit"]:hover {
    background: var(--primary-hover);
}

.theme-toggle {
    position: fixed;
    top: 1rem;
    right: 1rem;
    font-size: 1.1rem;
    line-height: 1;
    padding: 0.4rem 0.5rem;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    cursor: pointer;
}

.header-links {
    display: flex;
    align-items: baseline;
    gap: 1rem;
}

.header-links a {
    color: var(--primary);
    font-size: 0.9rem;
}

table.apps {
    width: 100%;
    border-collapse: collapse;
    margin: 1rem 0 2rem;
    font-size: 0.9rem;
}

table.apps th,
table.apps td {
    text-align: left;
    padding: 0.5rem 0.6rem;
    border-bottom: 1px solid var(--border);
}

table.apps th {
    font-weight: 600;
    color: var(--muted);
}

table.apps tbody tr {
    transition: background 0.1s;
}

table.apps tbody tr:hover {
    background: var(--bg);
}

/* Wide cards: full-width primary buttons look clumsy on 760px, size to content */
.card.wide button[type="submit"]:not(.action-button):not(.menu-item) {
    width: auto;
    padding: 0.6rem 1.4rem;
}

/* ⋯ dropdown menu (a <details> element, closed on outside click via menu.js) */
details.menu {
    position: relative;
    display: inline-block;
}

details.menu summary {
    list-style: none;
}

details.menu summary::-webkit-details-marker {
    display: none;
}

.menu-items {
    position: absolute;
    right: 0;
    top: calc(100% + 4px);
    z-index: 10;
    min-width: 180px;
    padding: 0.3rem;
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.28);
}

.menu-item,
button.menu-item {
    display: block;
    width: 100%;
    margin: 0;
    padding: 0.45rem 0.7rem;
    font-size: 0.9rem;
    font-weight: 400;
    font-family: inherit;
    text-align: left;
    text-decoration: none;
    color: var(--text);
    background: none;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    white-space: nowrap;
}

.menu-item:hover,
button.menu-item:hover {
    background: var(--bg);
}

.menu-item.danger,
button.menu-item.danger {
    color: var(--error-text);
}

.inline-form {
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.limit-input {
    width: 6.5rem;
    padding: 0.3rem 0.5rem;
    font-size: 0.85rem;
}

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

.key-box {
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.6rem;
    font-size: 0.78rem;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
    margin: 0.5rem 0;
}

h2 {
    font-size: 1.1rem;
    margin: 1rem 0 0.5rem;
}

.balance {
    font-size: 0.85rem;
    color: var(--muted);
    margin: -0.8rem 0 1rem;
}

.counter {
    text-align: right;
    font-size: 0.8rem;
    color: var(--muted);
    margin-top: 0.25rem;
}

.alert {
    padding: 0.7rem 1rem;
    border-radius: 6px;
    margin-bottom: 1rem;
    font-size: 0.95rem;
    word-break: break-word;
}

.alert.success {
    background: var(--success-bg);
    color: var(--success-text);
}

.alert.error {
    background: var(--error-bg);
    color: var(--error-text);
}

.diag {
    margin-top: 2rem;
    border-top: 1px solid var(--border);
    padding-top: 1rem;
}

.diag h2 {
    margin: 0 0 0.5rem;
    font-size: 1rem;
}

.diag pre {
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.8rem;
    font-size: 0.78rem;
    line-height: 1.5;
    overflow-x: auto;
    white-space: pre-wrap;
    word-break: break-all;
}

.field-error {
    display: block;
    color: var(--error-text);
    font-size: 0.85rem;
    margin-top: 0.25rem;
}

/* Stats / history / app settings additions */

select {
    padding: 0.4rem 0.6rem;
    font-size: 0.9rem;
    font-family: inherit;
    color: var(--text);
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
}

.filter-row {
    margin: 0.5rem 0 1rem;
    flex-wrap: wrap;
}

.inline-label {
    display: inline;
    margin: 0;
    font-weight: 600;
    font-size: 0.9rem;
}

.date-input {
    width: auto;
}

.stat-row {
    display: flex;
    gap: 1.5rem;
    font-size: 0.9rem;
    color: var(--muted);
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.status-ok {
    color: var(--success-text);
}

.status-fail {
    color: var(--error-text);
}

.pager {
    display: flex;
    gap: 1rem;
    align-items: baseline;
    margin-top: 1rem;
}

.pager a {
    color: var(--primary);
    font-size: 0.9rem;
}

table.apps tfoot th {
    color: var(--text);
    border-top: 2px solid var(--border);
}

.md-toolbar {
    margin-top: 1rem;
    align-items: center;
}

.toolbar-actions {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

/* Proper buttons for actions; works on <button> and <a> alike.
   button.action-button must out-rank the generic button[type="submit"] rule. */
.action-button,
button.action-button {
    display: inline-block;
    width: auto;
    margin-top: 0;
    padding: 0.45rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    font-family: inherit;
    line-height: 1.3;
    color: #fff;
    background: var(--primary);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition: background 0.15s;
}

.action-button:hover,
button.action-button:hover {
    background: var(--primary-hover);
}

.action-button.secondary,
button.action-button.secondary {
    color: var(--text);
    background: var(--card-bg);
    border: 1px solid var(--border);
}

.action-button.secondary:hover,
button.action-button.secondary:hover {
    background: var(--bg);
}

/* Compact variant for table rows, filters and pagers */
.action-button.small,
button.action-button.small {
    padding: 0.28rem 0.7rem;
    font-size: 0.82rem;
    font-weight: 500;
}

.md-view {
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.9rem;
    font-size: 0.78rem;
    line-height: 1.55;
    max-height: 32rem;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
}

button.danger-button,
button.secondary-button {
    width: auto;
    margin-top: 0.5rem;
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
    background: var(--card-bg);
    border: 1px solid var(--border);
    color: var(--text);
}

button.danger-button {
    color: var(--error-text);
    border-color: var(--error-text);
}
