/**
 * Spreadsheet Editor Styles
 * Supports both light and dark modes
 */

/* Lock the page to the viewport for sheet notes: the wrapper stops
   scrolling and the editor page becomes a flex column, so the grid takes
   the remaining height and scrolls internally instead of stretching the
   note. */
/* Keep the scrollbar track so switching note types doesn't shift the
   layout; the page content itself is sized to fit, so it never scrolls. */
body.note-type-sheet .editor-content-wrapper {
    overflow-y: scroll;
}

body.note-type-sheet .editor-page {
    max-width: none;
    height: 100%;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 24px 40px 12px;
    box-sizing: border-box;
}

body.note-type-sheet .sheet-editor-container {
    flex: 1 1 auto;
    min-height: 0;
    height: auto !important;
    /* Guard against inline display:block set by editor switching — the
       sheet layout only works as a flex column. */
    display: flex !important;
}

.sheet-editor-container {
    width: 100%;
    height: calc(100vh - 140px);
    display: flex;
    flex-direction: column;
    background: var(--content-bg);
    color: var(--text-primary);
    position: relative;
}

/* ------------------------------------------------------------------ */
/* Toolbar                                                            */
/* ------------------------------------------------------------------ */
.sheet-toolbar {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 0;
    flex-wrap: wrap;
}

