:root {
  --primary: #1a73e8;
  --primary-light: #d3e3fd;
  --primary-bg: rgba(26,115,232,0.1);
  --border: #d4d4d4;
  --border-light: #e0e0e0;
  --header-bg: #f8f9fa;
  --toolbar-bg: #f3f3f3;
  --text: #202124;
  --text-sec: #5f6368;
  --surface: #fff;
  --danger: #d93025;
  --radius: 4px;
  --shadow: 0 2px 8px rgba(0,0,0,.15);
  --shadow-lg: 0 8px 24px rgba(0,0,0,.18);
  --row-header-w: 40px;
  --col-header-h: 24px;
  --cell-font: 11px;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--surface);
  height: 100vh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  position: relative;
}

/* ===== Menu Bar ===== */
.menu-bar {
  display: flex;
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  height: 32px;
  padding: 0 4px;
  user-select: none;
  position: relative;
  z-index: 100;
}

.menu-item {
  position: relative;
}

.menu-label {
  display: inline-flex;
  align-items: center;
  height: 32px;
  padding: 0 12px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  border-radius: var(--radius);
  transition: background .1s;
}
.menu-label:hover { background: rgba(0,0,0,.06); }
.menu-item.open .menu-label { background: rgba(0,0,0,.1); }

.menu-dropdown {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 240px;
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 4px 0;
  z-index: 1000;
}
.menu-item.open .menu-dropdown { display: block; }

.menu-action {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 7px 16px;
  border: none;
  background: none;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  transition: background .1s;
}
.menu-action:hover { background: var(--primary-bg); }

.menu-shortcut {
  font-size: 11px;
  color: var(--text-sec);
  margin-left: 24px;
}

.menu-divider {
  height: 1px;
  background: var(--border-light);
  margin: 4px 0;
}

/* ===== Toolbar ===== */
.toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px;
  padding: 4px 8px;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--border);
  min-height: 36px;
  user-select: none;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 4px;
  border-right: 1px solid var(--border-light);
}

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

.toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 28px;
  padding: 0 6px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: none;
  cursor: pointer;
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  transition: background .1s;
}

.toolbar-btn:hover { background: rgba(0,0,0,.08); }
.toolbar-btn:active, .toolbar-btn.active {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary);
}

.toolbar-btn-primary {
  background: var(--primary);
  color: white;
  font-weight: 500;
  padding: 0 12px;
}
.toolbar-btn-primary:hover { background: #1765cc; }

.toolbar-select {
  height: 28px;
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 12px;
  cursor: pointer;
  max-width: 120px;
}
.toolbar-select-sm { width: 55px; }

.toolbar-color {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  font-size: 14px;
  font-weight: bold;
  padding: 2px 4px;
  border-radius: var(--radius);
}
.toolbar-color:hover { background: rgba(0,0,0,.08); }
.toolbar-color input[type="color"] {
  width: 16px;
  height: 16px;
  border: none;
  padding: 0;
  cursor: pointer;
  background: none;
}

/* ===== Formula Bar ===== */
.formula-bar {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  height: 28px;
}

.name-box {
  width: 80px;
  min-width: 60px;
  height: 22px;
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: 2px;
  text-align: center;
  font-size: 12px;
  font-weight: 500;
}
.name-box:focus { border-color: var(--primary); outline: none; }

.formula-icon {
  font-style: italic;
  font-weight: 600;
  color: var(--text-sec);
  font-size: 12px;
  padding: 0 4px;
}

.formula-input {
  flex: 1;
  height: 22px;
  padding: 0 6px;
  border: 1px solid var(--border);
  border-radius: 2px;
  font-family: 'Courier New', monospace;
  font-size: 12px;
}
.formula-input:focus { border-color: var(--primary); outline: none; }

/* ===== Grid Container ===== */
.grid-container {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--surface);
}

.grid-scroll {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  overflow: auto;
}

