/* ===== Base ===== */
* { box-sizing: border-box; }

/* ===== Theme CSS Variables ===== */
:root {
    --color-bg:          #f8fafc;
    --color-surface:     #ffffff;
    --color-surface-2:   #f1f5f9;
    --color-border:      #e2e8f0;
    --color-text:        #0f172a;
    --color-text-2:      #475569;
    --color-text-3:      #94a3b8;
    --color-sidebar:     #0f172a;
    --color-sidebar-text:#94a3b8;
    --color-topbar:      #ffffff;
    --color-topbar-border:#e2e8f0;
    --color-primary:     #2563eb;
    --color-primary-light:#eff6ff;
    --color-primary-text:#1d4ed8;
    --color-accent:      #2563eb;
    /* Persistent tint for editable registry columns (spec §2.3). */
    --sheet-editable-bg: color-mix(in srgb, var(--color-primary) 4%, transparent);
}

[data-theme="dark"] {
    --color-bg:          #0f172a;
    --color-surface:     #1e293b;
    --color-surface-2:   #334155;
    --color-border:      #334155;
    --color-text:        #f1f5f9;
    --color-text-2:      #94a3b8;
    --color-text-3:      #64748b;
    --color-sidebar:     #020617;
    --color-sidebar-text:#64748b;
    --color-topbar:      #1e293b;
    --color-topbar-border:#334155;
    --color-primary-light:#1e3a8a;
    --color-primary-text:#93c5fd;
    --sheet-editable-bg: color-mix(in srgb, var(--color-primary) 6%, transparent);
}

/* Apply theme vars to key elements */
body                        { background: var(--color-bg); color: var(--color-text); }
aside#sidebar               { background: var(--color-sidebar) !important; }
header                      { background: var(--color-topbar) !important; border-color: var(--color-topbar-border) !important; }
.bg-white                   { background: var(--color-surface) !important; }
.bg-gray-50                 { background: var(--color-bg) !important; }
.bg-gray-100                { background: var(--color-surface-2) !important; }
.border-gray-200,
.border-gray-100            { border-color: var(--color-border) !important; }
.text-gray-900              { color: var(--color-text) !important; }
.text-gray-700,
.text-gray-800              { color: var(--color-text-2) !important; }
.text-gray-400,
.text-gray-500              { color: var(--color-text-3) !important; }

/* Spreadsheet table theming */
.sheet-table th             { background: var(--color-surface-2); color: var(--color-text-2); border-color: var(--color-border); }
.sheet-table td             { background: var(--color-surface); color: var(--color-text); border-color: var(--color-border); }
.sheet-table tr:hover td    { background: var(--color-surface-2); }
.sheet-cell-edit            { background: var(--color-primary-light) !important; outline: 2px solid var(--color-primary); }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    -webkit-font-smoothing: antialiased;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }

