* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    /* Tema Claro (padrão) */
    --bg-primary: #f0f2f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f8fafc;
    --bg-hover: #fafdff;
    --bg-selected: #e6f0ff;
    --bg-object: #f0f9ff;
    --bg-array: #f0fdf4;
    --bg-subgrid: #f8fafc;
    --bg-header: linear-gradient(0deg, #f1f5f9, #ffffff);
    
    --text-primary: #1a2639;
    --text-secondary: #1e293b;
    --text-header: #0b1c33;
    --text-object: #0369a1;
    --text-array: #166534;
    --text-number: #0b5e8a;
    --text-boolean: #b45309;
    --text-null: #94a3b8;
    
    --border-primary: #e2e8f0;
    --border-secondary: #edf2f7;
    --border-cell: #eef2f6;
    --border-header: #d0d9e8;
    
    --accent-blue: #3b82f6;
    --accent-green: #16a34a;
    --accent-orange: #b45309;
    
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.02);
    --shadow-md: 0 4px 20px rgba(0,0,0,0.02);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.03);
}

/* Tema Escuro forçado */
[data-theme="dark"] {
    --bg-primary: #0f172a;
    --bg-secondary: #1e293b;
    --bg-tertiary: #334155;
    --bg-hover: #2d3a4a;
    --bg-selected: #1e3a5f;
    --bg-object: #1e3a5f;
    --bg-array: #1a4731;
    --bg-subgrid: #2d3a4a;
    --bg-header: linear-gradient(0deg, #334155, #1e293b);
    
    --text-primary: #e2e8f0;
    --text-secondary: #cbd5e1;
    --text-header: #f1f5f9;
    --text-object: #7ab7e0;
    --text-array: #6fcf97;
    --text-number: #7ab7e0;
    --text-boolean: #fbbf24;
    --text-null: #94a3b8;
    
    --border-primary: #334155;
    --border-secondary: #2d3a4a;
    --border-cell: #334155;
    --border-header: #475569;
    
    --accent-blue: #60a5fa;
    --accent-green: #4ade80;
    --accent-orange: #fbbf24;
    
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 20px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
}

/* Tema claro forçado */
[data-theme="light"] {
    --bg-primary: #f0f2f5;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f8fafc;
    --bg-hover: #fafdff;
    --bg-selected: #e6f0ff;
    --bg-object: #f0f9ff;
    --bg-array: #f0fdf4;
    --bg-subgrid: #f8fafc;
    --bg-header: linear-gradient(0deg, #f1f5f9, #ffffff);
    
    --text-primary: #1a2639;
    --text-secondary: #1e293b;
    --text-header: #0b1c33;
    --text-object: #0369a1;
    --text-array: #166534;
    --text-number: #0b5e8a;
    --text-boolean: #b45309;
    --text-null: #94a3b8;
    
    --border-primary: #e2e8f0;
    --border-secondary: #edf2f7;
    --border-cell: #eef2f6;
    --border-header: #d0d9e8;
    
    --accent-blue: #3b82f6;
    --accent-green: #16a34a;
    --accent-orange: #b45309;
    
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.02);
    --shadow-md: 0 4px 20px rgba(0,0,0,0.02);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.03);
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    padding: 1.5rem;
    min-height: 100vh;
    font-size: 16px;
    transition: background-color 0.3s ease, color 0.3s ease;
}

.container {
    max-width: 1800px;
    margin: 0 auto;
    width: 100%;
}

/* cabeçalho e controles */
.toolbar {
    position: relative;               /* allow dropdown to escape stacking context */
    z-index: 1000;                    /* keep toolbar (and children) above grid */
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    align-items: center;
    margin-bottom: 1.5rem;
    background: var(--bg-secondary);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: 1.2rem 2rem;
    border-radius: 16px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border-primary);
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.logo-area {
    display: flex;
    align-items: center;
}

.logo {
    font-size: 1.4rem;
    font-weight: 600;
    background: linear-gradient(135deg, var(--accent-blue), var(--accent-green));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    letter-spacing: -0.02em;
}

.file-area {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
}

.btn {
    background: var(--bg-secondary);
    border: 1px solid var(--border-primary);
    padding: 0.6rem 1.4rem;
    border-radius: 12px;
    font-weight: 500;
    font-size: 0.95rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text-secondary);
    box-shadow: var(--shadow-sm);
}

