/*
  InvoiceApp — Interface Design System
  Depth: surface shifts + subtle borders (rgba). No decorative gradients/shadows.
  Signature: letterhead rule under titles; inset controls.
*/

@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,400;0,9..40,500;0,9..40,600;0,9..40,700&display=swap');

/* --- Primitives: canvas, surface, ink, rule, accent, control, semantic --- */
:root {
    --font: 'DM Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --space-base: 4px;
    --space-1: 8px;
    --space-2: 12px;
    --space-3: 16px;
    --space-4: 24px;
    --space-5: 32px;
    --space-6: 48px;
    --radius-sm: 6px;
    --radius-md: 8px;
    --radius-lg: 12px;

    /* Light: paper world */
    --canvas: #f5f5f0;
    --surface-1: #fcfcf9;
    --surface-2: #f8f8f4;
    --ink: #1c1c1a;
    --ink-secondary: #4a4a46;
    --ink-tertiary: #73736e;
    --ink-muted: #9a9a94;
    --rule: rgba(0, 0, 0, 0.08);
    --rule-subtle: rgba(0, 0, 0, 0.05);
    --rule-strong: rgba(0, 0, 0, 0.12);
    --accent: #2c5282;
    --accent-hover: #234e77;
    --control-bg: #eeeeea;
    --control-rule: rgba(0, 0, 0, 0.1);
    --control-focus: rgba(44, 82, 130, 0.35);
    --success: #276749;
    --warning: #b45309;
    --destructive: #c53030;
    --wave-fill: rgba(44, 82, 130, 0.06);
}

[data-theme="dark"] {
    --canvas: #18181a;
    --surface-1: #1e1e20;
    --surface-2: #242426;
    --ink: #f4f4f0;
    --ink-secondary: #c8c8c4;
    --ink-tertiary: #9a9a96;
    --ink-muted: #6b6b68;
    --rule: rgba(255, 255, 255, 0.08);
    --rule-subtle: rgba(255, 255, 255, 0.04);
    --rule-strong: rgba(255, 255, 255, 0.14);
    --accent: #63b3ed;
    --accent-hover: #90cdf4;
    --control-bg: #252528;
    --control-rule: rgba(255, 255, 255, 0.1);
    --control-focus: rgba(99, 179, 237, 0.3);
    --success: #68d391;
    --warning: #f6ad55;
    --destructive: #fc8181;
    --wave-fill: rgba(99, 179, 237, 0.05);
}

/* --- Base --- */
* {
    box-sizing: border-box;
}

body {
    font-family: var(--font);
    background: var(--canvas);
    color: var(--ink);
    margin: 0;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    position: relative;
}

/* Fondo decorativo wave: siempre detrás del contenido, no ocupa espacio en el flujo */
.wave-bg {
    position: fixed;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    overflow: hidden;
}

.wave-bg svg {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40vh;
    min-height: 200px;
}

.wave-bg__path {
    fill: var(--wave-fill);
}

/* --- Layout --- */
main.main-content {
    margin: 0;
}

.container {
    max-width: 960px;
    margin: 0 auto;
    padding: 0 var(--space-3);
}

.main-content {
    position: relative;
    z-index: 2;
    padding-top: 120px;
    padding-bottom: var(--space-4);
    min-height: calc(100vh - 120px);
}

.main-content>* {
    margin-top: 0;
}

.main-content.auth-page {
    padding: var(--space-3);
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* --- Header: mismo fondo que canvas. padding-top de .main-content debe clearing el header. --- */
.main-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: var(--canvas);
    border-bottom: 1px solid var(--rule);
    padding: var(--space-3) 0;
}

.main-header .container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--ink) !important;
    text-decoration: none;
    letter-spacing: -0.02em;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: var(--space-1);
}

.nav-links a {
    color: var(--ink-secondary);
    text-decoration: none;
    font-size: 0.875rem;
    font-weight: 500;
    padding: var(--space-1) var(--space-2);
    border-radius: var(--radius-sm);
    transition: color 0.15s, background 0.15s;
}

.nav-links a:hover {
    color: var(--ink);
    background: var(--rule-subtle);
}

.nav-links .btn {
    padding: var(--space-1) var(--space-3);
    font-size: 0.875rem;
    width: auto;
}

.nav-links .btn-secondary {
    background: transparent;
    border: 1px solid var(--rule-strong);
    color: var(--ink);
}

