/* ---------------------------------------------------------------------------
   Design tokens — dark is default, [data-theme="light"] overrides. A user's
   saved preference (users.theme_preference) sets data-theme server-side, so
   there's no flash-of-wrong-theme after login. Pre-login (the login page has
   no user yet) falls back to the OS preference via prefers-color-scheme.
--------------------------------------------------------------------------- */
:root {
    --bg: #0f1115;
    --bg-elevated: #171a21;
    --bg-subtle: #1d212a;
    --border: #262b36;
    --text: #eceef2;
    --text-dim: #9aa1b0;
    --text-faint: #6b7080;
    --accent: #5b8cff;
    --accent-text: #ffffff;
    --danger: #f2555c;
    --danger-bg: rgba(242, 85, 92, 0.14);
    --success: #3ddc8f;
    --success-bg: rgba(61, 220, 143, 0.14);
    --warning: #f5a83f;
    --warning-bg: rgba(245, 168, 63, 0.14);
    --accent-bg: rgba(91, 140, 255, 0.14);

    --radius-sm: 6px;
    --radius: 10px;
    --radius-lg: 14px;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.24), 0 8px 24px rgba(0, 0, 0, 0.18);
    --header-h: 56px;
}

:root[data-theme="light"] {
    --bg: #f6f7f9;
    --bg-elevated: #ffffff;
    --bg-subtle: #f0f1f4;
    --border: #e2e4ea;
    --text: #1a1d24;
    --text-dim: #676d7d;
    --text-faint: #9198a8;
    --accent: #3d6bf0;
    --accent-text: #ffffff;
    --danger: #d92d36;
    --danger-bg: rgba(217, 45, 54, 0.08);
    --success: #14a86a;
    --success-bg: rgba(20, 168, 106, 0.1);
    --warning: #b5730a;
    --warning-bg: rgba(181, 115, 10, 0.1);
    --accent-bg: rgba(61, 107, 240, 0.08);
    --shadow: 0 1px 2px rgba(20, 20, 30, 0.06), 0 8px 24px rgba(20, 20, 30, 0.06);
}

@media (prefers-color-scheme: light) {
    :root:not([data-theme]) {
        --bg: #f6f7f9;
        --bg-elevated: #ffffff;
        --bg-subtle: #f0f1f4;
        --border: #e2e4ea;
        --text: #1a1d24;
        --text-dim: #676d7d;
        --text-faint: #9198a8;
        --accent: #3d6bf0;
        --accent-text: #ffffff;
        --danger: #d92d36;
        --danger-bg: rgba(217, 45, 54, 0.08);
        --success: #14a86a;
        --success-bg: rgba(20, 168, 106, 0.1);
        --warning: #b5730a;
        --warning-bg: rgba(181, 115, 10, 0.1);
        --accent-bg: rgba(61, 107, 240, 0.08);
        --shadow: 0 1px 2px rgba(20, 20, 30, 0.06), 0 8px 24px rgba(20, 20, 30, 0.06);
    }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

h1 { font-size: 22px; font-weight: 700; margin: 0 0 16px; letter-spacing: -0.01em; }
h2 { font-size: 16px; font-weight: 600; margin: 0 0 10px; }
p { margin: 0 0 10px; }

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

code {
    background: var(--bg-subtle);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 1px 5px;
    font-size: 12px;
}

/* ---------------------------------------------------------------------------
   Buttons & form controls
--------------------------------------------------------------------------- */
button, .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: var(--accent);
    color: var(--accent-text);
    border: 1px solid var(--accent);
    border-radius: var(--radius-sm);
    padding: 9px 14px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    min-height: 40px;
    transition: opacity 0.12s ease, transform 0.05s ease;
    text-decoration: none;
}
button:hover, .btn:hover { opacity: 0.88; text-decoration: none; }
button:active, .btn:active { transform: scale(0.98); }
button:disabled { opacity: 0.45; cursor: not-allowed; }

button.secondary, .btn.secondary {
    background: transparent;
    border-color: var(--border);
    color: var(--text);
}
button.danger, .btn.danger { background: var(--danger); border-color: var(--danger); }

input, select, textarea {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: var(--radius-sm);
    padding: 9px 11px;
    font-size: 14px;
    min-height: 40px;
    width: 100%;
    font-family: inherit;
}
input:focus, select:focus, textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-bg);
}
textarea { min-height: unset; }
label { font-size: 13px; color: var(--text-dim); display: block; }

/* ---------------------------------------------------------------------------
   Header / nav — collapses into a slide-down drawer under 860px
--------------------------------------------------------------------------- */
.app-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    height: var(--header-h);
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}
.app-header__brand {
    font-weight: 700;
    color: var(--text);
    font-size: 16px;
    letter-spacing: -0.01em;
}
.app-header__brand:hover { text-decoration: none; }

