:root {
    --primary-color: #3b82f6;
    --primary-hover: #2563eb;
    --secondary-color: #8b5cf6;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --info-color: #3b82f6;
    --dark-bg: #1f2937;
    --dark-card: #374151;
    --dark-text: #f9fafb;
    --light-bg: #f9fafb;
    --light-card: #ffffff;
    --light-text: #1f2937;
    --border-radius: 8px;
    --box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --transition: all 0.3s ease;
    
    /* Spreadsheet specific colors */
    --grid-border: #e5e7eb;
    --grid-bg: #ffffff;
    --header-bg: #f3f4f6;
    --cell-selected: rgba(59, 130, 246, 0.1);
    --cell-highlight: rgba(59, 130, 246, 0.05);
    --toolbar-bg: #f8fafc;
    --formula-bg: #f1f5f9;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, var(--light-bg) 0%, #e0f2fe 100%);
    color: var(--light-text);
    transition: var(--transition);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

body.dark-mode {
    background: linear-gradient(135deg, var(--dark-bg) 0%, #1e3a8a 100%);
    color: var(--dark-text);
    --grid-border: #4b5563;
    --grid-bg: #1f2937;
    --header-bg: #374151;
    --cell-selected: rgba(139, 92, 246, 0.2);
    --cell-highlight: rgba(139, 92, 246, 0.1);
    --toolbar-bg: #1f2937;
    --formula-bg: #111827;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 20px;
    flex: 1;
}

/* Header Styles */
header {
    background-color: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    position: sticky;
    top: 0;
    z-index: 100;
    transition: var(--transition);
}

.dark-mode header {
    background-color: rgba(31, 41, 55, 0.9);
}

.header-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

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

.logo-container a {
    display: flex;
    align-items: center;
    text-decoration: none;
}

.logo {
    height: 40px;
    margin-right: 15px;
}

.site-title {
    font-size: 24px;
    font-weight: 600;
    background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.nav-actions {
    display: flex;
    gap: 15px;
}

#theme-btn {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    color: var(--light-text);
    background-color: #e2e8f0;
}

.dark-mode #theme-btn {
    color: var(--dark-text);
    background-color: #4b5563;
}

#theme-btn:hover {
    background-color: var(--primary-color);
    color: white;
    transform: rotate(30deg);
}

/* Page Title */
.page-title {
    text-align: center;
    margin: 20px 0;
}

.page-title h1 {
    font-size: 36px;
    margin-bottom: 10px;
    background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.page-title p {
    font-size: 18px;
    color: #64748b;
}

.dark-mode .page-title p {
    color: #9ca3af;
}

/* Spreadsheet Styles */
.spreadsheet-container {
    background-color: var(--light-card);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
    margin: 0 auto;
    position: relative;
    display: flex;
    flex-direction: column;
    height: 70vh;
    overflow: hidden;
    min-height: 480px; /* safeguard so grid always has space */
    --grid-font-size: 12px; /* grid zoom baseline */
}

.dark-mode .spreadsheet-container {
    background-color: var(--dark-card);
}

/* Crosshair hover lines */
.hover-line {
    position: absolute;
    background: var(--primary-color);
    opacity: 0;
    transition: opacity 80ms linear;
    pointer-events: none;
    z-index: 5;
}
.hover-line.h { height: 2px; width: 100%; left: 0; }
.hover-line.v { width: 2px; height: 100%; top: 0; }

#spreadsheet {
    flex: 1;
    min-height: 300px; /* grid always visible */
    position: relative;
    cursor: crosshair;
}

.spreadsheet-container .handsontable.ht_master {
    height: 100% !important;
}
.spreadsheet-container .handsontable.ht_master .wtHolder {
    height: 100% !important;
}
.spreadsheet-container .handsontable:not(.ht_master) {
    height: auto !important;
}
.spreadsheet-container .handsontable:not(.ht_master) .wtHolder {
    height: auto !important;
}

/* Toolbar Styles */
.toolbar {
    display: flex;
    padding: 10px;
    background-color: var(--toolbar-bg);
    border-bottom: 1px solid var(--grid-border);
    flex-wrap: wrap;
    gap: 10px;
}

.toolbar-group {
    display: flex;
    gap: 5px;
    padding-right: 15px;
    border-right: 1px solid var(--grid-border);
    margin-right: 15px;
}

.toolbar-group:last-child {
    border-right: none;
}