.grid-scroll::-webkit-scrollbar { width: 10px; height: 10px; }
.grid-scroll::-webkit-scrollbar-track { background: var(--toolbar-bg); }
.grid-scroll::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 5px;
  border: 2px solid var(--toolbar-bg);
}
.grid-scroll::-webkit-scrollbar-thumb:hover { background: #a0a0a0; }
.grid-scroll::-webkit-scrollbar-corner { background: var(--toolbar-bg); }

.grid-canvas {
  position: relative;
  min-width: 100%;
  min-height: 100%;
}

.grid-scroll-sizer { pointer-events: none; }

.grid-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  overflow: hidden;
}

.grid-overlay > * { pointer-events: auto; }

/* ===== Grid Cells ===== */
.grid-cell {
  position: absolute;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 2px 4px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  cursor: cell;
  font-size: var(--cell-font);
  font-family: Arial, sans-serif;
  line-height: 1.4;
  background: var(--surface);
}

.grid-cell.cell-error { color: var(--danger); }
.grid-cell.cell-has-comment::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 0;
  height: 0;
  border-left: 6px solid transparent;
  border-top: 6px solid #f4b400;
}
.grid-cell .cell-link {
  color: #1a73e8;
  text-decoration: underline;
  cursor: pointer;
}
.grid-cell .cell-link:hover {
  color: #174ea6;
}

/* ===== Headers ===== */
.col-header, .row-header {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--header-bg);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  font-size: 11px;
  font-weight: 500;
  color: var(--text-sec);
  user-select: none;
  cursor: default;
}

.col-header:hover, .row-header:hover { background: #e8eaed; }
.col-header.header-selected, .row-header.header-selected {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 600;
}

.grid-corner {
  position: absolute;
  background: var(--header-bg);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}

.resize-handle {
  position: absolute;
  z-index: 5;
}
.col-header .resize-handle {
  right: -2px; top: 0; width: 4px; height: 100%;
  cursor: col-resize;
}
.row-header .resize-handle {
  bottom: -2px; left: 0; height: 4px; width: 100%;
  cursor: row-resize;
}

/* ===== Selection ===== */
.selection-box {
  position: absolute;
  border: 2px solid var(--primary);
  pointer-events: none;
  z-index: 10;
}

.range-highlight {
  position: absolute;
  background: var(--primary-bg);
  border: 1px solid var(--primary);
  pointer-events: none;
  z-index: 9;
}

.fill-handle {
  position: absolute;
  width: 8px; height: 8px;
  background: var(--primary);
  border: 1px solid var(--surface);
  cursor: crosshair;
  z-index: 11;
  pointer-events: auto;
}

/* ===== Cell Editor ===== */
.cell-editor {
  position: absolute;
  z-index: 20;
  padding: 2px 4px;
  font-family: Arial, sans-serif;
  font-size: var(--cell-font);
  border: none;
  outline: 2px solid var(--primary);
  resize: none;
  overflow: hidden;
  background: var(--surface);
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
  line-height: 1.4;
}

/* ===== Sheet Tabs ===== */
.sheet-tab-bar {
  display: flex;
  align-items: center;
  background: var(--toolbar-bg);
  border-top: 1px solid var(--border);
  height: 30px;
  padding: 0 4px;
  overflow-x: auto;
}

.sheet-tabs {
  display: flex;
  align-items: center;
  gap: 0;
  height: 100%;
}

.sheet-tab {
  display: flex;
  align-items: center;
  height: 28px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-bottom: none;
  border-radius: 4px 4px 0 0;
  background: var(--surface);
  cursor: pointer;
  font-size: 12px;
  color: var(--text-sec);
  white-space: nowrap;
  margin-right: -1px;
  transition: background .1s;
}

.sheet-tab:hover { background: #e8eaed; }
.sheet-tab.active {
  background: var(--surface);
  color: var(--primary);
  font-weight: 600;
  border-bottom: 2px solid var(--primary);
  margin-bottom: -1px;
}

.btn-add-sheet {
  width: 28px; height: 28px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: none;
  cursor: pointer;
  font-size: 16px;
  color: var(--text-sec);
  margin-left: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-add-sheet:hover { background: rgba(0,0,0,.08); }

/* ===== Status Bar ===== */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 22px;
  padding: 0 12px;
  background: var(--toolbar-bg);
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-sec);
  user-select: none;
}

.status-stats { font-weight: 500; }

/* ===== Context Menu ===== */
.context-menu {
  position: fixed;
  z-index: 1000;
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 4px 0;
  min-width: 200px;
}

.context-menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px;
  cursor: pointer;
  font-size: 13px;
  transition: background .1s;
}
.context-menu-item:hover { background: var(--primary-bg); }