.btn:hover {
    background: var(--bg-hover);
    border-color: var(--text-null);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn:active {
    transform: translateY(0);
}

.btn-small {
    padding: 0.4rem 1.1rem;
    font-size: 0.85rem;
    border-radius: 10px;
}

.btn-primary {
    background: var(--accent-blue);
    border-color: var(--accent-blue);
    color: white;
}

.btn-primary:hover {
    background: var(--accent-blue);
    filter: brightness(1.1);
    border-color: var(--accent-blue);
}

.btn-outline {
    background: transparent;
}

.btn-outline:hover {
    background: var(--bg-hover);
}

.status-badge {
    background: var(--bg-tertiary);
    border-radius: 30px;
    padding: 0.4rem 1.3rem;
    font-size: 0.9rem;
    color: var(--text-secondary);
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    border: 1px solid var(--border-primary);
}

.status-badge.warning {
    background: #fffbeb;
    color: #b45309;
    border-color: #fcd34d;
}

@media (prefers-color-scheme: dark) {
    .status-badge.warning {
        background: #422006;
        color: #fbbf24;
        border-color: #854d0e;
    }
}

.flex-spacer {
    flex: 1;
}

/* Dropdown de tema - Z-INDEX CORRIGIDO */
.theme-dropdown {
    position: relative;
    display: inline-block;
    z-index: 100000; /* extra high so dropdown always floats above everything */
}

.theme-dropdown-content {
    display: none;
    /* position will be dynamically updated in JS to "fixed" so it lives
       at the root of the document and cannot be clipped by siblings/parents */
    position: absolute;
    right: 0;
    top: 100%;
    background-color: var(--bg-secondary);
    min-width: 180px;
    box-shadow: var(--shadow-lg);
    border-radius: 12px;
    border: 1px solid var(--border-primary);
    z-index: 20001;
    margin-top: 8px;
    overflow: hidden;
}

.theme-dropdown:hover .theme-dropdown-content,
.theme-dropdown-content:hover {
    display: block;
}

.theme-dropdown-content a {
    color: var(--text-primary);
    padding: 12px 20px;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.95rem;
    transition: all 0.2s ease;
    cursor: pointer;
    border-left: 3px solid transparent;
    background-color: var(--bg-secondary);
}

.theme-dropdown-content a:hover {
    background-color: var(--bg-hover);
    border-left-color: var(--accent-blue);
}

.theme-dropdown-content a.active {
    background-color: var(--bg-selected);
    color: var(--accent-blue);
    font-weight: 500;
    border-left-color: var(--accent-blue);
}

.theme-dropdown-content {
    animation: slideDown 0.15s ease-out;
}

@keyframes slideDown {
    from {
        opacity: 0;
        transform: translateY(-8px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Layout de duas colunas */
.split-layout {
    display: flex;
    gap: 1.5rem;
    height: calc(100vh - 130px);
    min-height: 600px;
}

/* default flex settings so panels are resizable */
.editor-panel {
    flex: 0 0 40%; /* initial width */
    min-width: 200px;
}
.grid-panel {
    flex: 1; /* fill remaining space */
    min-width: 300px;
}

/* draggable splitters */
.splitter {
    background: transparent;
    z-index: 500;
}
.splitter:hover {
    background: rgba(0,0,0,0.05);
}
.splitter.vertical {
    width: 6px;
    cursor: col-resize;
    margin: 0 -3px; /* overlap neighbors slightly for easier grab */
}

/* highlight applied when text from editor/grid matches */
.selection-highlight {
    background: var(--bg-hover) !important;
    outline: 2px dashed var(--accent-green) !important;
}

/* controls that sit above the sheet grid after preview removal */
.sheet-controls {
    display: flex;
    gap: 0.5rem;
    padding: 0.6rem 1.2rem;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border-secondary);
    border-radius: 0 0 12px 12px;
}

/* Painel esquerdo - Editor */
.editor-panel {
    flex: 1;
    background: var(--bg-secondary);
    border-radius: 20px;
    box-shadow: var(--shadow-lg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--border-secondary);
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

.panel-header {
    padding: 1.2rem 1.8rem;
    border-bottom: 1px solid var(--border-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--bg-tertiary);
}

.panel-header h3 {
    margin: 0;
    color: var(--text-primary);
    font-weight: 600;
    font-size: 1.1rem;
    letter-spacing: -0.01em;
}

.panel-actions {
    display: flex;
    gap: 0.5rem;
}

#jsonEditorTextarea {
    flex: 1;
    width: 100%;
    padding: 1.5rem;
    font-family: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', monospace;
    font-size: 0.95rem;
    border: none;
    resize: none;
    background: var(--bg-secondary);
    transition: background 0.2s, color 0.3s ease;
    line-height: 1.6;
    color: var(--text-primary);
}

#jsonEditorTextarea:focus {
    outline: none;
    background: var(--bg-hover);
}

/* Destaque no JSON */
.json-highlight {
    background: var(--bg-selected);
    color: var(--accent-blue);
    font-weight: 500;
    border-radius: 2px;
}

/* Painel direito - Grid */
.grid-panel {
    flex: 2;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    min-width: 0;
}

/* GRID PRINCIPAL */
.sheet-wrapper {
    z-index: 0;                     /* sit below toolbar/dropdowns */
    background: var(--bg-secondary);
    border-radius: 20px;
    box-shadow: var(--shadow-lg);
    overflow: auto;
    flex: 4 1 auto;
    min-height: 100px;
    border: 1px solid var(--border-secondary);
    position: relative;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

/* Estilizando a scrollbar */
.sheet-wrapper::-webkit-scrollbar {
    width: 12px;
    height: 12px;
}

.sheet-wrapper::-webkit-scrollbar-track {
    background: var(--bg-tertiary);
    border-radius: 10px;
}

.sheet-wrapper::-webkit-scrollbar-thumb {
    background: var(--text-null);
    border-radius: 10px;
    border: 2px solid var(--bg-tertiary);
}

.sheet-wrapper::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

.sheet-grid {
    display: table;
    min-width: 100%;
    background: var(--bg-secondary);
    border-collapse: collapse;
    font-size: 1rem;
    transition: background-color 0.3s ease;
}

/* CABEÇALHO DO GRID */
.sheet-header {
    display: table-header-group;
    background: var(--bg-header);
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 100; /* Reduzido para não competir com dropdown */
    box-shadow: 0 2px 4px rgba(0,0,0,0.02);
}

.sheet-row {
    display: table-row;
    transition: all 0.15s ease;
    border-bottom: 1px solid var(--border-cell);
    position: relative; /* needed for resize handle */
}

.sheet-row::after {
    content: '';
    position: absolute;
    left: 0;
    right: 0;
    bottom: -3px;
    height: 6px;
    cursor: row-resize;
    z-index: 5;
}

.sheet-row:hover {
    background: var(--bg-hover);
}

/* CÉLULAS */
.sheet-cell {
    display: table-cell;
    min-width: 180px;
    padding: 14px 18px;
    border-right: 1px solid var(--border-cell);
    border-bottom: 1px solid var(--border-cell);
    font-size: 1rem;
    background: inherit;
    vertical-align: middle;
    word-break: break-word;
    white-space: normal;
    line-height: 1.5;
    color: var(--text-primary);
    position: relative;
    transition: background-color 0.3s ease, border-color 0.3s ease;
}

/* RESIZE HANDLE */
.col-resizer {
    position: absolute;
    top: 0;
    right: 0;
    width: 6px;
    height: 100%;
    cursor: col-resize;
    z-index: 10;
    background: transparent;
}

/* increase hit area slightly when hovered */
.col-resizer:hover {
    background: rgba(0,0,0,0.05);
}

.sheet-cell:last-child {
    border-right: none;
}

/* Garantir que a célula selecionada tenha destaque */
.sheet-cell.selected {
    background: var(--bg-selected) !important;
    outline: 2px solid var(--accent-blue) !important;
    outline-offset: -2px;
    z-index: 5;
}

/* Para subgrids, garantir que a célula selecionada também seja destacada */
.subgrid-item .sheet-cell.selected {
    background: var(--bg-selected) !important;
    outline: 2px solid var(--accent-blue) !important;
}

/* CABEÇALHO PRINCIPAL */
.sheet-header .sheet-cell {
    background: var(--bg-header);
    color: var(--text-header);
    font-size: 0.9rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    border-bottom: 2px solid var(--border-header);
    padding: 18px 20px;
    white-space: nowrap;
}

/* CÉLULAS EDITÁVEIS */
.sheet-cell[contenteditable="true"], .primitive[contenteditable="true"] {
    background: var(--bg-secondary);
    cursor: text;
}

.sheet-cell[contenteditable="true"]:hover {
    background: var(--bg-hover);
}

.sheet-cell[contenteditable="true"]:focus {
    outline: 2px solid var(--accent-blue);
    outline-offset: -2px;
    background: var(--bg-selected);
}

/* CÉLULAS COM OBJETO (EXPANSÍVEIS) */
.object-cell {
    cursor: pointer;
    background: var(--bg-object);
    color: var(--text-object);
    font-weight: 500;
    position: relative;
    padding-left: 24px;
    display: inline-block;
    border-radius: 4px;
}

.object-cell::before {
    content: '▶';
    position: absolute;
    left: 6px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    color: var(--text-object);
    transition: transform 0.2s ease;
}

.object-cell.expanded::before {
    transform: translateY(-50%) rotate(90deg);
}

/* CÉLULAS COM ARRAY (EXPANSÍVEIS) */
.array-cell {
    cursor: pointer;
    background: var(--bg-array);
    color: var(--text-array);
    font-weight: 500;
    position: relative;
    padding-left: 24px;
    display: inline-block;
    border-radius: 4px;
}

.array-cell::before {
    content: '▶';
    position: absolute;
    left: 6px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 12px;
    color: var(--text-array);
    transition: transform 0.2s ease;
}

.array-cell.expanded::before {
    transform: translateY(-50%) rotate(90deg);
}

/* SUBGRID DENTRO DA CÉLULA */
.subgrid-content {
    margin-top: 12px;
    padding: 8px 0 4px 16px;
    border-left: 3px solid var(--border-header);
    font-size: 0.95rem;
}

.subgrid-item {
    padding: 6px 0;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    border-bottom: 1px dashed var(--border-cell);
}

.subgrid-item:last-child {
    border-bottom: none;
}

.subgrid-key {
    font-weight: 600;
    color: var(--text-secondary);
    min-width: 100px;
    background: var(--bg-tertiary);
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 0.85rem;
    white-space: nowrap;
}

.subgrid-value {
    color: var(--text-primary);
    flex: 1;
    word-break: break-word;
}

/* Aninhamento mais profundo */
.subgrid-content .subgrid-content {
    margin-left: 8px;
    border-left-color: var(--border-primary);
}

.array-marker {
    color: var(--text-array);
    font-weight: 600;
    margin-right: 6px;
}

/* TIPOS DE DADOS */
.number-value {
    color: var(--text-number);
    font-weight: 500;
}

.boolean-value {
    color: var(--text-boolean);
    font-weight: 500;
}

.null-value {
    color: var(--text-null);
    font-style: italic;
}


input[type="file"] {
    display: none;
}

/* Validação */
.validation-message {
    padding: 0.8rem 1.2rem;
    border-radius: 0 0 16px 16px;
    font-size: 0.85rem;
    display: none;
    border-top: 1px solid var(--border-primary);
    font-weight: 500;
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}

.validation-message.success {
    display: block;
    background: #ecfdf5;
    color: #047857;
    border-top-color: #a7f3d0;
}

.validation-message.error {
    display: block;
    background: #fef2f2;
    color: #b91c1c;
    border-top-color: #fecaca;
}

@media (prefers-color-scheme: dark) {
    .validation-message.success {
        background: #022c22;
        color: #4ade80;
        border-top-color: #166534;
    }
    
    .validation-message.error {
        background: #2c0a0a;
        color: #f87171;
        border-top-color: #991b1b;
    }
}

/* Responsivo */
@media (max-width: 1024px) {
    .split-layout {
        flex-direction: column;
        height: auto;
    }
    
    .editor-panel {
        min-height: 300px;
    }
    
    .grid-panel {
        min-height: 500px;
    }
}

@media (max-width: 768px) {
    body {
        padding: 1rem;
        font-size: 14px;
    }
    
    .toolbar {
        padding: 1rem;
    }
    
    .sheet-cell {
        min-width: 140px;
        padding: 10px 14px;
    }
    
    .subgrid-key {
        min-width: 80px;
    }
}

/* Animações */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

.sheet-row {
    animation: fadeIn 0.2s ease-out;
}

.subgrid-content {
    animation: expandDown 0.2s ease-out;
}

@keyframes expandDown {
    from { opacity: 0; transform: translateY(-5px); }
    to { opacity: 1; transform: translateY(0); }
}