:root {
  --line: #d3d8e0;
  --line-strong: #b6bec9;
  --head: #f2f4f7;
  --accent: #2f6feb;
  --accent-soft: #d9e6ff;
  --danger: #b3261e;
  --text: #1f2328;
  --muted: #5c6672;
}

* { box-sizing: border-box; }

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

body {
  display: flex;
  flex-direction: column;
  font: 13px/1.45 -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: #fff;
}

button {
  font: inherit;
  padding: 4px 10px;
  border: 1px solid var(--line-strong);
  border-radius: 5px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
}

button:hover:not(:disabled) { background: #eef2f8; border-color: #8f9aa8; }
button:active:not(:disabled) { background: #e2e8f1; }
button:disabled { opacity: 0.45; cursor: default; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

code { font-family: ui-monospace, Menlo, Consolas, monospace; background: #eef1f5; padding: 0 3px; border-radius: 3px; }

/* ------------------------------------------------------------- top bar */

#topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: var(--head);
  flex: none;
}

.brand {
  margin: 0;
  font-size: 15px;
  font-weight: 650;
  letter-spacing: 0.2px;
}

.doc-name {
  color: var(--muted);
  font-style: italic;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar { display: flex; align-items: center; gap: 6px; margin-left: auto; }
.toolbar .sep { width: 1px; height: 18px; background: var(--line-strong); margin: 0 4px; }
.status { color: var(--muted); min-width: 108px; text-align: right; }

/* --------------------------------------------------------- conflict bar */

#conflict-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  background: #fff4e5;
  border-bottom: 1px solid #f0c98a;
  color: #7a4b00;
  font-weight: 550;
  flex: none;
}

#conflict-banner[hidden] { display: none; }

#conflict-banner button { border-color: #d9a95f; }

/* ---------------------------------------------------------- formula bar */

#formula-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-bottom: 1px solid var(--line);
  flex: none;
}

.name-box {
  width: 56px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 4px 0;
  background: var(--head);
}

#formula-bar {
  flex: 1;
  font: inherit;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  padding: 5px 8px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
}

#formula-bar:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

/* -------------------------------------------------------------- layout */

#layout { flex: 1; display: flex; min-height: 0; }

#sidebar {
  width: 210px;
  flex: none;
  border-right: 1px solid var(--line);
  background: #fbfcfd;
  padding: 10px;
  overflow: auto;
}

#sidebar h2 { margin: 0 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); }
#sidebar .hint { color: var(--muted); font-size: 11px; margin-top: 14px; }

#doc-list { list-style: none; margin: 0; padding: 0; }

#doc-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 5px;
  padding: 2px 2px 2px 8px;
  cursor: pointer;
}

#doc-list li:hover { background: #eef2f8; }
#doc-list li.active { background: var(--accent-soft); }

#doc-list .doc-label {
  flex: 1;
  padding: 4px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  text-align: left;
  cursor: pointer;
}

#doc-list .doc-label:hover { text-decoration: underline; }
#doc-list .doc-label:focus-visible { outline: 2px solid var(--accent); }
#doc-list [data-testid="delete-doc"] { padding: 2px 7px; line-height: 1.2; color: var(--danger); }
#doc-list .empty { color: var(--muted); padding: 4px 8px; cursor: default; }
#doc-list .empty:hover { background: none; }

/* ---------------------------------------------------------------- sheet */

#sheet-wrap {
  position: relative;
  flex: 1;
  overflow: auto;
  outline: none;
  background: #fff;
}

#sheet {
  border-collapse: separate;
  border-spacing: 0;
  table-layout: fixed;
  width: max-content;
}

#sheet th, #sheet td {
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 0 6px;
  height: 22px;
  font-weight: 400;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#sheet thead th, #sheet tbody th {
  position: sticky;
  background: var(--head);
  color: var(--muted);
  font-size: 11px;
  text-align: center;
  z-index: 2;
  cursor: pointer;
  user-select: none;
}

#sheet thead th { top: 0; height: 22px; border-bottom-color: var(--line-strong); }
#sheet tbody th { left: 0; width: 44px; min-width: 44px; border-right-color: var(--line-strong); }
#sheet thead th.corner { left: 0; z-index: 3; width: 44px; min-width: 44px; cursor: default; }
#sheet thead th.selected, #sheet tbody th.selected { background: var(--accent-soft); color: #15417f; }

#sheet col { width: 84px; }
#sheet td { width: 84px; min-width: 84px; max-width: 84px; cursor: cell; }
#sheet td.number { text-align: right; font-variant-numeric: tabular-nums; }
#sheet td.error { color: var(--danger); font-weight: 600; }
#sheet td.formula { background: #f7faff; }

#sheet td.selected, #sheet th.selected-cell { background: var(--accent-soft); }
#sheet td.active { outline: 2px solid var(--accent); outline-offset: -2px; }

#cell-editor {
  position: absolute;
  display: none;
  z-index: 5;
  margin: 0;
  padding: 0 5px;
  font: inherit;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  border: 2px solid var(--accent);
  border-radius: 0;
  background: #fff;
  outline: none;
}

#cell-editor.visible { display: block; }
