:root {
  --accent: #770000;
  --accent-hover: #aa0000;
  --border-color: #ccc;
  --header-bg: #eee;
  --header-fg: #333;
  --error-bg: #fdd;
  --error-fg: #a00;
  --header-flag: #2a8c4a;
  --overflow-flag: #2a6ca8;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: Arial, Helvetica, sans-serif;
  background: #fff;
  color: #222;
}

/* Suppresses native text selection while drag-selecting a range or drag-picking a formula reference. */
body.dragging {
  user-select: none;
  -webkit-user-select: none;
}

#toolbar,
#edit-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  flex: 0 0 auto;
}

#edit-toolbar {
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  flex-wrap: wrap;
}

#toolbar button,
#edit-toolbar button {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 3px;
  padding: 4px 8px;
  font-size: 13px;
  cursor: pointer;
}

#toolbar button:hover,
#edit-toolbar button:hover {
  background: var(--accent-hover);
}

#edit-toolbar button.active {
  background: white;
  color: var(--accent);
}

#cell-type-select {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 3px;
  padding: 4px 8px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
}

#cell-type-select:hover {
  background: var(--accent-hover);
}

.tb-label {
  font-size: 11px;
  text-transform: uppercase;
  opacity: 0.6;
  letter-spacing: 0.05em;
}

.tb-sep {
  width: 1px;
  align-self: stretch;
  background: rgba(255, 255, 255, 0.2);
  margin: 0 2px;
}

#file-name-label {
  font-family: monospace;
  font-size: 12px;
  opacity: 0.85;
}

#formula-bar {
  display: flex;
  align-items: stretch;
  gap: 8px;
  padding: 4px 10px;
  background: rgba(0, 0, 0, 0.5);
  border-top: 1px solid rgba(255, 255, 255, 0.15);
  flex: 0 0 auto;
}

#formula-bar-address {
  flex: 0 0 auto;
  width: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: monospace;
  font-size: 12px;
  color: white;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 3px;
}

#formula-bar-input {
  flex: 1 1 auto;
  min-width: 0;
  font-family: monospace;
  font-size: 13px;
  padding: 4px 8px;
  border: none;
  border-radius: 3px;
  background: white;
  color: #222;
}

#formula-bar-input:focus {
  outline: 2px solid var(--accent);
}

#main-area {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
}

#grid-scroll {
  flex: 1 1 auto;
  min-width: 0;
  width: 100%;
  overflow: auto;
}

/* Vertical rail on the left rather than a bottom tab strip - vertical space is scarcer, horizontal is cheap. Modeled on Lotus SmartSuite's page-tab rail. */
#sheet-tabs {
  flex: 0 0 auto;
  width: 34px;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--header-bg);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
}

.sheet-tab {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 5px;
  border-bottom: 1px solid var(--border-color);
  cursor: pointer;
  color: var(--header-fg);
  font-family: monospace;
  font-size: 12px;
}

.sheet-tab:hover {
  background: rgba(0, 0, 0, 0.06);
}

.sheet-tab.active {
  background: #fff;
  color: var(--accent);
  font-weight: bold;
}

.sheet-tab-label {
  writing-mode: vertical-rl;
  text-orientation: sideways;
  transform: rotate(180deg);
  white-space: nowrap;
  max-height: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sheet-tab-add {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 5px;
  cursor: pointer;
  color: var(--header-fg);
  font-size: 15px;
  font-weight: bold;
}

.sheet-tab-add:hover {
  background: rgba(0, 0, 0, 0.06);
}

#toast {
  position: fixed;
  bottom: 10px;
  right: 10px;
  z-index: 2147483647;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  padding: 8px 14px;
  border-radius: 5px;
  font-family: monospace;
  pointer-events: none;
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}

#toast.visible {
  opacity: 1;
  transform: translateY(0);
}

#grid {
  display: grid;
  /* grid-template-columns/rows are set from script.js - dynamic per-column/per-row. */
  width: max-content;
}

.gcell {
  border-right: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  padding: 2px 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
  line-height: 18px;
}

.corner,
.col-header,
.row-header {
  background: var(--header-bg);
  color: var(--header-fg);
  font-family: monospace;
  text-align: center;
  user-select: none;
  position: sticky;
  cursor: pointer;
}