/* ===== Sidebar Nav Active ===== */
.nav-item.active {
    background: rgba(255,255,255,0.1);
    color: #ffffff !important;
}
.nav-item.active i { color: #60a5fa !important; }

/* ===== Dashboard Modules ===== */
.module-card {
    background: #ffffff;
    border-radius: 16px;
    border: 1px solid #e2e8f0;
    overflow: hidden;
    transition: box-shadow 0.2s, transform 0.2s;
    position: relative;
}

.module-card:hover {
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
}

/* Size classes for grid columns */
.module-sm  { grid-column: span 3; }
.module-md  { grid-column: span 6; }
.module-lg  { grid-column: span 12; }

@media (max-width: 1280px) {
    .module-sm  { grid-column: span 4; }
    .module-md  { grid-column: span 6; }
    .module-lg  { grid-column: span 12; }
}

@media (max-width: 900px) {
    .module-sm, .module-md { grid-column: span 12; }
    .module-lg  { grid-column: span 12; }
}

/* Edit mode */
.edit-mode .module-card {
    border: 2px dashed #93c5fd;
    cursor: grab;
}
.edit-mode .module-card:active { cursor: grabbing; }
.edit-mode .module-card .module-controls { display: flex !important; }

.module-controls {
    display: none;
    position: absolute;
    top: 10px;
    right: 10px;
    gap: 4px;
    z-index: 10;
}

.module-ctrl-btn {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: rgba(255,255,255,0.95);
    border: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 11px;
    color: #64748b;
    transition: all 0.15s;
    box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}
.module-ctrl-btn:hover { background: #f1f5f9; color: #1e293b; }
.module-ctrl-btn.danger:hover { background: #fee2e2; color: #ef4444; border-color: #fca5a5; }

/* Drag & Drop */
.module-dragging {
    opacity: 0.5;
    transform: scale(0.98);
}
.module-drag-over {
    border-color: #3b82f6 !important;
    background: #eff6ff !important;
}

/* ===== Stat Cards ===== */
.stat-value {
    font-size: 2rem;
    font-weight: 700;
    line-height: 1;
    letter-spacing: -0.02em;
}

/* ===== Status Badges ===== */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 99px;
    font-size: 11px;
    font-weight: 600;
}
.badge-green  { background: #dcfce7; color: #16a34a; }
.badge-blue   { background: #dbeafe; color: #2563eb; }
.badge-yellow { background: #fef9c3; color: #ca8a04; }
.badge-red    { background: #fee2e2; color: #dc2626; }
.badge-gray   { background: #f1f5f9; color: #64748b; }
.badge-orange { background: #ffedd5; color: #ea580c; }

/* ===== Progress Bar ===== */
.progress-bar {
    height: 6px;
    border-radius: 99px;
    background: #e2e8f0;
    overflow: hidden;
}
.progress-fill {
    height: 100%;
    border-radius: 99px;
    transition: width 0.6s ease;
}

/* ===== Chat Module ===== */
.chat-msg { display: flex; gap: 8px; align-items: flex-start; }
.chat-msg.own { flex-direction: row-reverse; }
.chat-bubble {
    max-width: 75%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 13px;
    line-height: 1.4;
}
.chat-bubble.other { background: #f1f5f9; color: #1e293b; border-bottom-left-radius: 4px; }
.chat-bubble.own   { background: #2563eb; color: #fff; border-bottom-right-radius: 4px; }

/* ===== Topbar Search ===== */
#globalSearch:focus + div kbd { opacity: 0; }

/* ===== Org Modal z-index ===== */
.z-60 { z-index: 60; }

/* ===== Animations ===== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(8px); }
    to   { opacity: 1; transform: translateY(0); }
}
.animate-in { animation: fadeIn 0.25s ease forwards; }

/* ===== Tooltip ===== */
[data-tooltip] { position: relative; }
[data-tooltip]:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    background: #1e293b;
    color: #fff;
    font-size: 11px;
    padding: 4px 8px;
    border-radius: 6px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 100;
}

/* ===== Spreadsheet Table ===== */
.sheet-table {
    border-collapse: collapse;
    width: 100%;
    font-size: 13px;
}
.sheet-table th {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 8px 10px;
    text-align: left;
    font-weight: 600;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    border-bottom: 2px solid var(--color-border);
    border-right: 1px solid var(--color-border);
    white-space: nowrap;
    user-select: none;
}
.sheet-table th:last-child { border-right: none; }
/* Editable-column header: pen hint + sortable affordance. */
.sheet-th-edit { margin-left: 6px; font-size: 12px; color: var(--color-text-3); }
.sheet-th-edit:hover { color: var(--color-primary); }
.sheet-th--sortable { cursor: pointer; }
.sheet-th--sortable:hover { background: var(--color-surface-2); }
.sheet-sort-icon { margin-left: 6px; font-size: 12px; color: var(--color-text-3); }
.sheet-th--sorted { color: var(--color-text); }
.sheet-th--sorted .sheet-sort-icon { color: var(--color-primary); }
.sheet-table th:focus-visible,
.sheet-table td.cell-editable:focus-visible {
    outline: none;
    box-shadow: inset 0 0 0 2px var(--color-primary-light, #bfdbfe);
    border-color: var(--color-primary, #2563eb);
}
.sheet-table td {
    padding: 0;
    border-bottom: 1px solid var(--color-border);
    border-right: 1px solid var(--color-border);
    vertical-align: middle;
    min-width: 80px;
}
.sheet-table td:last-child { border-right: none; }
/* Persistent editable-column tint (spec §2.3). */
.sheet-table td.cell-editable { background: var(--sheet-editable-bg); }
.sheet-table td.cell-editable { position: relative; cursor: cell; }
/* Single-click selection: outline only, no edit (spec Q2). */
.sheet-table td.cell-selected {
    outline: 2px solid var(--color-primary);
    outline-offset: -2px;
}
/* Hover pencil hint inside editable cells. */
.cell-edit-hint {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 14px;
    color: var(--color-primary);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s;
}
.sheet-table td.cell-editable:hover { background: var(--color-surface-2); }
.sheet-table td.cell-editable:hover .cell-edit-hint { opacity: 1; }
.sheet-table td .cell-inner {
    display: block;
    padding: 6px 10px;
    min-height: 36px;
    cursor: inherit;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.sheet-table td input.cell-input,
.sheet-table td select.cell-input {
    width: 100%;
    padding: 6px 10px;
    border: none;
    outline: 2px solid var(--color-primary, #2563eb);
    background: var(--color-primary-light, #eff6ff);
    font-size: 13px;
    font-family: inherit;
    color: var(--color-text, #0f172a);
    min-height: 36px;
}
.sheet-table td.cell-dirty .cell-inner::after {
    content: '';
    position: absolute;
    top: 2px; right: 2px;
    width: 5px; height: 5px;
    border-radius: 50%;
    background: #f59e0b;
}
.sheet-table td.cell-dirty { position: relative; }
.sheet-row-new td { background: #f0fdf4 !important; }
[data-theme="dark"] .sheet-row-new td { background: #14532d33 !important; }

/* Custom scrollbar for the registry scroll area (spec §2.3: 5px → 10px). */
.accounting-sheet-scroll::-webkit-scrollbar { width: 10px; height: 10px; }
.accounting-sheet-scroll::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 999px;
}
.accounting-sheet-scroll::-webkit-scrollbar-track { background: transparent; }

/* ===== Reusable pager (spec §2.2) ===== */
.pager {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 16px;
    border-top: 1px solid var(--color-border);
}
.pager-count { font-size: 13px; color: var(--color-text-3); }
.pager-controls { display: flex; align-items: center; gap: 4px; }
.pager-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 36px;
    padding: 0 8px;
    border-radius: 10px;
    border: 1px solid transparent;
    background: transparent;
    color: var(--color-text-2);
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.pager-btn:hover:not(:disabled) { background: var(--color-surface-2); }
.pager-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.pager-btn--active { background: var(--color-primary); color: #fff; }
.pager-btn--active:hover { background: var(--color-primary); }
.pager-btn:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--color-primary-light, #bfdbfe);
    border-color: var(--color-primary, #2563eb);
}
.pager-ellipsis { padding: 0 4px; color: var(--color-text-3); }

/* ===== Theme Switcher Panel ===== */
/* Panel is built and styled entirely via JS (theme.js) */
.theme-swatch {
    width: 28px; height: 28px;
    border-radius: 50%;
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform 0.15s, border-color 0.15s;
}
.theme-swatch:hover { transform: scale(1.15); }
.theme-swatch.active { outline: 2px solid var(--color-primary, #2563eb); outline-offset: 2px; transform: scale(1.15); }

/* =========================================================================
   Auth / Login  (spec T2 — one centered card, token-driven, theme-aware)
   Self-contained: does not rely on Tailwind utility classes so the login
   surface stays intact even without the CDN. Colors come from --color-*
   tokens, so light/dark both work via [data-theme].
   ========================================================================= */
.auth-page {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2.5rem 1.25rem;
    background: var(--color-bg);
}
.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 20px;
    box-shadow: 0 12px 40px rgba(15, 23, 42, 0.08);
    padding: 2rem 1.75rem 1.75rem;
}
[data-theme="dark"] .auth-card { box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45); }

.auth-head { text-align: center; }
.auth-logo {
    width: 52px;
    height: 52px;
    margin: 0 auto;
    border-radius: 14px;
    background: var(--color-primary);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.35rem;
    box-shadow: 0 6px 18px color-mix(in srgb, var(--color-primary) 35%, transparent);
}
.auth-org {
    margin-top: 0.85rem;
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--color-text-3);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    word-break: break-word;
}
.auth-title {
    margin-top: 0.35rem;
    font-size: 1.5rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    color: var(--color-text);
}
.auth-subtitle {
    margin-top: 0.4rem;
    font-size: 0.875rem;
    line-height: 1.45;
    color: var(--color-text-2);
}

.auth-alert {
    margin-top: 1.25rem;
    border-radius: 12px;
    border: 1px solid #fecaca;
    background: #fef2f2;
    color: #b91c1c;
    padding: 0.75rem 0.9rem;
    font-size: 0.8125rem;
    line-height: 1.4;
    display: flex;
    gap: 0.55rem;
    align-items: flex-start;
}
.auth-alert i { margin-top: 0.1rem; }
[data-theme="dark"] .auth-alert {
    border-color: #7f1d1d;
    background: #450a0a;
    color: #fca5a5;
}

.auth-form { margin-top: 1.5rem; display: flex; flex-direction: column; gap: 1.1rem; }
.auth-field { display: flex; flex-direction: column; gap: 0.4rem; }
.auth-label {
    font-size: 0.8125rem;
    font-weight: 600;
    color: var(--color-text-2);
}
.auth-input {
    width: 100%;
    border-radius: 11px;
    border: 1px solid var(--color-border);
    background: var(--color-surface);
    color: var(--color-text);
    padding: 0.65rem 0.8rem;
    font-size: 0.9375rem;
    font-family: inherit;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.auth-input::placeholder { color: var(--color-text-3); }
.auth-input:focus {
    outline: none;
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-primary) 20%, transparent);
}

.auth-remember {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.8125rem;
    color: var(--color-text-2);
    user-select: none;
    cursor: pointer;
}
.auth-remember input { width: 15px; height: 15px; accent-color: var(--color-primary); cursor: pointer; }

.auth-submit {
    width: 100%;
    border: none;
    border-radius: 11px;
    background: var(--color-primary);
    color: #fff;
    padding: 0.7rem 1rem;
    font-size: 0.9375rem;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition: filter 0.15s, opacity 0.15s;
}
.auth-submit:hover:not(:disabled) { filter: brightness(0.94); }
.auth-submit:focus-visible {
    outline: none;
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-primary) 30%, transparent);
}
.auth-submit:disabled { opacity: 0.75; cursor: progress; }
.auth-spinner {
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255, 255, 255, 0.45);
    border-top-color: #fff;
    border-radius: 50%;
    animation: authSpin 0.7s linear infinite;
}
@keyframes authSpin { to { transform: rotate(360deg); } }

.auth-support {
    margin-top: 1.4rem;
    padding-top: 1.1rem;
    border-top: 1px solid var(--color-border);
    text-align: center;
    font-size: 0.8125rem;
    color: var(--color-text-3);
    line-height: 1.5;
}
.auth-support a { color: var(--color-primary); text-decoration: none; font-weight: 600; }
.auth-support a:hover { text-decoration: underline; }