.nav-toggle {
    display: flex;
    flex-direction: column;
    justify-content: center;
    gap: 4px;
    width: 36px;
    height: 36px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0;
    min-height: unset;
    align-items: center;
}
.nav-toggle span {
    display: block;
    width: 16px;
    height: 2px;
    background: var(--text);
    border-radius: 1px;
    transition: transform 0.15s ease, opacity 0.15s ease;
}
.nav-toggle.open span:nth-child(1) { transform: translateY(6px) rotate(45deg); }
.nav-toggle.open span:nth-child(2) { opacity: 0; }
.nav-toggle.open span:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }

.app-nav {
    display: none;
    position: fixed;
    top: var(--header-h);
    left: 0;
    right: 0;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    flex-direction: column;
    padding: 8px;
    gap: 2px;
    box-shadow: var(--shadow);
    max-height: calc(100vh - var(--header-h));
    overflow-y: auto;
}
.app-nav.open { display: flex; }
.app-nav a {
    color: var(--text-dim);
    padding: 11px 12px;
    border-radius: var(--radius-sm);
    font-size: 14.5px;
    font-weight: 500;
}
.app-nav a:hover { background: var(--bg-subtle); text-decoration: none; color: var(--text); }
.app-nav a.active { color: var(--text); background: var(--accent-bg); }
.app-nav__divider { display: none; }
.app-nav__user {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px 4px;
    margin-top: 4px;
    border-top: 1px solid var(--border);
    font-size: 13px;
}
.user-chip { color: var(--text-dim); }
.user-chip em { font-style: normal; color: var(--text-faint); }
.install-btn {
    background: transparent;
    color: var(--text-dim);
    border: 1px solid var(--border);
    margin: 4px 12px;
    width: calc(100% - 24px);
}

@media (min-width: 860px) {
    .app-header { gap: 24px; padding: 0 24px; }
    .nav-toggle { display: none; }
    .app-nav {
        display: flex;
        position: static;
        flex-direction: row;
        align-items: center;
        background: transparent;
        border: none;
        box-shadow: none;
        padding: 0;
        flex: 1;
        max-height: unset;
        overflow: visible;
    }
    .app-nav a { padding: 8px 12px; }
    .app-nav__divider.desktop-only { display: block; width: 1px; height: 22px; background: var(--border); margin: 0 4px; }
    .app-nav__user { border: none; padding: 0; margin: 0; margin-left: auto; gap: 14px; }
    .install-btn { width: auto; margin: 0; padding: 7px 12px; min-height: 34px; font-size: 13px; }
}

.app-main { padding: 16px; max-width: 1200px; margin: 0 auto; }
@media (min-width: 860px) { .app-main { padding: 24px; } }

/* ---------------------------------------------------------------------------
   Login
--------------------------------------------------------------------------- */
.login-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 16px;
}
.login-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    padding: 32px 28px;
    width: 100%;
    max-width: 340px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}