.corner {
  top: 0;
  left: 0;
  z-index: 3;
}

.col-header {
  top: 0;
  z-index: 2;
}

.row-header {
  left: 0;
  z-index: 1;
}

/* Italicized while showing R1's content instead of C1/C2/etc (see updateColumnHeaderLabels in script.js), so it still reads as a stand-in. */
.col-header.showing-r1-label .col-header-label {
  font-style: italic;
}

/* col-header/row-header's position:sticky establishes the containing block these absolutely-positioned handles need. */
.col-resize-handle {
  position: absolute;
  top: 0;
  right: 0;
  width: 6px;
  height: 100%;
  cursor: col-resize;
}

.row-resize-handle {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 6px;
  cursor: row-resize;
}

.data-cell {
  background: #fff;
  outline: none;
  cursor: cell;
}

.data-cell.selected {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  z-index: 1;
  position: relative;
}

.data-cell.in-range {
  background: rgba(119, 0, 0, 0.12);
}

.data-cell.editing {
  cursor: text;
  background: #fffef0;
}

.data-cell.cell-error {
  background: var(--error-bg);
  color: var(--error-fg);
  font-weight: bold;
}

.data-cell.fmt-bold {
  font-weight: bold;
}

.data-cell.fmt-italic {
  font-style: italic;
}

.data-cell.fmt-align-center {
  text-align: center;
}

.data-cell.fmt-align-right {
  text-align: right;
}

/* Marks a cell excluded from sorting (toggleActiveHeader in script.js) - a different color/corner than an error, so the two are never confused. */
.data-cell.fmt-header {
  position: relative;
}

.data-cell.fmt-header::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  border-width: 0 8px 8px 0;
  border-style: solid;
  border-color: transparent var(--header-flag) transparent transparent;
}

/* Opposite corner from fmt-header, ::before not ::after, so a cell can carry both markers. Documents overflow is *enabled*, not that it's actively spilling. */
.data-cell.fmt-overflow {
  position: relative;
}

.data-cell.fmt-overflow::before {
  content: '';
  position: absolute;
  bottom: 0;
  right: 0;
  border-width: 0 0 8px 8px;
  border-style: solid;
  border-color: transparent transparent var(--overflow-flag) transparent;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2147483000;
}

.modal-backdrop[hidden] {
  display: none;
}

/* Matches the sibling `textfields` project's panel look, so the two feel like the same family of tools. */
.modal-panel {
  background: #333;
  color: white;
  padding: 24px;
  border-radius: 8px;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.modal-panel-wide {
  max-width: 480px;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-title {
  margin: 0;
  font-size: 15px;
  font-weight: bold;
}

.modal-panel label {
  font-size: 13px;
  opacity: 0.85;
}

.modal-note {
  margin: 0;
  font-size: 12px;
  opacity: 0.7;
}

.modal-panel a {
  color: #ff8888;
}

.modal-panel a:hover {
  color: #ffaaaa;
}

.modal-panel input[type="text"],
.modal-panel input[type="number"] {
  font-family: monospace;
  font-size: 13px;
  padding: 6px 8px;
  border: none;
  border-radius: 3px;
  background: white;
  color: #222;
}

.modal-panel input[type="text"]:focus,
.modal-panel input[type="number"]:focus {
  outline: 2px solid var(--accent);
}

.settings-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.settings-field input[type="number"] {
  width: 100px;
}

.settings-field-checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

input[type="checkbox"] {
  accent-color: var(--accent);
}

.shortcuts-list {
  margin: 0;
  padding-left: 20px;
  line-height: 1.6;
  font-size: 13px;
}

.shortcuts-list kbd {
  background: #222;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 3px;
  padding: 1px 6px;
  font-family: monospace;
}

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

.modal-actions button {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  font-size: 13px;
  cursor: pointer;
}

.modal-actions button:hover {
  background: var(--accent-hover);
}

#filename-modal-cancel-btn,
#settings-cancel-btn {
  background: #555;
}

#filename-modal-cancel-btn:hover,
#settings-cancel-btn:hover {
  background: #666;
}
