:root[data-theme="light"] {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --text: #1b1f24;
    --text-muted: #5b6472;
    --border: #d9dce1;
    --accent: #2f6f4f;
    --accent-contrast: #ffffff;
}

:root[data-theme="dark"] {
    --bg: #14171c;
    --surface: #1d2127;
    --text: #e7e9ec;
    --text-muted: #9aa2ad;
    --border: #2c313a;
    --accent: #4caf7d;
    --accent-contrast: #0d0f12;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    line-height: 1.5;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.app-name {
    font-weight: 600;
}

.header-controls {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.header-controls label {
    display: flex;
    gap: 0.4rem;
    align-items: center;
    font-size: 0.9rem;
    color: var(--text-muted);
}

select {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.2rem 0.4rem;
}

.app-main {
    max-width: 960px;
    margin: 0 auto;
    padding: 2rem 1.5rem;
}

/* Left sidebar shell (logged-in layout). Anonymous pages keep the simple
   top .app-header defined above. */
.app-shell {
    display: flex;
    min-height: 100vh;
}

.sidebar {
    flex: 0 0 240px;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    padding: 1rem 0;
    gap: 0.25rem;
}

.sidebar-brand {
    display: block;
    font-weight: 700;
    font-size: 1.05rem;
    padding: 0.5rem 1.25rem 1rem;
    color: var(--text);
    text-decoration: none;
}

.sidebar-project {
    padding: 0.5rem 1.25rem 0.75rem;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.5rem;
}

.sidebar-project-name {
    font-weight: 600;
    font-size: 0.95rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-project-switch {
    font-size: 0.8rem;
    color: var(--text-muted);
    text-decoration: none;
}

.sidebar-project-switch:hover {
    color: var(--accent);
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
}

.sidebar-nav-global {
    margin-top: auto;
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
}

.sidebar-link {
    display: block;
    padding: 0.55rem 1.25rem;
    color: var(--text);
    text-decoration: none;
    font-size: 0.92rem;
    border-left: 3px solid transparent;
}

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

.sidebar-link.active {
    background: var(--bg);
    border-left-color: var(--accent);
    font-weight: 600;
}

.sidebar-user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.75rem 1.25rem 0.25rem;
    border-top: 1px solid var(--border);
    margin-top: 0.5rem;
    font-size: 0.9rem;
    color: var(--text-muted);
}

.sidebar-toggle {
    display: none;
    position: fixed;
    top: 0.75rem;
    left: 0.75rem;
    z-index: 50;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    width: 2.25rem;
    height: 2.25rem;
    font-size: 1.1rem;
    cursor: pointer;
}

.app-content {
    flex: 1 1 auto;
    min-width: 0;
}

@media (max-width: 860px) {
    .sidebar-toggle {
        display: block;
    }

    .sidebar {
        position: fixed;
        inset: 0 auto 0 0;
        width: 240px;
        transform: translateX(-100%);
        transition: transform 0.15s ease-out;
        z-index: 40;
        box-shadow: 2px 0 12px rgba(0, 0, 0, 0.15);
    }

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

    .app-main {
        padding-top: 4rem;
    }
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1.5rem 2rem;
}

table.sortable {
    width: 100%;
    border-collapse: collapse;
}

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

table.sortable th {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

table.sortable th .sort-indicator {
    margin-left: 0.3rem;
    color: var(--text-muted);
}

form label {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    margin-bottom: 0.9rem;
    font-size: 0.9rem;
}

form input[type="text"],
form input[type="email"],
form input[type="password"],
form input[type="number"],
form select {
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.5rem 0.6rem;
    font-size: 1rem;
}

form button {
    background: var(--accent);
    color: var(--accent-contrast);
    border: none;
    border-radius: 4px;
    padding: 0.5rem 1rem;
    font-size: 0.95rem;
    cursor: pointer;
}

form button:disabled {
    background: var(--border);
    color: var(--text-muted);
    cursor: not-allowed;
}

/* Checkbox + label text on one line, checkbox first (form label defaults to
   a stacked column layout, which is right for text inputs but wrong here). */
.checkbox-row {
    display: flex !important;
    flex-direction: row !important;
    align-items: center;
    gap: 0.5rem;
}

.checkbox-row input[type="checkbox"] {
    flex: 0 0 auto;
    width: 1.1rem;
    height: 1.1rem;
}

/* Buttons that are visually links (navigation, "back", exports) but styled
   as clear, modern actions instead of underlined text. */
.btn {
    display: inline-block;
    background: var(--accent);
    color: var(--accent-contrast);
    border: none;
    border-radius: 4px;
    padding: 0.5rem 1rem;
    font-size: 0.95rem;
    text-decoration: none;
    cursor: pointer;
}

.btn:hover {
    filter: brightness(1.08);
}

.btn-secondary {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
}

.errors {
    color: #c0392b;
    padding-left: 1.2rem;
}

.error {
    color: #c0392b;
}

.notice {
    background: color-mix(in srgb, var(--accent) 12%, var(--surface));
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.75rem 1rem;
}

.flash {
    border-radius: 6px;
    padding: 0.6rem 1rem;
}

.flash-success {
    background: color-mix(in srgb, var(--accent) 18%, var(--surface));
}

.flash-error {
    background: color-mix(in srgb, #c0392b 18%, var(--surface));
}

.project-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin: 1rem 0 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 0.75rem;
}

.calendar-grid a {
    display: block;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.75rem;
    text-decoration: none;
    color: var(--text);
}

.calendar-grid .balance-positive {
    color: var(--accent);
}

.calendar-grid .balance-negative {
    color: #c0392b;
}

.columns-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
}

.recovery-code {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    font-size: 1.3rem;
    letter-spacing: 0.05em;
    background: var(--bg);
    border: 1px dashed var(--border);
    border-radius: 6px;
    padding: 0.75rem 1rem;
    display: inline-block;
}

.recovery-code-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.field-with-button {
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
}

.field-with-button select {
    flex: 1 1 auto;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 1rem;
}

.modal-overlay.hidden {
    display: none;
}

.modal-box {
    background: var(--surface);
    border-radius: 8px;
    padding: 1.5rem 2rem;
    max-width: 480px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-box h2 {
    margin-top: 0;
}

.modal-actions {
    display: flex;
    gap: 0.5rem;
    justify-content: flex-end;
    margin-top: 1rem;
}
