/* ─── Variables ───────────────────────────────────────────────────────────── */
:root {
    --bg: #1e1e1e;
    --bg-surface: #2d2d2d;
    --bg-elevated: #3d3d3d;
    --bg-input: #3d3d3d;
    --fg: #e0e0e0;
    --fg-muted: #999;
    --accent: #4a6fa5;
    --accent-hover: #5a7fb5;
    --danger: #c0392b;
    --danger-hover: #e74c3c;
    --border: #555;
    --success: #27ae60;

    --entity-existing: #D4EDDA;
    --entity-new: #FFF3CD;
    --entity-error: #FFCCCC;

    --radius: 4px;
    --toolbar-h: 44px;
    --tabs-h: 38px;
    --row-controls-h: 34px;
}

/* ─── Reset ──────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-size: 14px;
    background: var(--bg);
    color: var(--fg);
    overflow: hidden;
    height: 100vh;
}

/* ─── Toolbar ────────────────────────────────────────────────────────────── */
.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: var(--toolbar-h);
    padding: 0 12px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
}
.toolbar-left { display: flex; align-items: center; gap: 12px; }
.toolbar-right { display: flex; align-items: center; gap: 6px; }
.toolbar-sep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }

.app-title { font-weight: 600; font-size: 15px; }
.context-label { font-size: 12px; color: var(--fg-muted); }
.user-info { display: inline-flex; align-items: center; gap: 6px; margin-left: 6px; }
.user-avatar {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--bg-elevated);
}

/* ─── Login ──────────────────────────────────────────────────────────────── */
.login-section {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    background: var(--bg);
}
.login-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    width: 360px;
    padding: 32px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-surface);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
.login-card p,
.domain-badge {
    color: var(--fg-muted);
}
.domain-badge {
    padding: 4px 10px;
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: 12px;
}
.auth-error {
    color: var(--danger-hover);
    text-align: center;
}

/* ─── Buttons ────────────────────────────────────────────────────────────── */
.btn {
    padding: 6px 14px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-elevated);
    color: var(--fg);
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
}
.btn:hover { background: #4a4a4a; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn-primary { background: var(--accent); border-color: var(--accent); }
.btn-primary:hover { background: var(--accent-hover); }
.btn-accent { background: var(--accent); border-color: var(--accent); }
.btn-accent:hover { background: var(--accent-hover); }
.btn-danger { color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }

/* ─── Tabs ───────────────────────────────────────────────────────────────── */
.tabs {
    display: flex;
    height: var(--tabs-h);
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    padding: 0 8px;
    gap: 2px;
    align-items: flex-end;
}
.tab {
    padding: 6px 18px;
    border: 1px solid transparent;
    border-bottom: none;
    border-radius: var(--radius) var(--radius) 0 0;
    background: transparent;
    color: var(--fg-muted);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
}
.tab:hover { color: var(--fg); background: var(--bg-elevated); }
.tab.active {
    color: var(--fg);
    background: var(--bg);
    border-color: var(--border);
}

/* ─── Row Controls ───────────────────────────────────────────────────────── */
.row-controls {
    display: flex;
    align-items: center;
    height: var(--row-controls-h);
    padding: 0 12px;
    gap: 8px;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
}

/* ─── Spreadsheet ────────────────────────────────────────────────────────── */
#spreadsheet-container {
    height: calc(100vh - var(--toolbar-h) - var(--tabs-h) - var(--row-controls-h));
    overflow: hidden;
}

/* Each sheet: flex column so table fills space and add-row bar stays at bottom */
#spreadsheet-container .sheet-container {
    display: flex;
    flex-direction: column;
    height: 100%;
}
#spreadsheet-container .sheet-table-wrapper {
    min-height: 0;
}
#spreadsheet-container .add-row-bar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    padding: 6px 12px;
    background: var(--bg-surface);
    border-top: 1px solid var(--border);
}
#spreadsheet-container .add-row-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    font-size: 20px;
    line-height: 1;
    border-radius: var(--radius);
    display: flex;
    align-items: center;
    justify-content: center;
}
#spreadsheet-container .add-row-btn:hover {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

