:root {
  color-scheme: light;
  --bg: #edf3fb;
  --panel: rgba(255, 255, 255, 0.92);
  --panel-strong: #ffffff;
  --text: #162033;
  --muted: #5b6f87;
  --grid: #d7e1ee;
  --header: linear-gradient(180deg, #f8fbff 0%, #eef3f8 100%);
  --active: #1877f2;
  --range: rgba(24, 119, 242, 0.12);
  --range-strong: rgba(24, 119, 242, 0.18);
  --error: #d93025;
  --error-bg: #fef3f2;
  --shadow: 0 20px 52px rgba(19, 32, 51, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(24, 119, 242, 0.14), transparent 28%),
    radial-gradient(circle at top, #ffffff 0%, var(--bg) 45%, #e9eef8 100%);
}

.app-shell {
  min-height: 100vh;
  padding: 18px;
}

.topbar,
.formula-bar-wrap,
.grid-shell {
  max-width: 1440px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 12px;
}

.product-name {
  font-size: 22px;
  font-weight: 760;
  letter-spacing: -0.03em;
}

.product-hint {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
}

.selection-chip,
.name-box {
  min-width: 72px;
  text-align: center;
  background: var(--panel);
  border: 1px solid rgba(101, 112, 138, 0.16);
  border-radius: 12px;
  padding: 10px 12px;
  font-weight: 600;
  box-shadow: var(--shadow), inset 0 0 0 1px rgba(255, 255, 255, 0.7);
}

.formula-bar-wrap {
  display: grid;
  grid-template-columns: 84px 52px 1fr;
  gap: 10px;
  align-items: center;
  margin-bottom: 14px;
}

.formula-prefix,
.formula-input {
  background: var(--panel);
  border: 1px solid rgba(101, 112, 138, 0.16);
  border-radius: 12px;
}

.formula-prefix {
  padding: 10px 12px;
  text-align: center;
  font-weight: 700;
  color: var(--active);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.7);
}

.formula-input {
  width: 100%;
  padding: 12px 14px;
  font: inherit;
  color: var(--text);
  background: white;
  box-shadow: inset 0 1px 2px rgba(19, 32, 51, 0.04);
}

.formula-input:focus {
  outline: 2px solid rgba(24, 119, 242, 0.28);
  border-color: rgba(24, 119, 242, 0.4);
}

.grid-shell {
  background: var(--panel-strong);
  border: 1px solid rgba(101, 112, 138, 0.16);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: auto;
  max-height: calc(100vh - 154px);
}

.sheet-grid {
  border-collapse: separate;
  border-spacing: 0;
  min-width: 100%;
}

.sheet-grid th,
.sheet-grid td {
  border-right: 1px solid var(--grid);
  border-bottom: 1px solid var(--grid);
  min-width: 110px;
  height: 34px;
  padding: 0;
  position: relative;
}

.sheet-grid th {
  background: var(--header);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  user-select: none;
}

.corner-cell,
.row-header {
  position: sticky;
  left: 0;
  z-index: 3;
}

.corner-cell {
  top: 0;
  z-index: 4;
}

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

.row-header {
  min-width: 56px;
  width: 56px;
}

.cell {
  background: #fff;
  cursor: cell;
  transition: background-color 120ms ease;
}

.cell button,
.cell input {
  width: 100%;
  height: 100%;
  border: 0;
  padding: 7px 10px;
  text-align: left;
  background: transparent;
  color: inherit;
  font: 14px/1.2 inherit;
}

.cell button {
  cursor: cell;
}

.cell button:hover {
  background: rgba(24, 119, 242, 0.03);
}

.cell button:focus,
.cell input:focus {
  outline: none;
}

.cell.numeric button,
.cell.numeric input,
.cell.boolean button,
.cell.boolean input {
  text-align: right;
}

.cell.error button,
.cell.error input {
  color: var(--error);
  font-weight: 600;
  background: linear-gradient(180deg, var(--error-bg) 0%, white 100%);
}

.cell.active {
  background: linear-gradient(180deg, rgba(24, 119, 242, 0.08) 0%, rgba(24, 119, 242, 0.04) 100%);
  box-shadow: inset 0 0 0 2px var(--active), 0 0 0 1px rgba(24, 119, 242, 0.1);
  z-index: 1;
}

.cell.in-range {
  background: var(--range);
}

.cell.in-range.active {
  background: linear-gradient(180deg, var(--range-strong) 0%, rgba(24, 119, 242, 0.08) 100%);
}

.cell.active::after {
  content: "";
  position: absolute;
  inset: -2px;
  border: 2px solid var(--active);
  pointer-events: none;
}

.header-menu {
  position: fixed;
  display: grid;
  gap: 6px;
  min-width: 164px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.98);
  border: 1px solid rgba(101, 112, 138, 0.18);
  border-radius: 12px;
  box-shadow: 0 14px 34px rgba(22, 32, 51, 0.18);
  z-index: 20;
}

.header-menu.hidden {
  display: none;
}

.header-menu button {
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 8px 10px;
  font: inherit;
}

.header-menu button:hover {
  background: rgba(24, 119, 242, 0.08);
}

@media (max-width: 768px) {
  .app-shell {
    padding: 10px;
  }

  .topbar {
    flex-direction: column;
    align-items: stretch;
  }

  .formula-bar-wrap {
    grid-template-columns: 72px 44px 1fr;
  }

  .sheet-grid th,
  .sheet-grid td {
    min-width: 96px;
  }
}