.nav-links .btn-secondary:hover {
    background: var(--surface-2);
    border-color: var(--ink-tertiary);
}

.theme-toggle-btn {
    background: transparent;
    border: none;
    padding: var(--space-1);
    border-radius: var(--radius-sm);
    cursor: pointer;
    color: var(--ink-tertiary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: color 0.15s, background 0.15s;
}

.theme-toggle-btn:hover {
    color: var(--ink);
    background: var(--rule-subtle);
}

[data-theme="light"] .moon-icon {
    display: none;
}

[data-theme="dark"] .sun-icon {
    display: none;
}

/* --- Letterhead rule (signature) --- */
.letterhead-rule {
    display: block;
    height: 1px;
    background: var(--rule);
    margin: var(--space-2) 0 var(--space-3) 0;
    border: 0;
}

/* --- Cards: surface-1, subtle rule --- */
.card {
    background: var(--surface-1);
    border: 1px solid var(--rule);
    border-radius: var(--radius-lg);
    padding: var(--space-4);
}

/* --- Buttons --- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-2) var(--space-4);
    border-radius: var(--radius-md);
    font-family: var(--font);
    font-size: 0.9375rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
    text-decoration: none;
    border: none;
    width: 100%;
}

.btn-primary {
    background: var(--accent);
    color: #fff;
}

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

.btn-secondary {
    background: transparent;
    color: var(--ink);
    border: 1px solid var(--rule-strong);
}

.btn-secondary:hover {
    background: var(--surface-2);
    border-color: var(--ink-tertiary);
}

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

/* --- Forms: control tokens (inset) --- */
.form-field {
    margin-bottom: var(--space-3);
}

.form-field label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--ink-secondary);
    margin-bottom: var(--space-1);
}

.form-input {
    width: 100%;
    padding: var(--space-2) var(--space-3);
    font-family: var(--font);
    font-size: 0.9375rem;
    color: var(--ink);
    background: var(--control-bg);
    border: 1px solid var(--control-rule);
    border-radius: var(--radius-md);
    transition: border-color 0.15s, box-shadow 0.15s;
}

.form-input::placeholder {
    color: var(--ink-muted);
}

.form-input:hover {
    border-color: var(--rule-strong);
}

.form-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--control-focus);
}

textarea.form-input {
    min-height: 88px;
    resize: vertical;
}

.error {
    font-size: 0.8125rem;
    color: var(--destructive);
    margin-top: var(--space-1);
    display: block;
}

/* --- Typography hierarchy --- */
.page-title {
    font-size: 1.5rem;
    font-weight: 600;
    letter-spacing: -0.02em;
    color: var(--ink);
    margin: 0 0 var(--space-1) 0;
}

.page-subtitle {
    font-size: 0.9375rem;
    color: var(--ink-secondary);
    margin: 0 0 var(--space-3) 0;
}

.section-title {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--ink-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin: 0 0 var(--space-3) 0;
}

/* --- Flash --- */
.flash-messages {
    position: fixed;
    top: var(--space-4);
    right: var(--space-4);
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
}

.flash-message {
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 500;
    background: var(--surface-1);
    border: 1px solid var(--rule);
    color: var(--ink);
    animation: flashIn 0.2s ease;
}

.flash-success {
    border-left: 3px solid var(--success);
}

.flash-error {
    border-left: 3px solid var(--destructive);
}

.flash-info {
    border-left: 3px solid var(--accent);
}