.login-card h1 { margin-bottom: 4px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.login-card .error {
    color: var(--danger);
    background: var(--danger-bg);
    border-radius: var(--radius-sm);
    padding: 8px 10px;
    font-size: 13px;
    margin: 0;
}

/* ---------------------------------------------------------------------------
   Tables — become stacked cards under 700px
--------------------------------------------------------------------------- */
table.data-table { width: 100%; border-collapse: collapse; }
table.data-table th, table.data-table td {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: middle;
}
table.data-table th { color: var(--text-faint); font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
table.data-table tr:last-child td { border-bottom: none; }

@media (max-width: 700px) {
    table.data-table thead { display: none; }
    table.data-table, table.data-table tbody, table.data-table tr, table.data-table td { display: block; width: 100%; }
    table.data-table tr {
        border: 1px solid var(--border);
        border-radius: var(--radius);
        margin-bottom: 10px;
        padding: 4px 0;
        overflow: hidden;
    }
    table.data-table td {
        border-bottom: none;
        padding: 6px 12px;
        display: flex;
        justify-content: space-between;
        gap: 12px;
        align-items: center;
    }
    table.data-table td:empty { display: none; }
    table.data-table td::before {
        content: attr(data-label);
        color: var(--text-faint);
        font-size: 11px;
        text-transform: uppercase;
        letter-spacing: 0.04em;
        font-weight: 600;
        flex-shrink: 0;
    }
}

/* ---------------------------------------------------------------------------
   Shared surfaces
--------------------------------------------------------------------------- */
.badge {
    display: inline-flex;
    align-items: center;
    padding: 3px 9px;
    border-radius: 999px;
    font-size: 11.5px;
    font-weight: 600;
    line-height: 1.4;
    white-space: nowrap;
}

.card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 18px;
    margin-bottom: 14px;
    box-shadow: var(--shadow);
}

.flash { padding: 11px 14px; border-radius: var(--radius); margin-bottom: 14px; font-size: 13.5px; font-weight: 500; }
.flash.success { background: var(--success-bg); color: var(--success); }
.flash.error { background: var(--danger-bg); color: var(--danger); }

.desktop-only { display: none; }
@media (min-width: 860px) {
    .desktop-only { display: initial; }
}

/* ---------------------------------------------------------------------------
   Settings — theme picker
--------------------------------------------------------------------------- */
.theme-picker { display: flex; gap: 10px; flex-wrap: wrap; }
.theme-option {
    display: flex;
    align-items: center;
    gap: 10px;
    background: var(--bg-subtle);
    border: 1.5px solid var(--border);
    color: var(--text);
    border-radius: var(--radius);
    padding: 12px 16px;
    min-height: unset;
}
.theme-option.active { border-color: var(--accent); background: var(--accent-bg); }
.theme-swatch {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1px solid var(--border);
    flex-shrink: 0;
}
.theme-swatch--dark { background: linear-gradient(135deg, #0f1115 50%, #262b36 50%); }
.theme-swatch--light { background: linear-gradient(135deg, #ffffff 50%, #e2e4ea 50%); }

/* ---------------------------------------------------------------------------
   Orders dashboard
--------------------------------------------------------------------------- */
.tabs {
    display: flex;
    gap: 2px;
    margin-bottom: 18px;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}
.tabs a {
    padding: 9px 14px;
    color: var(--text-dim);
    border-bottom: 2px solid transparent;
    font-size: 13.5px;
    font-weight: 500;
    white-space: nowrap;
    flex-shrink: 0;
}
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }

.bulk-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    padding: 10px 12px;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 18px;
    position: sticky;
    top: calc(var(--header-h) + 8px);
    z-index: 5;
    box-shadow: var(--shadow);
}
.bulk-actions label { display: flex; align-items: center; gap: 6px; font-size: 13.5px; color: var(--text); flex-shrink: 0; }
.bulk-actions button, .bulk-actions .btn { font-size: 13.5px; padding: 8px 12px; min-height: 36px; }

.store-group { margin-bottom: 26px; }
.store-group h2 {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14.5px;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}
.store-group__count { color: var(--text-faint); font-weight: 400; font-size: 13px; }

.order-card {
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 14px;
    margin-bottom: 10px;
    display: flex;
    gap: 12px;
}
.order-checkbox { width: 20px; height: 20px; flex-shrink: 0; margin-top: 2px; accent-color: var(--accent); }
.order-card__main { flex: 1; min-width: 0; }

.order-card__header {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 6px;
}
.order-card__number { font-weight: 700; font-size: 14.5px; }
.badge--outline { background: var(--bg-subtle); border: 1px solid var(--border); color: var(--text-dim); }

.order-card__customer {
    font-size: 13.5px;
    color: var(--text-dim);
    margin-bottom: 10px;
    line-height: 1.5;
}
.order-card__customer strong { color: var(--text); font-weight: 600; }

.expand-toggle {
    background: transparent;
    border: 1px dashed var(--border);
    color: var(--accent);
    font-size: 12.5px;
    padding: 6px 10px;
    min-height: 32px;
}

.order-items { display: flex; flex-direction: column; gap: 10px; }
.order-items.collapsed .order-item:not(:first-child) { display: none; }

.order-item {
    display: flex;
    gap: 12px;
    padding-top: 10px;
    border-top: 1px solid var(--border);
}
.order-item:first-child { padding-top: 0; border-top: none; }

.order-item__image {
    width: 60px;
    height: 60px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    background: var(--bg-subtle);
    border: 1px solid var(--border);
    cursor: zoom-in;
    flex-shrink: 0;
}
.order-item__details { font-size: 13.5px; flex: 1; min-width: 0; }
.order-item__name { font-weight: 600; margin-bottom: 2px; }
.order-item__meta { color: var(--text-faint); font-size: 12px; margin-bottom: 6px; }

.order-item__stats {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 6px;
    max-width: 340px;
}
.order-item__stat { display: flex; flex-direction: column; gap: 1px; }
.order-item__stat-label { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-faint); }
.order-item__stat-value { font-size: 13px; font-weight: 600; }

.order-item__discounts { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.badge--accent { background: var(--accent-bg); color: var(--accent); }
.badge--success { background: var(--success-bg); color: var(--success); }
.badge--danger { background: var(--danger-bg); color: var(--danger); }
.badge--warning { background: var(--warning-bg); color: var(--warning); }

.order-item__footer { color: var(--text-faint); font-size: 12px; }

.lightbox {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.85);
    align-items: center;
    justify-content: center;
    z-index: 100;
    cursor: zoom-out;
    padding: 20px;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 8px; }

@media (min-width: 860px) {
    .order-item__stats { max-width: 420px; }
}

.form-stack { display: flex; flex-direction: column; gap: 12px; }
.form-stack label { display: flex; flex-direction: column; gap: 6px; }
.card--narrow { max-width: 440px; }
.hint { color: var(--text-dim); font-size: 13px; margin: 0 0 12px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