.context-menu-shortcut {
  color: var(--text-sec);
  font-size: 11px;
  margin-left: 24px;
}

.context-menu-divider {
  height: 1px;
  background: var(--border-light);
  margin: 4px 0;
}

/* ===== Dialogs ===== */
.dialog {
  position: fixed;
  z-index: 900;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-lg);
  min-width: 320px;
  max-width: 90vw;
}

.find-dialog {
  top: 60px; right: 20px;
}

.format-dialog-panel {
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
}

.chart-dialog-panel {
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 440px;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border-light);
  font-weight: 600;
  font-size: 14px;
  cursor: move;
}

.dialog-close {
  width: 24px; height: 24px;
  border: none;
  background: none;
  font-size: 18px;
  cursor: pointer;
  color: var(--text-sec);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dialog-close:hover { background: rgba(0,0,0,.08); }

.dialog-body {
  padding: 16px;
}

.dialog-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.dialog-row label { font-size: 12px; min-width: 60px; color: var(--text-sec); }

.dialog-input {
  flex: 1;
  height: 28px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12px;
}
.dialog-input:focus { border-color: var(--primary); outline: none; }

.dialog-options { gap: 16px; }
.dialog-options label { min-width: auto; display: flex; align-items: center; gap: 4px; cursor: pointer; }

.dialog-actions { gap: 8px; justify-content: flex-end; }

.dialog-btn {
  height: 28px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  font-size: 12px;
  transition: background .1s;
}
.dialog-btn:hover { background: rgba(0,0,0,.06); }

.dialog-btn-primary {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}
.dialog-btn-primary:hover { background: #1765cc; }

.find-status { font-size: 11px; color: var(--text-sec); margin-left: 8px; }

/* ===== Help Panel ===== */
.help-panel {
  position: fixed;
  top: 0; right: 0;
  width: 360px;
  max-width: 100vw;
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  z-index: 800;
  display: flex;
  flex-direction: column;
}

.help-panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.help-function-list { margin-top: 12px; }

.help-category {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .5px;
  color: var(--text-sec);
  padding: 12px 0 6px;
  border-bottom: 1px solid var(--border-light);
}

.help-function-card {
  padding: 8px 4px;
  border-bottom: 1px solid var(--border-light);
  cursor: pointer;
  transition: background .1s;
}
.help-function-card:hover { background: var(--primary-bg); }

.help-fn-name {
  font-family: 'Courier New', monospace;
  font-weight: 600;
  font-size: 12px;
  color: var(--primary);
}

.help-fn-syntax {
  font-family: 'Courier New', monospace;
  font-size: 11px;
  color: var(--text-sec);
  margin-top: 2px;
}

.help-fn-desc {
  font-size: 11px;
  color: var(--text);
  margin-top: 4px;
  line-height: 1.5;
}

.help-fn-examples {
  font-size: 10px;
  color: var(--text-sec);
  margin-top: 4px;
  font-family: 'Courier New', monospace;
}

.help-empty {
  padding: 20px;
  text-align: center;
  color: var(--text-sec);
  font-style: italic;
}

/* ===== Shortcuts Panel ===== */
.shortcuts-panel { width: 440px; }

.shortcuts-body { padding: 16px; }

.shortcut-category {
  font-size: 12px;
  font-weight: 700;
  color: var(--primary);
  margin: 16px 0 8px;
}
.shortcut-category:first-child { margin-top: 0; }

.shortcut-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
  font-size: 12px;
  border-bottom: 1px solid var(--border-light);
}

.shortcut-keys {
  font-family: 'Courier New', monospace;
  font-size: 11px;
  background: var(--header-bg);
  padding: 2px 8px;
  border-radius: 3px;
  border: 1px solid var(--border);
  font-weight: 500;
}

.shortcut-desc { color: var(--text-sec); }

/* ===== Script Editor ===== */
.script-editor-panel { width: 500px; }

.script-editor-body { display: flex; flex-direction: column; flex: 1; padding: 12px; }

.script-hint {
  font-size: 11px;
  color: var(--text-sec);
  margin-bottom: 8px;
  line-height: 1.5;
}
.script-hint code {
  background: var(--header-bg);
  padding: 1px 4px;
  border-radius: 2px;
  font-size: 11px;
}

.script-textarea {
  flex: 1;
  min-height: 300px;
  font-family: 'Courier New', monospace;
  font-size: 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  resize: vertical;
  tab-size: 2;
  line-height: 1.5;
}
.script-textarea:focus { border-color: var(--primary); outline: none; }

.script-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

.script-status { font-size: 11px; color: var(--text-sec); }
.script-status.error { color: var(--danger); }
.script-status.success { color: #188038; }

/* ===== Chart Preview ===== */
.chart-preview {
  margin-top: 12px;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  min-height: 200px;
  background: var(--header-bg);
}

/* ===== Charts Container ===== */
.charts-container {
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
  z-index: 50;
}

.chart-floating {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  pointer-events: auto;
  overflow: hidden;
}

.chart-floating.selected { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); }

.chart-floating-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--border-light);
  font-size: 11px;
  cursor: move;
}

