:root {
  --title-height: 52px;
  --menu-height: 36px;
  --toolbar-height: 42px;
  --formula-height: 40px;
  --footer-height: 38px;
  --row-header-width: 52px;
  --column-header-height: 28px;
  --cell-width: 112px;
  --cell-height: 28px;
  --border: #d8dee8;
  --header-bg: #f4f7fb;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --text: #172033;
  --muted: #64748b;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: #eef2f7;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: inherit;
}

.app-shell {
  height: 100%;
  display: grid;
  grid-template-rows:
    var(--title-height)
    var(--menu-height)
    var(--toolbar-height)
    var(--formula-height)
    minmax(0, 1fr)
    var(--footer-height);
  background: #ffffff;
}

.title-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  color: #ffffff;
  background: linear-gradient(120deg, #153b72, #2563a8);
  box-shadow: 0 1px 5px rgba(15, 23, 42, 0.25);
  z-index: 20;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.18);
  font-weight: 800;
}

.workbook-name {
  min-width: 200px;
  width: min(50vw, 520px);
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 5px 8px;
  color: #ffffff;
  background: transparent;
  font-size: 17px;
  font-weight: 650;
}

.workbook-name:hover,
.workbook-name:focus {
  outline: none;
  border-color: rgba(255, 255, 255, 0.45);
  background: rgba(255, 255, 255, 0.1);
}

.title-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 14px;
}

.save-status {
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
}

.user-button {
  position: relative;
  width: 34px;
  height: 34px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.12);
  cursor: pointer;
}

.user-head {
  position: absolute;
  left: 11px;
  top: 7px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ffffff;
}

.user-body {
  position: absolute;
  left: 7px;
  bottom: 5px;
  width: 18px;
  height: 11px;
  border-radius: 11px 11px 6px 6px;
  background: #ffffff;
}

.menu-bar {
  display: flex;
  align-items: stretch;
  padding: 0 8px;
  border-bottom: 1px solid var(--border);
  background: #ffffff;
  z-index: 18;
}

.menu {
  position: relative;
}

.menu-trigger {
  height: 100%;
  border: 0;
  padding: 0 12px;
  background: transparent;
  cursor: pointer;
}

.menu-trigger:hover,
.menu-trigger.active {
  background: #edf2f8;
}

.menu-panel {
  position: absolute;
  top: calc(100% + 1px);
  left: 0;
  display: none;
  min-width: 220px;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.15);
}

.menu-panel.open {
  display: block;
}

.menu-panel button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 20px;
  border: 0;
  border-radius: 5px;
  padding: 8px 10px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.menu-panel button:hover {
  background: #eff5ff;
}

.menu-panel button span {
  color: var(--muted);
  font-size: 12px;
}

.menu-separator {
  height: 1px;
  margin: 5px 2px;
  background: var(--border);
}

.quick-toolbar {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 4px 10px;
  border-bottom: 1px solid var(--border);
  background: #fafcff;
}

.quick-toolbar button {
  min-width: 31px;
  height: 30px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  cursor: pointer;
}

.quick-toolbar button:hover,
.quick-toolbar button.active {
  border-color: #c9d7ed;
  background: #eaf2ff;
}

.toolbar-divider {
  width: 1px;
  height: 24px;
  margin: 0 5px;
  background: var(--border);
}

.formula-bar {
  display: grid;
  grid-template-columns: 84px 36px minmax(0, 1fr) 34px 34px;
  align-items: center;
  border-bottom: 1px solid var(--border);
  background: #ffffff;
}

.name-box {
  height: 100%;
  display: grid;
  place-items: center;
  border-right: 1px solid var(--border);
  font-size: 13px;
  font-weight: 650;
}

.formula-symbol {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-family: Georgia, serif;
  font-style: italic;
  font-weight: 700;
}

.formula-input {
  height: 100%;
  border: 0;
  border-left: 1px solid var(--border);
  padding: 0 10px;
  outline: none;
}