.toolbar-btn {
    background: none;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--light-text);
    transition: var(--transition);
}

.dark-mode .toolbar-btn {
    color: var(--dark-text);
}

.toolbar-btn:hover {
    background-color: rgba(59, 130, 246, 0.1);
}

.toolbar-btn.active {
    background-color: var(--primary-color);
    color: white;
}

.toolbar-select {
    padding: 5px;
    border-radius: 4px;
    border: 1px solid var(--grid-border);
    background-color: var(--light-card);
    color: var(--light-text);
}

.dark-mode .toolbar-select {
    background-color: var(--dark-card);
    color: var(--dark-text);
}

.color-input {
	width: 36px;
	height: 36px;
	padding: 0;
	border: 1px solid var(--grid-border);
	border-radius: 4px;
	background: none;
	cursor: pointer;
}
.dark-mode .color-input {
	border-color: var(--grid-border);
}

/* Formula Bar */
.formula-bar {
    display: flex;
    align-items: center;
    padding: 8px 10px;
    background-color: var(--formula-bg);
    border-bottom: 1px solid var(--grid-border);
}

.cell-address {
    font-weight: 600;
    padding: 0 10px;
    min-width: 50px;
    text-align: center;
    border-right: 1px solid var(--grid-border);
    margin-right: 10px;
}

#formula-input {
    flex: 1;
    padding: 8px;
    border: 1px solid var(--grid-border);
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    background-color: var(--light-card);
    color: var(--light-text);
}

.dark-mode #formula-input {
    background-color: var(--dark-card);
    color: var(--dark-text);
}