.chart-floating-body {
  padding: 8px;
  height: calc(100% - 28px);
}

.chart-floating-body canvas {
  width: 100% !important;
  height: 100% !important;
}

/* ===== Filter Dropdown ===== */
.filter-dropdown {
  position: fixed;
  z-index: 999;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 8px 0;
  min-width: 200px;
  max-height: 300px;
  overflow-y: auto;
}

.filter-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
}
.filter-item:hover { background: var(--primary-bg); }

.filter-divider { height: 1px; background: var(--border-light); margin: 4px 0; }

.filter-apply-btn {
  display: block;
  width: calc(100% - 24px);
  margin: 8px 12px 4px;
  height: 28px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12px;
}

/* ===== Save Notification ===== */
.save-notification {
  position: fixed;
  bottom: 40px;
  left: 50%;
  transform: translateX(-50%) translateY(60px);
  background: #323232;
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 13px;
  z-index: 2000;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: opacity .3s, transform .3s;
  pointer-events: none;
}
.save-notification.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ===== Print ===== */
@media print {
  .toolbar, .formula-bar, .sheet-tab-bar, .status-bar,
  .help-panel, .dialog, .context-menu, .selection-box,
  .range-highlight, .fill-handle, .cell-editor, .grid-overlay,
  .save-notification, .charts-container, .shortcuts-panel,
  .script-editor-panel, .chart-dialog-panel { display: none !important; }

  .grid-container { overflow: visible !important; }
  .grid-scroll { overflow: visible !important; position: static !important; }
}

/* ===== Mobile ===== */
@media (max-width: 768px) {
  .toolbar {
    padding: 2px 4px;
    gap: 1px;
    overflow-x: auto;
    flex-wrap: nowrap;
  }
  .toolbar-group { padding: 0 2px; border-right: none; }
  .toolbar-select { max-width: 80px; font-size: 11px; }
  .name-box { width: 60px; min-width: 50px; }
  .formula-input { font-size: 11px; }
  .help-panel, .shortcuts-panel, .script-editor-panel { width: 100vw; }
  .dialog { max-width: 95vw; }
  .grid-cell { font-size: 10px; }
  .fill-handle { width: 12px; height: 12px; }
}

@media (max-width: 480px) {
  .toolbar-group:nth-child(n+5) { display: none; }
  .toolbar-right { display: flex; }
}
