:root {
    --bg: #faf9f7;
    --surface: #ffffff;
    --text: #1c1917;
    --muted: #78716c;
    --border: #e7e5e4;
    --primary: #c2410c;
    --primary-hover: #9a3412;
    --primary-tint: #fff7ed;
    --surface-muted: #fafaf9;
    --ok: #15803d;
    --error: #b91c1c;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(28, 25, 23, 0.08);
    --sidebar-width: 220px;
    --sidebar-width-collapsed: 56px;
}

html[data-theme="dark"] {
    --bg: #1c1917;
    --surface: #292524;
    --text: #fafaf9;
    --muted: #a8a29e;
    --border: #44403c;
    --primary: #ea580c;
    --primary-hover: #c2410c;
    --primary-tint: rgba(234, 88, 12, 0.15);
    --surface-muted: #353330;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

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

.container {
    width: min(1100px, 92vw);
    margin: 0 auto;
}

/* App shell with collapsible sidebar */
.app-layout {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    min-height: 100vh;
    transition: grid-template-columns 0.2s ease;
}

.app-layout.sidebar-collapsed {
    grid-template-columns: var(--sidebar-width-collapsed) 1fr;
}

.sidebar {
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    position: sticky;
    top: 0;
    height: 100vh;
    overflow: hidden;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 0.5rem;
    border-bottom: 1px solid var(--border);
    min-height: 3.25rem;
}

.sidebar-brand {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
}

.sidebar-brand .brand {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.app-layout.sidebar-collapsed .sidebar-brand {
    display: none;
}

.app-layout.sidebar-collapsed .sidebar-header {
    justify-content: center;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
}

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

.sidebar-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.625rem 0.75rem;
    color: var(--muted);
    text-decoration: none;
    border-radius: 8px;
    white-space: nowrap;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-link:hover,
.sidebar-link.active {
    color: var(--primary);
    background: var(--primary-tint);
}

.sidebar-icon {
    width: 1.25rem;
    height: 1.25rem;
    flex-shrink: 0;
}

.sidebar-label {
    overflow: hidden;
    transition: opacity 0.15s ease;
}

.app-layout.sidebar-collapsed .sidebar-label {
    opacity: 0;
    width: 0;
}

.app-layout.sidebar-collapsed .sidebar-link {
    justify-content: center;
    padding-left: 0.625rem;
    padding-right: 0.625rem;
}

.sidebar-footer {
    border-top: 1px solid var(--border);
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.sidebar-profile-label {
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    padding: 0.375rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease;
}

.sidebar-toggle:hover {
    background: #f5f5f4;
    color: var(--text);
}

.sidebar-toggle-icon {
    width: 1.125rem;
    height: 1.125rem;
    transition: transform 0.2s ease;
}

.app-layout.sidebar-collapsed .sidebar-toggle-icon {
    transform: rotate(180deg);
}

.app-body {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 100vh;
}

.app-body .main-content {
    flex: 1;
    width: min(1100px, 92%);
    margin: 0 auto;
    padding: 2rem 0 4rem;
}

.app-body .site-footer {
    width: min(1100px, 92%);
    margin: 0 auto;
}

.site-header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.header-inner {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 1.5rem;
    padding: 0.9rem min(4vw, 2rem);
}

.header-inner--guest {
    justify-content: flex-start;
    padding: 0.9rem 0;
}

.brand {
    font-weight: 700;
    color: var(--text);
    text-decoration: none;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.user-email {
    color: var(--muted);
    font-size: 0.875rem;
}

.main-content {
    padding: 2rem 0 4rem;
}

.site-footer {
    border-top: 1px solid var(--border);
    padding: 1.5rem 0;
    color: var(--muted);
    font-size: 0.875rem;
}

.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.5rem;
}

.page-header h1 {
    margin: 0;
    font-size: 1.75rem;
}

.stat-tiles {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 1rem;
    margin: 1.25rem 0 2rem;
}

.stat-tile {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem 1.5rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.stat-tile-label {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.stat-tile-value {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1.1;
    color: var(--text);
}

.status-badge {
    font-size: 0.8125rem;
    padding: 0.35rem 0.75rem;
    border-radius: 999px;
    font-weight: 600;
}

.status-ok {
    background: #dcfce7;
    color: var(--ok);
}

.status-error {
    background: #fee2e2;
    color: var(--error);
}

.meta, .hint {
    color: var(--muted);
    font-size: 0.9375rem;
}

.hint-inline {
    font-size: 0.75rem;
    color: var(--muted);
}

.metrics-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 1rem;
    margin-top: 1rem;
}

.metric-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
    box-shadow: var(--shadow);
}

.metric-card h2 {
    margin: 0 0 1rem;
    font-size: 1rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.metric-card dl {
    margin: 0;
    display: grid;
    gap: 0.65rem;
}

.metric-card dl div {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 1rem;
}

.metric-card dt {
    color: var(--muted);
    font-size: 0.875rem;
}

.metric-card dd {
    margin: 0;
    font-weight: 700;
    font-size: 1.125rem;
}

.chart-section {
    margin-top: 2.5rem;
}

.charts-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1rem;
    margin-top: 1rem;
}

.chart-wrap {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
}

.table-wrap {
    overflow-x: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}

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

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

.data-table th {
    background: var(--surface-muted);
    font-size: 0.8125rem;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.load-more-wrap,
.load-more-cell {
    text-align: center;
    padding: 1rem;
}

.auth-card {
    max-width: 420px;
    margin: 3rem auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 2rem;
    box-shadow: var(--shadow);
    text-align: center;
}

.auth-card.wide {
    max-width: 480px;
}

.auth-card h1 {
    margin: 0 0 0.5rem;
    font-size: 1.5rem;
}

.subtitle {
    color: var(--muted);
    margin-bottom: 1.5rem;
}

.qr-wrap {
    margin: 1rem 0 1.5rem;
}

.form {
    text-align: left;
    display: grid;
    gap: 0.75rem;
}

.form-input {
    width: 100%;
    padding: 0.65rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 1rem;
    background: var(--surface);
    color: var(--text);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.65rem 1.25rem;
    border-radius: 8px;
    font-weight: 600;
    text-decoration: none;
    border: none;
    cursor: pointer;
    font-size: 0.9375rem;
}

.btn-primary {
    background: var(--primary);
    color: white;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

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

.btn-ghost {
    background: transparent;
    color: var(--muted);
    padding: 0.4rem 0.75rem;
}

.btn-google {
    width: 100%;
}

.btn-sm {
    padding: 0.45rem 0.9rem;
    font-size: 0.875rem;
}

.alert {
    padding: 0.85rem 1rem;
    border-radius: 8px;
    margin-bottom: 1rem;
}

.alert-error {
    background: #fee2e2;
    color: var(--error);
}

.error {
    color: var(--error);
}

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

.messages {
    list-style: none;
    padding: 0;
    margin: 0 0 1rem;
}

.message {
    padding: 0.75rem 1rem;
    border-radius: 8px;
    background: #eff6ff;
}

code {
    background: var(--surface-muted);
    padding: 0.15rem 0.35rem;
    border-radius: 4px;
    font-size: 0.875em;
}

/* Settings */
.settings-layout {
    display: grid;
    grid-template-columns: 200px 1fr;
    gap: 2rem;
    align-items: start;
}

.settings-nav {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    position: sticky;
    top: 1rem;
}

.settings-nav-link {
    display: block;
    padding: 0.625rem 0.875rem;
    border-radius: 8px;
    color: var(--muted);
    text-decoration: none;
    font-weight: 500;
    transition: background 0.15s ease, color 0.15s ease;
}

.settings-nav-link:hover,
.settings-nav-link.active {
    color: var(--primary);
    background: var(--primary-tint);
}

.settings-panel {
    min-width: 0;
}

.settings-section {
    margin-bottom: 2.5rem;
}

.settings-section h2 {
    margin: 0 0 0.75rem;
    font-size: 1.125rem;
}

.settings-section:last-child {
    margin-bottom: 0;
}

.settings-form {
    max-width: 420px;
}

.settings-dl {
    margin: 0;
    display: grid;
    gap: 0.75rem;
}

.settings-dl div {
    display: flex;
    gap: 1rem;
}

.settings-dl dt {
    min-width: 5rem;
    color: var(--muted);
    font-size: 0.875rem;
}

.settings-dl dd {
    margin: 0;
    font-weight: 600;
}

.settings-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    box-shadow: var(--shadow);
}

.settings-card-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
}

.settings-card-row p {
    margin: 0.25rem 0 0;
}

.settings-actions {
    margin-top: 1rem;
}

.theme-toggle {
    display: inline-flex;
    gap: 0.5rem;
    padding: 0.25rem;
    background: var(--surface-muted);
    border: 1px solid var(--border);
    border-radius: 10px;
}

.theme-toggle-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.625rem 1rem;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    font-weight: 600;
    font-size: 0.9375rem;
    cursor: pointer;
    transition: background 0.15s ease, color 0.15s ease, box-shadow 0.15s ease;
}

.theme-toggle-btn svg {
    width: 1.125rem;
    height: 1.125rem;
}

.theme-toggle-btn:hover {
    color: var(--text);
}

.theme-toggle-btn.active {
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}

html[data-theme="dark"] .status-ok {
    background: rgba(21, 128, 61, 0.2);
}

html[data-theme="dark"] .status-error {
    background: rgba(185, 28, 28, 0.2);
}

html[data-theme="dark"] .alert-error {
    background: rgba(185, 28, 28, 0.15);
}

html[data-theme="dark"] .message {
    background: rgba(59, 130, 246, 0.15);
}

html[data-theme="dark"] .sidebar-toggle:hover {
    background: var(--surface-muted);
}

@media (max-width: 720px) {
    .settings-layout {
        grid-template-columns: 1fr;
        gap: 1.25rem;
    }

    .settings-nav {
        flex-direction: row;
        flex-wrap: wrap;
        position: static;
    }
}