@keyframes flashIn {
    from {
        opacity: 0;
        transform: translateY(-4px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* --- Auth: single centered panel, no gradient --- */
.auth-panel {
    width: 100%;
    max-width: 400px;
}

.auth-panel .card {
    padding: var(--space-4);
}

.auth-panel .letterhead-rule {
    margin-bottom: var(--space-3);
}

.auth-panel .page-subtitle {
    margin-bottom: var(--space-3);
}

.auth-panel .form-field {
    margin-bottom: var(--space-3);
}

.auth-panel .btn {
    margin-top: var(--space-1);
}

.auth-panel .auth-footer {
    margin-top: var(--space-3);
    font-size: 0.875rem;
    color: var(--ink-tertiary);
}

.auth-panel .auth-footer a {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
}

.auth-panel .auth-footer a:hover {
    text-decoration: underline;
}

/* --- Dashboard: one focal block + stats --- */
.dashboard {
    margin: 0;
    padding: 0;
}

.dashboard .container {
    max-width: 960px;
    padding-top: 0;
}

/* --- Dashboard Headers --- */
.dashboard-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: var(--space-4);
    flex-wrap: wrap;
    gap: var(--space-3);
}

.dashboard-hero {
    background: var(--surface-1);
    border: 1px solid var(--rule);
    border-radius: var(--radius-lg);
    padding: var(--space-4);
    margin-bottom: var(--space-4);
    margin-top: 0;
}

.dashboard-hero .page-title {
    margin-bottom: var(--space-1);
    margin-top: 0;
}

.dashboard-hero .page-subtitle {
    margin-bottom: var(--space-3);
    margin-top: 0;
}

.dashboard-hero__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

.dashboard-hero__actions .btn {
    width: auto;
    min-width: 200px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: var(--space-3);
}

.stat-card {
    background: var(--surface-1);
    border: 1px solid var(--rule);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
}

.stat-card__icon {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.stat-card__icon svg {
    width: 20px;
    height: 20px;
}

.stat-card__icon--muted {
    background: var(--rule-subtle);
    color: var(--ink-tertiary);
}

.stat-card__label {
    font-size: 0.8125rem;
    color: var(--ink-tertiary);
    margin: 0 0 var(--space-1) 0;
}

.stat-card__value {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--ink);
    margin: 0;
    font-variant-numeric: tabular-nums;
}

.stat-card__list {
    list-style: none;
    padding: 0;
    margin: var(--space-1) 0 0 0;
    font-size: 0.875rem;
    color: var(--ink-secondary);
}

.stat-card__list li {
    display: flex;
    justify-content: space-between;
    padding: var(--space-1) 0;
    border-bottom: 1px solid var(--rule-subtle);
}

.stat-card__list li:last-child {
    border-bottom: 0;
}

/* --- Profile: sections with rule --- */
.profile .container {
    max-width: 800px;
}

.profile .card {
    padding: var(--space-4);
}

.profile-header {
    margin-bottom: var(--space-4);
}

.profile-header .letterhead-rule {
    margin-bottom: var(--space-3);
}

.form-fieldset {
    margin-bottom: var(--space-4);
    padding: 0;
    border: 0;
}

.fieldset-legend {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--ink-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin-bottom: var(--space-2);
    padding: 0;
}

.fieldset-grid {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: var(--space-2) var(--space-3);
}

.form-actions {
    margin-top: var(--space-4);
    padding-top: var(--space-3);
    border-top: 1px solid var(--rule);
}

.form-actions .btn {
    max-width: 220px;
}

/* Firma: subir imagen o dibujar */
.form-fieldset--signature .signature-options {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

.signature-upload input[type="file"] {
    padding: var(--space-1);
}

.signature-draw label,
.signature-upload label,
.signature-preview label {
    display: block;
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--ink-secondary);
    margin-bottom: var(--space-1);
}

.signature-canvas-wrap {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
}

.signature-canvas {
    display: block;
    border: 1px solid var(--rule);
    border-radius: var(--radius-md);
    background: var(--control-bg);
    cursor: crosshair;
    touch-action: none;
}

.signature-clear-btn {
    width: auto;
    min-width: auto;
    padding: var(--space-1) var(--space-2);
}

.signature-preview-img {
    max-width: 200px;
    max-height: 80px;
    object-fit: contain;
    border: 1px solid var(--rule);
    border-radius: var(--radius-sm);
}

.span-3 {
    grid-column: span 3;
}

.span-4 {
    grid-column: span 4;
}

.span-6 {
    grid-column: span 6;
}

.span-12 {
    grid-column: span 12;
}

@media (max-width: 640px) {
    .fieldset-grid {
        grid-template-columns: 1fr;
    }

    .span-3,
    .span-4,
    .span-6,
    .span-12 {
        grid-column: span 1;
    }
}

/* Text content (e.g. TOS) */
.text-content {
    max-width: 65ch;
}

.text-content h1 {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0 0 var(--space-3) 0;
}

.text-content h2 {
    font-size: 1.125rem;
    font-weight: 600;
    margin: var(--space-4) 0 var(--space-2) 0;
}

.text-content p,
.text-content ul {
    margin: 0 0 var(--space-2) 0;
    color: var(--ink-secondary);
}

.text-content ul {
    padding-left: var(--space-4);
}

/* --- Variable Chips --- */
.variables-helper {
    margin-bottom: var(--space-3);
    padding: var(--space-2);
    background: var(--surface-2);
    border-radius: var(--radius-md);
    border: 1px dashed var(--rule-strong);
    /* Stronger border for container */
}

.variables-helper p {
    font-size: 0.75rem;
    color: var(--ink-tertiary);
    margin: 0 0 var(--space-2) 0;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.chip-container {
    display: flex;
    gap: var(--space-2);
    flex-wrap: wrap;
}

.chip {
    display: inline-flex;
    align-items: center;
    padding: 4px 12px;
    background: var(--surface-1);
    /* Lighter than control-bg */
    border: 1px solid var(--ink-tertiary);
    /* Visible border */
    border-radius: 999px;
    font-size: 0.8125rem;
    /* Slightly larger text */
    font-family: var(--font-mono);
    color: var(--ink);
    /* Stronger text color */
    cursor: grab;
    /* Indicate draggable */
    transition: all 0.2s ease;
    user-select: none;
    font-weight: 500;
}

.chip:hover {
    background: var(--accent);
    color: #fff;
    border-color: var(--accent);
    transform: translateY(-1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.15);
    cursor: pointer;
}


/* --- Toggle Switch --- */
.toggle-switch {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    cursor: pointer;
    user-select: none;
}

/* Hide default checkbox but keep it accessible/focusable if needed, though appearance none handles visual */
.toggle-switch input[type="checkbox"] {
    appearance: none;
    -webkit-appearance: none;
    width: 44px;
    height: 24px;
    background: var(--control-bg);
    border: 1px solid var(--control-rule);
    border-radius: 999px;
    position: relative;
    transition: background 0.2s, border-color 0.2s;
    cursor: pointer;
    flex-shrink: 0;
    outline: none;
}

.toggle-switch input[type="checkbox"]::after {
    content: "";
    position: absolute;
    top: 2px;
    left: 2px;
    width: 18px;
    height: 18px;
    background: var(--ink-muted);
    border-radius: 50%;
    transition: transform 0.2s, background 0.2s;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.toggle-switch input[type="checkbox"]:checked {
    background: var(--accent);
    border-color: var(--accent);
}

.toggle-switch input[type="checkbox"]:checked::after {
    transform: translateX(20px);
    background: white;
}

.toggle-switch:hover input[type="checkbox"] {
    border-color: var(--rule-strong);
}

.toggle-switch:hover input[type="checkbox"]::after {
    background: var(--ink-secondary);
}

.toggle-switch input[type="checkbox"]:checked:hover::after {
    background: white;
}

/* --- Data Table & Toolbar --- */
.table-toolbar {
    background: var(--surface-1);
    padding: var(--space-3);
    border-bottom: 1px solid var(--rule);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-3);
    flex-wrap: wrap;
}

.table-toolbar .search-group {
    display: flex;
    gap: var(--space-2);
    flex: 1;
    max-width: 400px;
}

.table-toolbar .search-group input {
    width: 100%;
}

/* Search Wrapper for Icon Integration */
.search-wrapper {
    position: relative;
    width: 100%;
    display: flex;
    align-items: center;
}

.search-wrapper input[type="search"] {
    width: 100%;
    padding-right: 40px;
    padding: var(--space-1) var(--space-3);
    /* Room for icon */
}

.search-wrapper .search-btn-icon {
    position: absolute;
    right: 8px;
    background: none;
    border: none;
    color: var(--ink-muted);
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s;
}

.search-wrapper .search-btn-icon:hover {
    color: var(--accent);
}

.table-toolbar .per-page-group {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    color: var(--ink-secondary);
    font-size: 0.875rem;
}

/* --- Pagination --- */
.pagination-container {
    padding: var(--space-3);
    border-top: 1px solid var(--rule);
    display: flex;
    justify-content: flex-end;
    /* Right align */
    align-items: center;
    gap: var(--space-2);
}

.pagination-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 32px;
    height: 32px;
    padding: 0 6px;
    border-radius: var(--radius-md);
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--ink-secondary);
    text-decoration: none;
    transition: all 0.2s ease;
    border: 1px solid transparent;
}

.pagination-btn:hover:not(.disabled) {
    background: var(--surface-3);
    color: var(--ink);
}

.pagination-btn.active {
    background: var(--accent);
    color: white;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.pagination-btn.disabled {
    opacity: 0.5;
    cursor: default;
}

.pagination-dots {
    color: var(--ink-muted);
    padding: 0 4px;
}