:root {
    --bg: #ffffff;
    --surface: #ffffff;
    --surface-hover: #f3f7fe;
    --surface-selected: #e7f0fd;
    --text-primary: #18181b;
    --text-secondary: #71717a;
    --text-muted: #a1a1aa;
    --border: #e4e4e7;
    --border-subtle: #eeeef0;
    --accent: #2478f0;
    --accent-hover: #1a66d4;
    --accent-foreground: #ffffff;
    --header-fg: #ffffff;
    --header-fg-muted: rgba(255, 255, 255, 0.78);
    --focus-shadow: 0 0 0 3px rgba(36, 120, 240, 0.22);
    --danger: #9b3a3a;
    --ok: #1f6b4a;
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 10px;
    --transition: background-color 150ms ease, border-color 150ms ease, color 150ms ease;

    --ink: var(--text-primary);
    --muted: var(--text-secondary);
    --line: var(--border);
    --paper: var(--surface);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
    --radius: var(--radius-lg);
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
}

body {
    color: var(--text-primary);
    background: var(--bg);
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    font-size: 0.9375rem;
    line-height: 1.5;
}

a {
    color: var(--accent);
}

.brand,
.brand:hover {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-primary);
    text-decoration: none;
    font-weight: 600;
    letter-spacing: -0.02em;
    font-size: 1.05rem;
    line-height: 1;
}

.brand-mark {
    width: 28px;
    height: 28px;
    flex: none;
    display: block;
}

.brand-text {
    white-space: nowrap;
}

.app-frame {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--surface);
}

.app-header {
    flex: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    min-height: 48px;
    padding: 0.625rem 1.25rem;
    border-bottom: 1px solid #1a66d4;
    background: var(--accent);
    color: var(--header-fg);
}

.app-header .brand,
.app-header .brand:hover {
    color: var(--header-fg);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    min-width: 0;
}

.nav {
    display: flex;
    align-items: center;
    gap: 0.9rem;
}

.header-nav-link,
.header-nav-link:hover {
    color: var(--header-fg-muted);
    text-decoration: none;
    font-size: 0.9375rem;
    white-space: nowrap;
    transition: var(--transition);
}

.header-nav-link.is-active {
    color: var(--header-fg);
    font-weight: 600;
}

.header-nav-link:hover {
    color: var(--header-fg);
}

.header-divider {
    color: rgba(255, 255, 255, 0.45);
    font-size: 0.95rem;
    user-select: none;
}

.user-menu {
    position: relative;
    font-size: 0.9375rem;
}

.user-menu summary {
    list-style: none;
    cursor: pointer;
    color: var(--header-fg);
    padding: 0.25rem 0.15rem;
}

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

.user-menu summary::after {
    content: "";
    display: inline-block;
    width: 0.4rem;
    height: 0.4rem;
    margin-left: 0.4rem;
    border-right: 1.5px solid currentColor;
    border-bottom: 1.5px solid currentColor;
    transform: translateY(-0.15rem) rotate(45deg);
    opacity: 0.7;
}

.user-menu.is-active summary,
.user-menu[open] summary,
.user-menu summary:hover {
    color: var(--header-fg);
}

