:root {
  --row-head: 52px;
  --col-head: 30px;
  --cell-w: 96px;
  --cell-h: 26px;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --border: #d7dde5;
  --grid-line: #e3e8ee;
  --text: #1f2937;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --danger: #b91c1c;
  --warning-bg: #fef3c7;
  --warning-border: #f59e0b;
  --header-bg: #f0f3f7;
  --shadow: 0 8px 30px rgba(15, 23, 42, 0.08);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  color: var(--text);
}

#app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: -0.02em;
  min-width: 110px;
}

.brand-mark {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  background: linear-gradient(135deg, #2563eb, #0ea5e9);
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(2, 1fr);
  gap: 2px;
  padding: 5px;
  box-shadow: 0 3px 8px rgba(37, 99, 235, 0.28);
}

.brand-mark span {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 1px;
}

.brand-mark span:first-child {
  background: rgba(255, 255, 255, 0.7);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.toolbar .separator {
  width: 1px;
  height: 22px;
  background: var(--border);
  margin: 0 4px;
}

button.tool {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 7px;
  padding: 6px 11px;
  cursor: pointer;
  font-size: 13px;
  line-height: 1.2;
  transition:
    background 120ms ease,
    border-color 120ms ease,
    transform 80ms ease;
}

button.tool:hover:not(:disabled) {
  background: #f7f9fc;
  border-color: #b9c3d0;
}

button.tool:active:not(:disabled) {
  transform: translateY(1px);
}

button.tool:disabled {
  opacity: 0.42;
  cursor: default;
}

button.tool.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

button.tool.primary:hover:not(:disabled) {
  background: #1d4ed8;
  border-color: #1d4ed8;
}

button.tool.danger {
  color: var(--danger);
}

button.tool.danger:hover:not(:disabled) {
  background: #fef2f2;
  border-color: #fecaca;
}

.doc-title {
  margin-left: auto;
  color: var(--muted);
  font-size: 13px;
  max-width: 32vw;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

main {
  flex: 1;
  min-height: 0;
  padding: 18px;
}

.list-view {
  max-width: 920px;
  margin: 0 auto;
  padding-top: 26px;
}

.list-header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.list-header h1 {
  margin: 0 0 5px;
  font-size: 26px;
  letter-spacing: -0.03em;
}

.list-header p {
  margin: 0;
  color: var(--muted);
}

.create-row {
  display: flex;
  gap: 8px;
  margin-bottom: 20px;
}

.create-row input {
  flex: 1;
  min-width: 180px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 11px;
  background: #fff;
  outline: none;
}

.create-row input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.doc-list {
  display: grid;
  gap: 10px;
}

.doc-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 11px;
  padding: 14px 15px;
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.03);
  transition:
    border-color 120ms ease,
    box-shadow 120ms ease,
    transform 120ms ease;
}

.doc-item:hover {
  border-color: #b7c3d2;
  box-shadow: 0 5px 18px rgba(15, 23, 42, 0.07);
  transform: translateY(-1px);
}

.doc-item-main {
  flex: 1;
  min-width: 0;
}

.doc-item-name {
  font-weight: 650;
  font-size: 15px;
  margin-bottom: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-item-meta {
  color: var(--muted);
  font-size: 12px;
}

.empty-list {
  border: 1px dashed #c7d1dd;
  border-radius: 12px;
  padding: 42px 24px;
  text-align: center;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.55);
}

.editor-view {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 120px);
}

.conflict-banner {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  color: #78350f;
  border-radius: 10px;
  padding: 10px 13px;
  margin-bottom: 10px;
  font-weight: 550;
}

.conflict-banner .spacer {
  flex: 1;
}

.formula-row {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px 9px 0 0;
  overflow: hidden;
}

.active-ref {
  width: 74px;
  flex: 0 0 74px;
  text-align: center;
  padding: 9px 6px;
  border-right: 1px solid var(--border);
  background: #f8fafc;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
  color: #334155;
}

#formula-bar {
  flex: 1;
  border: 0;
  outline: none;
  padding: 9px 12px;
  background: transparent;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 13px;
  min-width: 0;
}

#formula-bar:focus {
  background: #fbfdff;
  box-shadow: inset 0 0 0 2px rgba(37, 99, 235, 0.18);
}

.grid-scroll {
  flex: 1;
  min-height: 280px;
  max-height: calc(100vh - 198px);
  overflow: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: 0;
  border-radius: 0 0 9px 9px;
  position: relative;
}

.grid {
  display: grid;
  grid-template-columns: var(--row-head) repeat(26, var(--cell-w));
  grid-auto-rows: var(--cell-h);
  width: max-content;
  position: relative;
  background: #fff;
  user-select: none;
}

.grid-head {
  background: var(--header-bg);
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  color: #536174;
  font-size: 11.5px;
  font-weight: 650;
  display: flex;
  align-items: center;
  justify-content: center;
  position: sticky;
  top: 0;
  z-index: 5;
}

.grid-corner {
  left: 0;
  z-index: 7;
}

.row-head {
  position: sticky;
  left: 0;
  top: auto;
  z-index: 4;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--grid-line);
  background: var(--header-bg);
  color: #64748b;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell {
  border-right: 1px solid var(--grid-line);
  border-bottom: 1px solid var(--grid-line);
  padding: 3px 7px;
  font-size: 12.5px;
  line-height: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: cell;
  color: #111827;
  background: #fff;
}

.cell:hover {
  background: #f8fbff;
}

.cell.in-range {
  background: #edf5ff;
}

.cell.active {
  background: #e8f1ff;
  box-shadow: inset 0 0 0 2px var(--accent);
  position: relative;
  z-index: 2;
}

.cell-editor {
  position: absolute;
  z-index: 20;
  border: 2px solid var(--accent);
  background: #fff;
  padding: 2px 6px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
  font-size: 12.5px;
  line-height: 18px;
  outline: none;
  color: #111827;
  box-shadow: 0 3px 12px rgba(37, 99, 235, 0.2);
}

.statusbar {
  display: flex;
  gap: 18px;
  align-items: center;
  color: var(--muted);
  font-size: 12px;
  padding: 8px 3px 0;
  min-height: 26px;
}

.statusbar .status-msg {
  margin-left: auto;
}

.statusbar .kbd {
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 4px;
  background: #fff;
  font-family: inherit;
  font-size: 11px;
}

@media (max-width: 720px) {
  main {
    padding: 10px;
  }

  .doc-title {
    display: none;
  }

  .grid-scroll {
    max-height: calc(100vh - 210px);
  }
}