.sheet-tool-btn {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.sheet-tool-btn:hover {
    background: var(--hover-bg, #f5f5f5);
    border-color: var(--border-color);
}

body.dark .sheet-tool-btn:hover {
    background: var(--hover-bg, #333);
}

.sheet-toolbar-sep {
    width: 1px;
    height: 20px;
    background: var(--border-color);
    margin: 0 6px;
}

.sheet-toolbar-spacer {
    flex: 1;
}

.sheet-color-btn {
    width: 32px;
    height: 32px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    color: var(--text-primary);
    font-size: 14px;
    border: 1px solid transparent;
    transition: all 0.15s ease;
}

.sheet-color-btn:hover {
    background: var(--hover-bg, #f5f5f5);
    border-color: var(--border-color);
}

.sheet-color-btn input[type="color"] {
    position: absolute;
    inset: 0;
    opacity: 0;
    cursor: pointer;
}

.sheet-export-wrap {
    position: relative;
}

.sheet-export-menu {
    position: absolute;
    top: 100%;
    right: 0;
    z-index: 100;
    min-width: 200px;
    background: var(--content-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    padding: 4px;
}

.sheet-export-menu button {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 8px 12px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 13px;
    text-align: left;
    cursor: pointer;
}

.sheet-export-menu button:hover {
    background: var(--hover-bg, #f5f5f5);
}

body.dark .sheet-export-menu {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
}

body.dark .sheet-export-menu button:hover {
    background: var(--hover-bg, #333);
}

/* ------------------------------------------------------------------ */
/* Find and replace bar                                               */
/* ------------------------------------------------------------------ */
.sheet-find-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 0 8px;
    flex-wrap: wrap;
}

.sheet-find-input,
.sheet-replace-input {
    width: 180px;
    padding: 5px 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 13px;
    color: var(--text-primary);
    background: var(--content-bg);
    outline: none;
}

.sheet-find-input:focus,
.sheet-replace-input:focus {
    border-color: var(--accent-color, #4a90d9);
}

.sheet-find-btn {
    padding: 5px 10px;
    background: var(--content-bg);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.sheet-find-btn:hover {
    background: var(--hover-bg, #f5f5f5);
    border-color: var(--accent-color, #4a90d9);
}

body.dark .sheet-find-btn:hover {
    background: var(--hover-bg, #333);
}

.sheet-find-count {
    font-size: 12px;
    color: var(--text-secondary, #888);
    margin-left: 4px;
}

.sheet-find-close {
    width: 24px;
    height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: var(--text-secondary, #888);
    font-size: 13px;
    cursor: pointer;
    margin-left: auto;
}

.sheet-find-close:hover {
    background: var(--hover-bg, #f5f5f5);
    color: var(--text-primary);
}

body.dark .sheet-find-close:hover {
    background: var(--hover-bg, #333);
}

/* ------------------------------------------------------------------ */
/* Formula bar                                                        */
/* ------------------------------------------------------------------ */
.sheet-formula-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0 8px;
}

.sheet-cell-ref {
    min-width: 60px;
    padding: 5px 8px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 13px;
    font-family: monospace;
    text-align: center;
    color: var(--text-primary);
    background: var(--content-bg);
}

.sheet-fx {
    font-style: italic;
    font-family: serif;
    font-size: 14px;
    color: var(--text-secondary, #888);
}

.sheet-formula-input {
    flex: 1;
    padding: 5px 10px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 13px;
    font-family: monospace;
    color: var(--text-primary);
    background: var(--content-bg);
    outline: none;
}

.sheet-formula-input:focus {
    border-color: var(--accent-color, #4a90d9);
}

/* ------------------------------------------------------------------ */
/* Grid area                                                          */
/* ------------------------------------------------------------------ */
.sheet-grid-area {
    flex: 1;
    min-height: 0;
    position: relative;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    overflow: hidden;
}

.sheet-grid {
    height: 100%;
}

.sheet-grid .jexcel_container {
    height: 100%;
    width: 100%;
}

.sheet-grid .jexcel_content {
    height: 100% !important;
    max-height: 100% !important;
    width: 100% !important;
    overflow: auto;
}

/* ------------------------------------------------------------------ */
/* Loading overlay (shown while a large sheet builds)                 */
/* ------------------------------------------------------------------ */
.sheet-loading-overlay {
    position: absolute;
    inset: 0;
    z-index: 50;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    background: var(--content-bg);
    color: var(--text-secondary, #888);
    font-size: 14px;
}

.sheet-loading-overlay i {
    font-size: 24px;
    color: var(--accent-color, #4a90d9);
}

/* ------------------------------------------------------------------ */
/* Sheet tabs                                                         */
/* ------------------------------------------------------------------ */
.sheet-tabs {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 0 2px;
}

.sheet-add-tab {
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 12px;
    cursor: pointer;
    flex-shrink: 0;
}

.sheet-add-tab:hover {
    background: var(--hover-bg, #f5f5f5);
    border-color: var(--accent-color, #4a90d9);
}

body.dark .sheet-add-tab:hover {
    background: var(--hover-bg, #333);
}

.sheet-tab-list {
    display: flex;
    align-items: center;
    gap: 2px;
    overflow-x: auto;
}

.sheet-tab {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 5px 12px;
    border: 1px solid transparent;
    border-radius: 6px;
    font-size: 13px;
    color: var(--text-secondary, #666);
    cursor: pointer;
    white-space: nowrap;
    user-select: none;
}

.sheet-tab:hover {
    background: var(--hover-bg, #f5f5f5);
}

.sheet-tab.active {
    color: var(--text-primary);
    background: var(--content-bg);
    border-color: var(--border-color);
    font-weight: 600;
}

body.dark .sheet-tab:hover {
    background: var(--hover-bg, #333);
}

.sheet-tab-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: var(--text-secondary, #888);
    font-size: 11px;
    cursor: pointer;
}

.sheet-tab-close:hover {
    color: #d93025;
    background: rgba(217, 48, 37, 0.1);
}

.sheet-tab-rename {
    width: 100px;
    padding: 2px 6px;
    border: 1px solid var(--accent-color, #4a90d9);
    border-radius: 4px;
    font-size: 13px;
    background: var(--content-bg);
    color: var(--text-primary);
    outline: none;
}

/* ------------------------------------------------------------------ */
/* Selection border (drawn on all four sides in both themes; without
   this the theme border-color overrides win on some edges)           */
/* ------------------------------------------------------------------ */
.sheet-grid .jexcel > tbody > tr > td.highlight-top {
    border-top: 1px solid var(--accent-color, #4a90d9) !important;
}

.sheet-grid .jexcel > tbody > tr > td.highlight-right {
    border-right: 1px solid var(--accent-color, #4a90d9) !important;
}

.sheet-grid .jexcel > tbody > tr > td.highlight-bottom {
    border-bottom: 1px solid var(--accent-color, #4a90d9) !important;
}

.sheet-grid .jexcel > tbody > tr > td.highlight-left {
    border-left: 1px solid var(--accent-color, #4a90d9) !important;
}

/* ------------------------------------------------------------------ */
/* Jspreadsheet dark mode overrides                                   */
/* ------------------------------------------------------------------ */
/* jspreadsheet sets a light box-shadow as an inline style on the scroll
   container (tableOverflow mode); it reads as a bright strip along the
   grid edge in dark mode, so it needs !important to beat the inline rule. */
body.dark .sheet-grid .jexcel_content {
    background: var(--content-bg, #1e1e1e);
    box-shadow: none !important;
}

body.dark .sheet-grid .jexcel {
    background: var(--content-bg, #1e1e1e);
    border-color: #3a3a3a;
}

body.dark .sheet-grid .jexcel > tbody > tr > td {
    background: var(--content-bg, #1e1e1e);
    color: var(--text-primary, #e0e0e0);
    border-color: #3a3a3a;
}

body.dark .sheet-grid .jexcel > thead > tr > td,
body.dark .sheet-grid .jexcel > tbody > tr > td:first-child {
    background: #2a2a2a;
    color: var(--text-secondary, #aaa);
    border-color: #3a3a3a;
}

body.dark .sheet-grid .jexcel > thead > tr > td.selected,
body.dark .sheet-grid .jexcel > tbody > tr > td:first-child.selected {
    background: #3a3f4b;
}

body.dark .sheet-grid .jexcel > tbody > tr > td.highlight,
body.dark .sheet-grid .jexcel > tbody > tr > td.highlight-left,
body.dark .sheet-grid .jexcel > tbody > tr > td.highlight-right,
body.dark .sheet-grid .jexcel > tbody > tr > td.highlight-top,
body.dark .sheet-grid .jexcel > tbody > tr > td.highlight-bottom {
    background-color: rgba(74, 144, 217, 0.18);
}

body.dark .sheet-grid .jexcel_corner {
    background-color: var(--accent-color, #4a90d9);
    border-color: var(--content-bg, #1e1e1e);
}

body.dark .sheet-grid .jexcel > tbody > tr > td > input,
body.dark .sheet-grid .jexcel > tbody > tr > td > textarea {
    background: #2a2a2a;
    color: var(--text-primary, #e0e0e0);
}

/* jSuites context menu (right-click) dark mode */
body.dark .jcontextmenu {
    background: #2a2a2a;
    border-color: #3a3a3a;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
}

body.dark .jcontextmenu > div {
    color: #e0e0e0;
}

body.dark .jcontextmenu > div:hover {
    background: #3a3f4b;
}

body.dark .jcontextmenu > div a {
    color: #e0e0e0;
}

/* Read-only (shared) presentation keeps toolbar hidden */
.sheet-editor-container.readonly .sheet-toolbar,
.sheet-editor-container.readonly .sheet-formula-bar {
    display: none;
}

/* ------------------------------------------------------------------ */
/* Mobile                                                             */
/* ------------------------------------------------------------------ */
@media (max-width: 768px) {
    body.note-type-sheet .editor-page {
        padding-left: 8px;
        padding-right: 8px;
    }

    .sheet-editor-container {
        height: calc(100vh - 120px);
    }
}