/* Handsontable dark theme overrides */
#spreadsheet-container .handsontable {
    color: var(--fg);
    font-size: 13px;
}
#spreadsheet-container .handsontable th,
#spreadsheet-container .handsontable td {
    border-color: var(--border) !important;
}
#spreadsheet-container .handsontable th {
    background: var(--bg-surface) !important;
    color: var(--fg) !important;
}
#spreadsheet-container .handsontable td {
    background: var(--bg) !important;
    color: var(--fg) !important;
}
#spreadsheet-container .handsontable td.area {
    background: #3a4a5a !important;
}
#spreadsheet-container .handsontable .currentRow td {
    background: #2a3a4a !important;
}
#spreadsheet-container .handsontable td.current {
    outline: 2px solid var(--accent) !important;
}

/* Entity color classes */
#spreadsheet-container .handsontable td.entity-existing {
    background: var(--entity-existing) !important;
    color: #155724 !important;
}
#spreadsheet-container .handsontable td.entity-new {
    background: var(--entity-new) !important;
    color: #856404 !important;
}
#spreadsheet-container .handsontable td.entity-error {
    background: var(--entity-error) !important;
    color: #721c24 !important;
}

/* Handsontable dropdown/autocomplete overrides */
.handsontableInput {
    background: var(--bg-input) !important;
    color: var(--fg) !important;
    border-color: var(--accent) !important;
}
.htAutocompleteArrow { color: var(--fg-muted) !important; }
ul.htContextMenu { background: var(--bg-surface) !important; border-color: var(--border) !important; }
ul.htContextMenu li { color: var(--fg) !important; }
ul.htContextMenu li:hover,
ul.htContextMenu li.current { background: var(--accent) !important; }
ul.htContextMenu li.htSeparator { border-color: var(--border) !important; }

/* ─── Modals ─────────────────────────────────────────────────────────────── */
.modal {
    position: fixed;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,0.6);
    z-index: 1000;
}
.modal.hidden { display: none; }
.modal-content {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 24px;
    min-width: 420px;
    max-width: 90vw;
    max-height: 80vh;
    overflow-y: auto;
}
.modal-content h2 { margin-bottom: 16px; font-size: 18px; }
.modal-wide { min-width: 650px; }
.modal-buttons { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; margin-bottom: 4px; font-size: 13px; color: var(--fg-muted); }
.form-group select,
.form-group input[type="text"] {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg-input);
    color: var(--fg);
    font-size: 14px;
}
.form-group select:disabled { opacity: 0.5; }
.status-text { font-size: 12px; color: var(--fg-muted); min-height: 18px; }

/* ─── Overlay ────────────────────────────────────────────────────────────── */
.overlay {
    position: fixed;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,0.5);
    z-index: 2000;
}
.overlay.hidden { display: none; }
.overlay-content { text-align: center; }
.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Progress Bar ───────────────────────────────────────────────────────── */
.progress-bar-container {
    width: 100%;
    height: 20px;
    background: var(--bg-elevated);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 10px;
}
.progress-bar {
    height: 100%;
    background: var(--accent);
    transition: width 0.3s;
    border-radius: 10px;
}

/* ─── Validation ─────────────────────────────────────────────────────────── */
.validation-list { max-height: 400px; overflow-y: auto; }
.validation-item {
    padding: 6px 10px;
    border-left: 3px solid;
    margin-bottom: 4px;
    font-size: 13px;
    border-radius: 0 var(--radius) var(--radius) 0;
    background: var(--bg-elevated);
}
.validation-item.error { border-color: var(--danger); }
.validation-item.warning { border-color: #f39c12; }
.validation-item.info { border-color: var(--accent); }

/* ─── Draft Table ────────────────────────────────────────────────────────── */
.draft-table { width: 100%; border-collapse: collapse; }
.draft-table th,
.draft-table td {
    padding: 8px 10px;
    text-align: left;
    border-bottom: 1px solid var(--border);
    font-size: 13px;
}
.draft-table th { color: var(--fg-muted); font-weight: 600; }
.draft-table tr:hover td { background: var(--bg-elevated); }
.draft-table tr.selected td { background: #3a4a5a; }

/* ─── Upload Errors ──────────────────────────────────────────────────────── */
.upload-errors {
    max-height: 200px;
    overflow-y: auto;
    margin-top: 12px;
    padding: 10px;
    background: var(--bg-elevated);
    border-radius: var(--radius);
    font-size: 13px;
}
.upload-errors.hidden { display: none; }

/* ─── Utility ────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
.text-success { color: var(--success); }
.text-error { color: var(--danger); }