.formula-input:focus {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.formula-action {
  height: 28px;
  margin-right: 4px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  cursor: pointer;
  font-size: 20px;
}

.formula-action:hover {
  background: #eef3f8;
}

.formula-action.cancel {
  color: #b91c1c;
}

.formula-action.commit {
  color: #15803d;
}

.work-area {
  min-height: 0;
  background: #ffffff;
}

.sheet-grid {
  height: 100%;
  display: grid;
  grid-template-columns: var(--row-header-width) minmax(0, 1fr);
  grid-template-rows: var(--column-header-height) minmax(0, 1fr);
}

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

.column-headers {
  grid-column: 2;
  grid-row: 1;
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
  background: var(--header-bg);
}

.row-headers {
  grid-column: 1;
  grid-row: 2;
  position: relative;
  overflow: hidden;
  border-right: 1px solid var(--border);
  background: var(--header-bg);
}

.column-header,
.row-header {
  position: absolute;
  display: grid;
  place-items: center;
  color: #526078;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  user-select: none;
}

.column-header.selected,
.row-header.selected {
  color: #174ea6;
  background: #dbeafe;
  font-weight: 700;
}

.column-header {
  top: 0;
  height: var(--column-header-height);
}

.row-header {
  left: 0;
  width: var(--row-header-width);
}

.grid-viewport {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  overflow: auto;
  outline: none;
  background: #ffffff;
}

.grid-viewport:focus-visible {
  box-shadow: inset 0 0 0 2px rgba(37, 99, 235, 0.35);
}

.grid-spacer {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: none;
}

.grid-layer {
  position: absolute;
  left: 0;
  top: 0;
}

.grid-cell {
  position: absolute;
  overflow: hidden;
  display: flex;
  align-items: center;
  padding: 0 7px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  background: #ffffff;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 13px;
  cursor: cell;
  user-select: none;
}

.grid-cell.formula {
  color: #1e3a8a;
}

.selection-box {
  position: absolute;
  display: none;
  border: 2px solid var(--accent);
  pointer-events: none;
  z-index: 4;
}

.selection-box::after {
  content: "";
  position: absolute;
  right: -4px;
  bottom: -4px;
  width: 6px;
  height: 6px;
  border: 1px solid #ffffff;
  background: var(--accent);
}

.cell-editor {
  position: absolute;
  display: none;
  z-index: 5;
  margin: 0;
  border: 2px solid var(--accent);
  padding: 0 6px;
  outline: none;
  background: #ffffff;
  font-size: 13px;
}

.sheet-footer {
  display: flex;
  align-items: stretch;
  border-top: 1px solid var(--border);
  background: #f7f9fc;
}

.add-sheet-button {
  width: 42px;
  border: 0;
  border-right: 1px solid var(--border);
  background: transparent;
  cursor: pointer;
  font-size: 22px;
}

.add-sheet-button:hover {
  background: #eaf2ff;
}

.sheet-tabs {
  display: flex;
  align-items: stretch;
  overflow-x: auto;
  min-width: 0;
}

.sheet-tab {
  position: relative;
  min-width: 96px;
  border: 0;
  border-right: 1px solid var(--border);
  padding: 0 20px;
  background: transparent;
  cursor: pointer;
  white-space: nowrap;
}

.sheet-tab:hover {
  background: #edf2f8;
}

.sheet-tab.active {
  color: #174ea6;
  background: #ffffff;
  font-weight: 700;
}

.sheet-tab.active::after {
  content: "";
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: 0;
  height: 3px;
  border-radius: 3px 3px 0 0;
  background: var(--accent);
}

.status-bar {
  margin-left: auto;
  display: flex;
  align-items: center;
  min-width: 150px;
  padding: 0 14px;
  border-left: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
}

.dialog {
  width: min(520px, calc(100vw - 32px));
  border: 0;
  border-radius: 12px;
  padding: 0;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.26);
}

.dialog::backdrop {
  background: rgba(15, 23, 42, 0.38);
}

.dialog form {
  padding: 18px;
}

.dialog-header {
  display: flex;
  align-items: center;
  margin-bottom: 18px;
}

.dialog-header h2 {
  margin: 0;
  font-size: 20px;
}

.dialog-close {
  margin-left: auto;
  border: 0;
  background: transparent;
  cursor: pointer;
  font-size: 25px;
}

.dialog label {
  display: grid;
  gap: 6px;
  margin-bottom: 13px;
  font-size: 13px;
  font-weight: 650;
}

.dialog input[type="text"] {
  height: 36px;
  border: 1px solid #c7d0dd;
  border-radius: 6px;
  padding: 0 9px;
  outline: none;
}

.dialog input[type="text"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.checkbox-row {
  display: flex !important;
  align-items: center;
  gap: 8px !important;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.dialog-actions button {
  border: 0;
  border-radius: 6px;
  padding: 9px 13px;
  color: #ffffff;
  background: var(--accent);
  cursor: pointer;
}

.dialog-result {
  min-height: 20px;
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.json-dialog {
  width: min(880px, calc(100vw - 32px));
}

.json-dialog textarea {
  width: 100%;
  height: min(60vh, 600px);
  resize: vertical;
  border: 1px solid #c7d0dd;
  border-radius: 7px;
  padding: 10px;
  font-family: Consolas, Monaco, monospace;
  font-size: 12px;
}

@media (max-width: 720px) {
  :root {
    --row-header-width: 44px;
    --cell-width: 100px;
  }

  .save-status {
    display: none;
  }

  .quick-toolbar {
    overflow-x: auto;
  }
}