.formula-help-btn {
    background: none;
    border: none;
    color: #64748b;
    font-size: 18px;
    margin-left: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

.formula-help-btn:hover {
    color: var(--primary-color);
}

.formula-hint {
    margin: 6px 0 0;
    font-size: 12px;
    color: #64748b;
    padding: 0 10px;
}

.formula-hint strong {
    color: var(--primary-color);
}

.formula-hint code {
    background: rgba(59, 130, 246, 0.1);
    padding: 1px 4px;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
}

.formula-hint span {
    display: block;
    margin-top: 2px;
    font-size: 12px;
}

.dark-mode .formula-hint {
    color: #9ca3af;
}

.dark-mode .formula-hint code {
    background: rgba(59, 130, 246, 0.2);
}

.formula-function-list {
    list-style: none;
    padding: 0;
    margin: 15px 0 0;
    max-height: 320px;
    overflow-y: auto;
}

.formula-function-list li {
    margin-bottom: 12px;
    border-left: 3px solid transparent;
    padding-left: 8px;
}

.formula-function-list h3 {
    margin: 0 0 4px;
    font-size: 15px;
    color: var(--primary-color);
}

.formula-function-list code {
    background: rgba(59, 130, 246, 0.1);
    padding: 2px 6px;
    border-radius: 4px;
    display: inline-block;
    margin-bottom: 4px;
    font-size: 13px;
}

.formula-function-list p {
    margin: 0;
    font-size: 13px;
    color: #475569;
}

.dark-mode .formula-function-list p {
    color: #cbd5f5;
}

.formula-function-list li.custom-function-entry {
    border-left-color: var(--primary-color);
}

.formula-search {
    width: 100%;
    padding: 8px;
    border: 1px solid var(--grid-border);
    border-radius: 4px;
    margin-top: 10px;
}

.function-help {
    max-width: 520px;
}

.custom-function-modal {
    max-width: 640px;
}

.custom-function-form .form-group {
    display: flex;
    flex-direction: column;
    margin-bottom: 12px;
    gap: 4px;
}

.custom-function-form input,
.custom-function-form textarea {
    border: 1px solid var(--grid-border);
    border-radius: 4px;
    padding: 8px;
    font-family: 'Courier New', Courier, monospace;
}

.custom-func-actions {
    display: flex;
    gap: 10px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.custom-function-list {
    list-style: none;
    margin: 15px 0 0;
    padding: 0;
    max-height: 260px;
    overflow-y: auto;
}

.custom-function-list li {
    border: 1px solid var(--grid-border);
    border-radius: 6px;
    padding: 10px;
    margin-bottom: 10px;
    background: rgba(148, 163, 184, 0.1);
}

.dark-mode .custom-function-list li {
    background: rgba(71, 85, 105, 0.3);
}

.custom-function-list h3 {
    margin: 0 0 4px;
    font-size: 15px;
    color: var(--primary-color);
}

.custom-function-list code {
    display: block;
    margin-bottom: 6px;
    background: rgba(59, 130, 246, 0.1);
    padding: 4px 6px;
    border-radius: 4px;
}

.custom-function-list p {
    margin: 0 0 8px;
    font-size: 13px;
    color: #475569;
}

.custom-function-list .custom-func-actions {
    justify-content: flex-end;
}

/* button utility styles for custom functions modal */
.custom-function-modal .primary-btn,
.custom-function-modal .secondary-btn,
.custom-function-modal .danger-btn {
    border: none;
    border-radius: 4px;
    padding: 8px 14px;
    cursor: pointer;
    font-weight: 600;
    width: auto;
    margin-top: 0;
}

.custom-function-modal .primary-btn {
    background: var(--primary-color);
    color: #fff;
}

.custom-function-modal .secondary-btn {
    background: rgba(59, 130, 246, 0.12);
    color: var(--primary-color);
}

.custom-function-modal .danger-btn {
    background: rgba(239, 68, 68, 0.15);
    color: #dc2626;
}

.custom-function-modal .primary-btn:hover {
    opacity: 0.9;
}

.custom-function-modal .secondary-btn:hover {
    background: rgba(59, 130, 246, 0.2);
}

.custom-function-modal .danger-btn:hover {
    background: rgba(239, 68, 68, 0.25);
}

.dark-mode .custom-function-modal .secondary-btn {
    background: rgba(96, 165, 250, 0.2);
    color: #93c5fd;
}

.dark-mode .custom-function-modal .danger-btn {
    background: rgba(239, 68, 68, 0.25);
    color: #fca5a5;
}

/* Spreadsheet */
#spreadsheet {
    flex: 1;
    overflow: hidden;
}

/* Customize Handsontable */
.handsontable .htCore th {
    background-color: var(--header-bg);
    color: var(--light-text);
    font-weight: 600;
}

.dark-mode .handsontable .htCore th {
    color: var(--dark-text);
}

.handsontable .htCore td {
    border-color: var(--grid-border);
    color: var(--light-text);
}

.dark-mode .handsontable .htCore td {
    color: var(--dark-text);
}

.handsontable .htCore td.current {
    background-color: var(--cell-selected);
}

.handsontable .htCore td.area {
    background-color: var(--cell-highlight);
}

/* Awards highlight cells */
.handsontable .htCore td.champion-cell {
    background: linear-gradient(135deg, #fff3bf, #ffe08a);
    font-weight: 700;
}

.handsontable .htCore td.runnerup-cell {
    background: linear-gradient(135deg, #e5e7eb, #cbd5e1);
    font-weight: 600;
}

.handsontable .htCore td.third-cell {
    background: linear-gradient(135deg, #f5e3d7, #f1c7a0);
    font-weight: 600;
}

/* Sheet tabs */
.tab-container {
    display: flex;
    border-top: 1px solid var(--grid-border);
    background-color: var(--toolbar-bg);
    padding: 0 10px;
}

.sheet-tabs {
    display: flex;
    overflow-x: auto;
    scrollbar-width: thin;
}

.sheet-tabs::-webkit-scrollbar {
    height: 5px;
}

.sheet-tabs::-webkit-scrollbar-track {
    background: var(--toolbar-bg);
}

.sheet-tabs::-webkit-scrollbar-thumb {
    background-color: var(--grid-border);
    border-radius: 10px;
}

.sheet-tab {
    padding: 8px 15px;
    border: none;
    background: none;
    cursor: pointer;
    border-right: 1px solid var(--grid-border);
    color: var(--light-text);
    white-space: nowrap;
}

.dark-mode .sheet-tab {
    color: var(--dark-text);
}

.sheet-tab.active {
    background-color: var(--light-card);
    font-weight: 600;
    color: var(--primary-color);
}

.dark-mode .sheet-tab.active {
    background-color: var(--dark-card);
}

.add-sheet-btn {
    padding: 8px;
    border: none;
    background: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary-color);
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 200;
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: var(--light-card);
    padding: 20px;
    border-radius: var(--border-radius);
    max-width: 500px;
    width: 90%;
    position: relative;
    box-shadow: var(--box-shadow);
}

.dark-mode .modal-content {
    background-color: var(--dark-card);
}

.close-modal {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 24px;
    cursor: pointer;
    color: #64748b;
}

.dark-mode .close-modal {
    color: #9ca3af;
}

.modal h2 {
    margin-bottom: 20px;
    color: var(--primary-color);
}

.form-group {
    margin-bottom: 15px;
}

.form-group label {
    display: block;
    margin-bottom: 5px;
    font-weight: 600;
}

.form-group input[type="text"],
.form-group select {
    width: 100%;
    padding: 8px;
    border: 1px solid var(--grid-border);
    border-radius: 4px;
    background-color: var(--light-card);
    color: var(--light-text);
}

.dark-mode .form-group input[type="text"],
.dark-mode .form-group select {
    background-color: var(--dark-bg);
    color: var(--dark-text);
}

.primary-btn {
    background-color: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 15px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 600;
    width: 100%;
    margin-top: 10px;
    transition: var(--transition);
}

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

/* Awards preview */
.awards-preview {
    margin: 15px 0;
    padding: 10px;
    background-color: rgba(59, 130, 246, 0.1);
    border-radius: var(--border-radius);
}

.awards-preview h3 {
    margin-bottom: 10px;
    font-size: 16px;
}

.award-item {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
}

.award-icon {
    display: inline-flex;
    width: 30px;
    height: 30px;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    margin-right: 10px;
    color: white;
}

.gold {
    background: linear-gradient(135deg, #ffd700, #f59e0b);
}

.silver {
    background: linear-gradient(135deg, #c0c0c0, #94a3b8);
}

.bronze {
    background: linear-gradient(135deg, #cd7f32, #a16207);
}

/* Chart Container */
.chart-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 400px;
    height: 300px;
    background-color: var(--light-card);
    border-radius: var(--border-radius);
    box-shadow: var(--box-shadow);
    z-index: 50;
    padding: 15px;
    display: flex;
    flex-direction: column;
}

.dark-mode .chart-container {
    background-color: var(--dark-card);
}

.chart-container.hidden {
    display: none;
}

.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.chart-header h3 {
    color: var(--primary-color);
}

.close-chart-btn {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    color: #64748b;
}

.dark-mode .close-chart-btn {
    color: #9ca3af;
}

/* Footer styles */
.footer {
    margin-top: 40px;
    padding: 20px 0;
    text-align: center;
    border-top: 1px solid #e2e8f0;
    background-color: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(10px);
    transition: var(--transition);
}

.dark-mode .footer {
    background-color: rgba(31, 41, 55, 0.8);
    border-top-color: #4b5563;
}

.footer-content {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
}

.footer-links {
    line-height: 1.6;
    color: #64748b;
}

.dark-mode .footer-links {
    color: #9ca3af;
}

.footer a {
    color: var(--primary-color);
    text-decoration: none;
    transition: var(--transition);
}

.footer a:hover {
    color: var(--primary-hover);
    text-decoration: underline;
}

/* Responsive Design */
@media (max-width: 768px) {
    .toolbar {
        padding: 5px;
    }
    
    .toolbar-group {
        margin-right: 5px;
        padding-right: 5px;
    }
    
    .toolbar-btn {
        width: 32px;
        height: 32px;
    }
    
    .chart-container {
        width: 80%;
        left: 10%;
        right: 10%;
    }
    
    .page-title h1 {
        font-size: 28px;
    }
    
    .page-title p {
        font-size: 16px;
    }
}

@media (max-width: 480px) {
    .toolbar-group:not(.file-operations):not(.special) {
        display: none;
    }
    
    .formula-bar {
        flex-direction: column;
        align-items: flex-start;
    }
    
    .cell-address {
        width: 100%;
        text-align: left;
        margin-bottom: 5px;
        border-right: none;
        padding: 0;
    }
    
    #formula-input {
        width: 100%;
    }
}

/* Panning cursor when Space+drag */
.spreadsheet-container.panning, 
.spreadsheet-container.panning * {
    cursor: grabbing !important;
}

/* Subtle row hover */
.handsontable .htCore tbody tr:hover td {
    background-color: var(--cell-highlight);
}

/* Header hover highlight */
.handsontable .htCore thead th.hover-header,
.handsontable .htCore tbody th.hover-header {
    background-color: var(--header-bg);
    box-shadow: inset 0 -2px 0 var(--primary-color);
    font-weight: 600;
}

/* Zoomable font size */
.handsontable .htCore td,
.handsontable .htCore th {
    font-size: var(--grid-font-size, 12px);
}