.user-menu-list {
    position: absolute;
    right: 0;
    top: calc(100% + 0.35rem);
    min-width: 180px;
    background: var(--surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: 0 8px 24px rgba(24, 24, 27, 0.08);
    padding: 0.35rem;
    z-index: 20;
}

.user-menu-list a {
    display: block;
    padding: 0.4rem 0.65rem;
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    text-decoration: none;
}

.user-menu-list a:hover,
.user-menu-list a.is-active {
    background: var(--surface-hover);
}

.simple-wrap {
    width: min(440px, calc(100% - 2rem));
    margin: 8vh auto 3rem;
}

.simple-wrap.is-wide {
    width: min(760px, calc(100% - 2rem));
}

.page-wrap,
.page {
    width: min(1040px, calc(100% - 2rem));
    margin: 0 auto;
    padding: 1.75rem 0 3rem;
}

.simple-wrap .page,
.auth-page {
    width: auto;
    margin: 0;
    padding: 0 0 1rem;
}

.layout-app .flash-banner {
    margin: 0;
    border: 0;
    border-radius: 0;
    border-bottom: 1px solid var(--border);
}

h1,
h2 {
    font-weight: 600;
    letter-spacing: -0.02em;
}

h1 {
    margin: 0 0 0.75rem;
    font-size: 1.6rem;
    letter-spacing: -0.03em;
}

.auth-page h1 {
    margin-top: 1.5rem;
}

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

.lede {
    margin-top: 0;
    color: var(--text-secondary);
}

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

.form label,
.stack-form label {
    display: block;
    margin-bottom: 0.85rem;
    font-size: 0.875rem;
}

.stack-form {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    margin: 0.25rem 0 1.5rem;
}

.form input,
.form select,
.form textarea,
.stack-form input,
.stack-form select,
.stack-form textarea {
    display: block;
    width: 100%;
    margin-top: 0.35rem;
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font: inherit;
    font-size: 0.875rem;
    line-height: 1.35;
    color: var(--text-primary);
    background: var(--surface);
    box-shadow: none;
    transition: var(--transition);
}

.form input::placeholder,
.stack-form input::placeholder,
.form textarea::placeholder {
    color: var(--text-muted);
}

.form input:hover,
.form select:hover,
.form textarea:hover,
.stack-form input:hover,
.stack-form select:hover,
.stack-form textarea:hover {
    border-color: var(--text-muted);
}

.form input:focus,
.form select:focus,
.form textarea:focus,
.stack-form input:focus,
.stack-form select:focus,
.stack-form textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: var(--focus-shadow);
}

.form input[type=file] {
    padding: 0.45rem;
    background: var(--surface);
}

.logo-preview {
    margin: 0 0 0.85rem;
    padding: 0.75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    max-width: 100%;
}

.logo-preview img {
    display: block;
    max-width: 240px;
    max-height: 120px;
    object-fit: contain;
}

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

.narrow {
    max-width: 480px;
}

.btn,
button.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--accent);
    background: var(--accent);
    color: var(--accent-foreground);
    padding: 0.45rem 0.95rem;
    border-radius: var(--radius-md);
    text-decoration: none;
    font: inherit;
    font-size: 0.875rem;
    cursor: pointer;
    transition: var(--transition);
}

.btn:hover,
.btn:focus {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    color: var(--accent-foreground);
}

.btn:focus-visible {
    box-shadow: var(--focus-shadow);
    outline: none;
}

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

.btn-secondary:hover,
.btn-secondary:focus {
    background: var(--surface-hover);
    border-color: var(--text-muted);
    color: var(--text-primary);
}

.actions,
.page-actions {
    display: flex;
    gap: 0.65rem;
    flex-wrap: wrap;
    align-items: center;
    margin-bottom: 1rem;
}

.invoice-actions {
    justify-content: flex-start;
}

.status-trigger {
    margin-left: auto;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-primary);
    padding: 0.45rem 0.95rem;
    border-radius: var(--radius-md);
    font: inherit;
    font-size: 0.875rem;
    cursor: pointer;
    transition: var(--transition);
}

.status-trigger:hover {
    background: var(--surface-hover);
    border-color: var(--text-muted);
}

.status-link {
    color: var(--accent);
}

.choice {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    margin-bottom: 0.65rem;
}

.choice input {
    width: auto;
    display: inline-block;
    margin: 0.2rem 0 0;
    padding: 0;
}

.actions form {
    margin: 0;
}

.table-wrap {
    overflow-x: auto;
}

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

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

.table th {
    color: var(--text-secondary);
    font-weight: 600;
    font-size: 0.82rem;
}

.num {
    text-align: right;
}

.row-actions {
    display: flex;
    gap: 0.65rem;
    align-items: center;
    justify-content: flex-end;
    white-space: nowrap;
}

.row-actions form {
    margin: 0;
}

