* { margin:0; padding:0; box-sizing:border-box; }
:root {
  --ribbon-bg: #f3f3f3;
  --tab-bg: #e8e8e8;
  --active-tab: #fff;
  --border: #d1d1d1;
  --header-bg: #f0f0f0;
  --cell-border: #d4d4d4;
  --select-blue: #0078d4;
  --select-bg: rgba(0,120,212,0.08);
  --hover-bg: rgba(0,0,0,0.04);
  --grid-bg: #fff;
  --text: #1a1a1a;
  --toolbar-h: 120px;
  --formula-h: 28px;
  --sheet-h: 26px;
  --status-h: 24px;
  --title-h: 32px;
  --col-header-h: 20px;
  --row-header-w: 40px;
  --cell-h: 21px;
  --cell-w: 80px;
}

body {
  font-family: 'Segoe UI', Calibri, Arial, sans-serif;
  font-size: 12px;
  color: var(--text);
  overflow: hidden;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: #fff;
  user-select: none;
}

/* Title Bar */
.title-bar {
  height: var(--title-h);
  background: #217346;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  font-size: 12px;
}
.title-bar-left { display: flex; align-items: center; gap: 8px; }
.app-icon { font-size: 16px; }
.autosave { font-size: 10px; opacity: 0.8; background: rgba(255,255,255,0.15); padding: 1px 6px; border-radius: 3px; }
.file-name { outline: none; padding: 1px 4px; border-radius: 2px; min-width: 40px; }
.file-name:focus { background: rgba(255,255,255,0.2); }
.title-bar-center { font-size: 12px; opacity: 0.9; }
.title-bar-right { display: flex; }
.title-btn {
  background: none; border: none; color: #fff; width: 46px; height: 32px;
  cursor: pointer; font-size: 13px; display: flex; align-items: center; justify-content: center;
}
.title-btn:hover { background: rgba(255,255,255,0.1); }
.close-btn:hover { background: #e81123 !important; }

/* Ribbon Tabs */
.ribbon-tabs {
  display: flex;
  background: var(--ribbon-bg);
  border-bottom: 1px solid var(--border);
  padding: 0 4px;
}
.ribbon-tabs .tab {
  padding: 5px 14px;
  cursor: pointer;
  font-size: 12px;
  color: #444;
  border-bottom: 2px solid transparent;
  transition: all 0.15s;
}
.ribbon-tabs .tab:hover { background: var(--hover-bg); }
.ribbon-tabs .tab.active {
  color: #217346;
  border-bottom-color: #217346;
  background: #fff;
}

/* Ribbon */
.ribbon {
  background: var(--ribbon-bg);
  border-bottom: 1px solid var(--border);
  min-height: 88px;
  display: none;
  padding: 2px 4px;
}
.ribbon-tab-content { display: none; align-items: stretch; gap: 0; height: 84px; }
.ribbon-tab-content.active { display: flex; }

.ribbon-group {
  display: flex;
  flex-direction: column;
  border-right: 1px solid #e0e0e0;
  padding: 2px 8px;
  min-width: 60px;
}
.ribbon-group:last-child { border-right: none; }
.ribbon-group-label {
  font-size: 10px;
  color: #666;
  text-align: center;
  padding: 0 2px;
  margin-top: auto;
}
.ribbon-group-content {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: 1;
  flex-wrap: wrap;
}

.ribbon-btn {
  background: transparent;
  border: 1px solid transparent;
  padding: 3px 8px;
  cursor: pointer;
  font-size: 11px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  gap: 3px;
  white-space: nowrap;
  color: var(--text);
}
.ribbon-btn:hover {
  background: rgba(0,120,212,0.08);
  border-color: rgba(0,120,212,0.2);
}
.ribbon-btn:active { background: rgba(0,120,212,0.15); }
.ribbon-btn.small {
  padding: 3px 5px;
  min-width: 26px;
  justify-content: center;
}
.btn-icon { font-size: 14px; }
.btn-label { font-size: 11px; }
.paste-btn { flex-direction: column; padding: 4px 12px; }
.clipboard-sub { display: flex; flex-direction: column; gap: 1px; }
.font-group { flex-wrap: wrap; }
.font-btns { display: flex; gap: 2px; align-items: center; }
.font-select { width: 120px; height: 22px; font-size: 11px; border: 1px solid #ccc; border-radius: 2px; }
.size-select { width: 48px; height: 22px; font-size: 11px; border: 1px solid #ccc; border-radius: 2px; }
.format-select { height: 22px; font-size: 11px; border: 1px solid #ccc; border-radius: 2px; }
.color-btn { position: relative; display: flex; flex-direction: column; align-items: center; }
.color-bar { width: 18px; height: 3px; margin-top: -2px; }

/* Formula Bar */
.formula-bar {
  height: var(--formula-h);
  display: flex;
  align-items: center;
  border-bottom: 1px solid var(--border);
  background: #fff;
}
.name-box {
  width: 72px;
  text-align: center;
  font-size: 11px;
  padding: 2px 4px;
  border-right: 1px solid var(--border);
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.formula-sep { color: #ccc; padding: 0 2px; }
.fx-label { color: #217346; font-style: italic; padding: 0 6px; font-weight: 600; font-size: 12px; }
.formula-input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 11px;
  padding: 2px 6px;
  font-family: Calibri, Arial, sans-serif;
}
.formula-input:focus { background: #fffde7; }

/* Main Area */
.main-area {
  flex: 1;
  position: relative;
  overflow: hidden;
}

.corner-cell {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--row-header-w);
  height: var(--col-header-h);
  background: var(--header-bg);
  border-right: 1px solid var(--cell-border);
  border-bottom: 1px solid var(--cell-border);
  z-index: 3;
}

.col-header-container {
  position: absolute;
  top: 0;
  left: var(--row-header-w);
  right: 0;
  height: var(--col-header-h);
  overflow: hidden;
  z-index: 2;
}
.col-headers {
  display: flex;
  height: 100%;
}
.col-header {
  min-width: var(--cell-w);
  height: 100%;
  background: var(--header-bg);
  border-right: 1px solid var(--cell-border);
  border-bottom: 1px solid var(--cell-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: #555;
  cursor: pointer;
  position: relative;
}
.col-header:hover { background: #e0e0e0; }
.col-header.selected { background: #d0e8ff; color: #000; }
.col-header .resize-handle {
  position: absolute;
  right: -3px;
  top: 0;
  bottom: 0;
  width: 6px;
  cursor: col-resize;
  z-index: 5;
}

.row-header-container {
  position: absolute;
  top: var(--col-header-h);
  left: 0;
  bottom: 0;
  width: var(--row-header-w);
  overflow: hidden;
  z-index: 2;
}
.row-headers { position: relative; }
.row-header {
  height: var(--cell-h);
  background: var(--header-bg);
  border-right: 1px solid var(--cell-border);
  border-bottom: 1px solid var(--cell-border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  color: #555;
  cursor: pointer;
  position: absolute;
  left: 0;
  width: 100%;
}
.row-header:hover { background: #e0e0e0; }
.row-header.selected { background: #d0e8ff; color: #000; }

.grid-container {
  position: absolute;
  top: var(--col-header-h);
  left: var(--row-header-w);
  right: 0;
  bottom: 0;
  overflow: auto;
  background: var(--grid-bg);
  will-change: transform;
}
.grid {
  position: relative;
  contain: layout style;
}
.cell {
  position: absolute;
  border-right: 1px solid var(--cell-border);
  border-bottom: 1px solid var(--cell-border);
  padding: 0 3px;
  font-size: 11px;
  font-family: Calibri, Arial, sans-serif;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  cursor: cell;
  height: var(--cell-h);
  contain: layout style;
}
.cell.selected { background: var(--select-bg) !important; }
.cell.in-range { background: rgba(0,120,212,0.04); }
.cell.editing { overflow: visible; }
.cell.error { color: #c00; }

.selection-box {
  position: absolute;
  border: 2px solid var(--select-blue);
  pointer-events: none;
  z-index: 4;
  display: none;
}
.fill-handle {
  position: absolute;
  width: 8px;
  height: 8px;
  background: var(--select-blue);
  border: 1px solid #fff;
  cursor: crosshair;
  z-index: 5;
  display: none;
}

.cell-editor {
  position: absolute;
  z-index: 10;
  min-width: 80px;
}
.cell-editor textarea {
  width: 100%;
  min-height: 21px;
  border: 2px solid var(--select-blue);
  outline: none;
  font-size: 11px;
  font-family: Calibri, Arial, sans-serif;
  padding: 0 3px;
  resize: none;
  background: #fff;
  box-shadow: 0 0 0 1px rgba(0,120,212,0.3);
}

/* Sheet Tabs */
.sheet-bar {
  height: var(--sheet-h);
  display: flex;
  align-items: center;
  background: #e8e8e8;
  border-top: 1px solid var(--border);
}
.sheet-nav { display: flex; padding: 0 2px; }
.sheet-nav-btn {
  background: none;
  border: none;
  width: 20px;
  height: 24px;
  cursor: pointer;
  font-size: 10px;
  color: #666;
}
.sheet-nav-btn:hover { background: rgba(0,0,0,0.06); }
.sheet-tabs {
  display: flex;
  overflow: hidden;
  flex: 1;
}
.sheet-tab {
  padding: 3px 16px;
  cursor: pointer;
  font-size: 11px;
  border-right: 1px solid #ccc;
  display: flex;
  align-items: center;
  gap: 4px;
  white-space: nowrap;
  background: #e8e8e8;
}
.sheet-tab:hover { background: #ddd; }
.sheet-tab.active {
  background: #fff;
  border-top: 2px solid #217346;
  font-weight: 600;
}
.sheet-tab .tab-close {
  font-size: 10px;
  opacity: 0;
  cursor: pointer;
  width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 2px;
}
.sheet-tab:hover .tab-close { opacity: 0.5; }
.sheet-tab .tab-close:hover { opacity: 1; background: rgba(0,0,0,0.1); }
.add-sheet-btn {
  background: none;
  border: none;
  width: 28px;
  height: 24px;
  cursor: pointer;
  font-size: 14px;
  color: #666;
}
.add-sheet-btn:hover { background: rgba(0,0,0,0.06); }

/* Status Bar */
.status-bar {
  height: var(--status-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 8px;
  background: #217346;
  color: #fff;
  font-size: 11px;
}
.status-left { min-width: 100px; }
.status-center { font-size: 11px; opacity: 0.9; }
.status-right { display: flex; align-items: center; gap: 8px; }
.zoom-slider { width: 100px; }
.zoom-slider input[type=range] { width: 100%; height: 3px; accent-color: #fff; }
.view-btns { display: flex; gap: 2px; }
.view-btn {
  background: none; border: none; color: #fff; opacity: 0.6;
  cursor: pointer; font-size: 12px; padding: 2px 6px; border-radius: 2px;
}
.view-btn:hover { opacity: 1; background: rgba(255,255,255,0.1); }
.view-btn.active { opacity: 1; background: rgba(255,255,255,0.2); }

/* Context Menu */
.context-menu {
  position: fixed;
  background: #fff;
  border: 1px solid #ccc;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  border-radius: 4px;
  padding: 4px 0;
  z-index: 1000;
  min-width: 200px;
}
.ctx-item {
  padding: 5px 24px 5px 12px;
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.ctx-item:hover { background: #e8f0fe; }
.ctx-icon { font-size: 12px; width: 16px; text-align: center; }
.ctx-key { margin-left: auto; font-size: 10px; color: #888; }
.ctx-sep { height: 1px; background: #e0e0e0; margin: 4px 8px; }

/* Dialogs */
.dialog-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.3);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dialog {
  background: #fff;
  border-radius: 6px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.2);
  min-width: 380px;
  max-width: 500px;
}
.dialog-title {
  padding: 10px 16px;
  font-size: 13px;
  font-weight: 600;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.dialog-close {
  cursor: pointer;
  font-size: 16px;
  color: #888;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
}
.dialog-close:hover { background: #f0f0f0; }
.dialog-body { padding: 12px 16px; }
.dialog-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.dialog-row label { min-width: 80px; font-size: 12px; }
.dialog-input {
  flex: 1;
  height: 26px;
  border: 1px solid #ccc;
  border-radius: 3px;
  padding: 0 6px;
  font-size: 12px;
  outline: none;
}
.dialog-input:focus { border-color: var(--select-blue); }
.dialog-footer {
  padding: 8px 16px;
  border-top: 1px solid #e0e0e0;
  display: flex;
  justify-content: flex-end;
  gap: 6px;
}
.dialog-btn {
  padding: 5px 16px;
  border: 1px solid #ccc;
  border-radius: 3px;
  background: #fff;
  cursor: pointer;
  font-size: 12px;
}
.dialog-btn:hover { background: #f0f0f0; }
.dialog-btn.primary {
  background: var(--select-blue);
  color: #fff;
  border-color: var(--select-blue);
}
.dialog-btn.primary:hover { background: #106ebe; }

.dialog-tabs { display: flex; border-bottom: 1px solid #e0e0e0; }
.dtab {
  padding: 6px 14px;
  cursor: pointer;
  font-size: 11px;
  border-bottom: 2px solid transparent;
}
.dtab:hover { background: #f0f0f0; }
.dtab.active { color: var(--select-blue); border-bottom-color: var(--select-blue); }
.dtab-content { display: none; padding: 8px 0; }
.dtab-content.active { display: block; }

.border-tools { display: flex; gap: 4px; margin-bottom: 8px; flex-wrap: wrap; }
.border-btn {
  padding: 4px 10px;
  border: 1px solid #ccc;
  background: #fff;
  cursor: pointer;
  font-size: 11px;
  border-radius: 2px;
}
.border-btn:hover { background: #e8f0fe; }

.fmt-preview {
  background: #f8f8f8;
  border: 1px solid #ddd;
  padding: 8px;
  text-align: right;
  font-size: 12px;
  min-height: 32px;
}

.func-list {
  max-height: 180px;
  overflow-y: auto;
  border: 1px solid #ddd;
  margin: 4px 0;
}
.func-item {
  padding: 3px 8px;
  cursor: pointer;
  font-size: 11px;
}
.func-item:hover { background: #e8f0fe; }
.func-item.selected { background: var(--select-blue); color: #fff; }
.func-desc {
  font-size: 11px;
  color: #666;
  padding: 4px;
  min-height: 20px;
}

/* Chart */
.chart-container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #fff;
  border: 1px solid #ccc;
  border-radius: 6px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.2);
  padding: 16px;
  z-index: 50;
  min-width: 500px;
  min-height: 350px;
}
.chart-title { font-size: 14px; font-weight: 600; margin-bottom: 8px; text-align: center; }
.chart-close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: #888;
}
.chart-close:hover { color: #000; }

/* Toast */
.toast {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 8px 20px;
  border-radius: 4px;
  font-size: 12px;
  z-index: 200;
  animation: fadeIn 0.2s;
}
@keyframes fadeIn { from { opacity: 0; transform: translateX(-50%) translateY(10px); } to { opacity: 1; transform: translateX(-50%) translateY(0); } }

/* Scrollbar */
.grid-container::-webkit-scrollbar { width: 12px; height: 12px; }
.grid-container::-webkit-scrollbar-track { background: #f0f0f0; }
.grid-container::-webkit-scrollbar-thumb { background: #c1c1c1; border-radius: 6px; border: 3px solid #f0f0f0; }
.grid-container::-webkit-scrollbar-thumb:hover { background: #a8a8a8; }
.grid-container::-webkit-scrollbar-corner { background: #f0f0f0; }

/* Filter dropdown */
.filter-icon {
  position: absolute;
  right: 2px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 8px;
  cursor: pointer;
  opacity: 0.5;
}
.filter-icon:hover { opacity: 1; }

/* Comment indicator */
.comment-indicator {
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-top: 6px solid #f00;
}

/* Freeze pane lines */
.freeze-line-h {
  position: absolute;
  left: 0;
  right: 0;
  height: 2px;
  background: #333;
  z-index: 6;
}
.freeze-line-v {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #333;
  z-index: 6;
}