.link-btn {
    border: 0;
    background: none;
    color: var(--danger);
    font: inherit;
    padding: 0;
    cursor: pointer;
}

.flash {
    padding: 0.75rem 0.9rem;
    border-radius: var(--radius-md);
    margin-bottom: 1rem;
}

.flash-error {
    background: #f8e8e6;
    color: var(--danger);
}

.flash-success {
    background: #e7f3ec;
    color: var(--ok);
}

.muted {
    color: var(--text-secondary);
}

.small {
    font-size: 0.86rem;
    white-space: pre-wrap;
}

.pre-wrap {
    white-space: pre-wrap;
}

.meta-list {
    margin: 0;
}

.meta-list > div {
    display: grid;
    grid-template-columns: 7rem minmax(0, 1fr);
    gap: 0.5rem;
    margin-bottom: 0.65rem;
}

.meta-list dt {
    margin: 0;
    color: var(--text-secondary);
    font-size: 0.82rem;
    font-weight: 600;
}

.meta-list dd {
    margin: 0;
}

.invoice-view-meta {
    align-items: start;
}

.layout-split {
    display: grid;
    grid-template-columns: minmax(0, 1.4fr) minmax(260px, 0.8fr);
    gap: 1rem;
}

.layout-split.even {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.stat-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
    margin-bottom: 1rem;
}

.stat {
    text-decoration: none;
    color: inherit;
}

.stat:hover {
    background: var(--surface-hover);
}

.stat-label {
    display: block;
    color: var(--text-secondary);
    font-size: 0.82rem;
    font-weight: 600;
}

.stat-value {
    font-size: 1.75rem;
    font-weight: 600;
    letter-spacing: -0.03em;
}

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

.field-with-action select {
    flex: 1;
}

.section-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.section-head h2 {
    margin: 0;
}

.items-table th:nth-child(2),
.items-table td:nth-child(2) {
    width: 42%;
}

.items-table td {
    vertical-align: top;
}

.items-table input,
.items-table select,
.items-table textarea {
    width: 100%;
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    font: inherit;
    font-size: 0.875rem;
    background: var(--surface);
    color: var(--text-primary);
}

.items-table textarea {
    min-height: 4.5em;
    resize: vertical;
}

.items-table input:focus,
.items-table select:focus,
.items-table textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: var(--focus-shadow);
}

.items-table input:disabled,
.items-table input.is-readonly {
    background: var(--surface-hover);
}

.invoice-total {
    margin: 1rem 0 0;
    text-align: right;
    font-size: 1rem;
    font-weight: 600;
}

.invoice-total-stack {
    margin-top: 1rem;
}

.invoice-total-stack .invoice-total {
    margin: 0 0 0.25rem;
}

.payment-form {
    display: grid;
    grid-template-columns: 10rem 10rem 8rem minmax(0, 1fr) auto;
    gap: 0.75rem;
    align-items: end;
    margin-top: 1.25rem;
}

.payment-form label {
    margin-bottom: 0;
}

.payment-form .actions {
    margin: 0;
}

.code-box {
    width: 100%;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.4;
    padding: 0.75rem;
    border-radius: var(--radius-md);
    border: 1px solid var(--border);
    background: #fafafa;
    color: var(--text-primary);
}

.modal {
    position: fixed;
    inset: 0;
    background: rgba(24, 24, 27, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.25rem;
}

.modal[hidden] {
    display: none;
}

.modal-card {
    width: min(480px, 100%);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.25rem 1.35rem;
    box-shadow: 0 12px 40px rgba(24, 24, 27, 0.12);
}

@media (max-width: 800px) {
    html,
    body {
        overflow-x: hidden;
    }

    .app-header {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        align-items: center;
        gap: 0.5rem 0.75rem;
        min-height: 0;
        padding: 0.75rem 1rem 0.65rem;
    }

    .brand {
        grid-column: 1;
        grid-row: 1;
        min-width: 0;
    }

    .brand-text {
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .header-actions {
        display: contents;
    }

    .user-menu {
        grid-column: 2;
        grid-row: 1;
        justify-self: end;
    }

    .header-divider {
        display: none;
    }

    .nav {
        grid-column: 1 / -1;
        grid-row: 2;
        display: flex;
        flex-wrap: wrap;
        gap: 0.45rem 0.85rem;
    }

    .user-menu-list {
        position: absolute;
        right: 0;
    }

    .page-wrap,
    .page {
        width: calc(100% - 1.5rem);
        padding: 1.25rem 0 3rem;
    }

    h1 {
        font-size: 1.4rem;
    }

    .grid-2,
    .layout-split,
    .payment-form {
        display: block;
    }

    .stat-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 0.75rem;
    }

    .card {
        padding: 1rem;
    }

    .payment-form label {
        margin-bottom: 0.85rem;
    }

    .payment-form .actions {
        margin-top: 0.25rem;
    }

    .payment-form .btn {
        width: 100%;
    }

    .field-with-action {
        flex-direction: column;
        align-items: stretch;
    }

    .field-with-action .btn {
        width: 100%;
    }

    .section-head {
        flex-wrap: wrap;
    }

    .page-actions,
    .invoice-actions,
    .actions {
        align-items: stretch;
    }

    .page-actions .btn,
    .invoice-actions .btn,
    .invoice-actions .status-trigger,
    .actions .btn {
        justify-content: center;
    }

    .invoice-actions .btn {
        flex: 1 1 8rem;
    }

    .status-trigger {
        margin-left: 0;
        width: 100%;
    }

    .table-wrap {
        overflow: visible;
    }

    .table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        border: 0;
    }

    .table,
    .table tbody {
        display: block;
        width: 100%;
    }

    .table tr {
        display: block;
        padding: 0.85rem 0;
        border-bottom: 1px solid var(--border);
    }

    .table tbody tr:first-child {
        padding-top: 0;
    }

    .table tbody tr:last-child {
        border-bottom: 0;
        padding-bottom: 0;
    }

    .table th,
    .table td {
        display: grid;
        grid-template-columns: 6.5rem minmax(0, 1fr);
        gap: 0.5rem;
        border-bottom: 0;
        padding: 0.25rem 0;
        text-align: left;
    }

    .table td::before {
        content: attr(data-label);
        color: var(--text-secondary);
        font-size: 0.75rem;
        font-weight: 600;
        padding-top: 0.2em;
    }

    .table td:not([data-label]),
    .table td.row-actions {
        display: flex;
        grid-template-columns: none;
        justify-content: flex-start;
        padding-top: 0.5rem;
    }

    .table td:not([data-label])::before,
    .table td.row-actions::before {
        content: none;
    }

    .table td.num {
        text-align: left;
    }

    .items-table tr.item-row {
        border: 1px solid var(--border);
        border-radius: var(--radius-lg);
        padding: 0.75rem;
        margin-bottom: 0.75rem;
        background: var(--surface);
    }

    .items-table tbody tr.item-row:first-child,
    .items-table tbody tr.item-row:last-child {
        padding: 0.75rem;
        border-bottom: 1px solid var(--border);
    }

    .items-table td {
        display: block;
        width: 100%;
        padding: 0.5rem 0;
        grid-template-columns: none;
    }

    .items-table th:nth-child(2),
    .items-table td:nth-child(2),
    .items-table td[data-label="Description"],
    .items-table .item-description {
        width: 100%;
        max-width: none;
    }

    .items-table td::before {
        display: block;
        margin-bottom: 0.25rem;
        padding-top: 0;
    }

    .items-table td.row-actions,
    .items-table td:not([data-label]) {
        padding-top: 0.25rem;
    }

    .meta-list > div {
        grid-template-columns: 5.5rem minmax(0, 1fr);
    }

    .modal {
        align-items: flex-end;
        padding: 0.75rem;
    }

    .modal-card {
        width: 100%;
        max-height: min(90vh, 720px);
        overflow: auto;
    }
}
