:root {
  --bg: #f6f8fb;
  --panel: #ffffff;
  --panel-soft: #f9fafb;
  --border: #d9e0ea;
  --border-strong: #aeb8c8;
  --text: #111827;
  --muted: #5b6472;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --accent-line: #1d4ed8;
  --danger: #b42318;
  --ok: #047857;
  --shadow: 0 12px 35px rgba(15, 23, 42, 0.08);
  --header-h: 36px;
  font-family: Inter, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
}

body {
  overflow: hidden;
  overflow-x: hidden;
  background: linear-gradient(180deg, #fbfdff 0%, #eef3f8 100%);
}

button,
input,
select {
  font: inherit;
}

button,
select,
input[type="text"],
input[type="number"] {
  min-height: 32px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 5px 9px;
  cursor: pointer;
  white-space: nowrap;
}

button:hover,
select:hover,
input:hover {
  border-color: var(--border-strong);
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
[contenteditable="true"]:focus-visible {
  outline: 3px solid rgba(37, 99, 235, 0.22);
  outline-offset: 1px;
}

button.active,
.lang-btn.active,
.sheet-tab.active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: #123c85;
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  width: 100%;
  max-width: 100vw;
  height: 100vh;
  min-height: 560px;
  overflow: hidden;
  color: var(--text);
}

.app-shell.is-fullscreen {
  background: var(--panel);
}

.topbar {
  z-index: 10;
  display: grid;
  width: 100%;
  max-width: 100vw;
  min-width: 0;
  gap: 7px;
  padding: 8px 12px 7px;
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 1px 0 rgba(15, 23, 42, 0.04);
  backdrop-filter: blur(14px);
}

.brand-row,
.toolbar,
.formula-row,
.sheet-footer,
.statusbar {
  display: flex;
  align-items: center;
  min-width: 0;
  max-width: 100%;
}

.brand-row {
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-width: 180px;
  font-weight: 700;
  letter-spacing: 0;
}

.brand-byline {
  padding-left: 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.brand-mark {
  display: inline-grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 7px;
  background: #111827;
  color: #fff;
  font-size: 17px;
}

.language-switch,
.sheet-actions {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.toolbar {
  flex-wrap: nowrap;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: 2px;
}

.toolbar.compact {
  align-items: stretch;
}

.toolbar > * {
  flex: 0 0 auto;
}

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

.icon-btn {
  width: 32px;
  padding: 6px;
}

.select-label,
.number-label,
.color-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.select-label select {
  max-width: 190px;
  padding: 0 28px 0 8px;
}

.number-label input {
  width: 62px;
  padding: 0 8px;
}

.color-label input {
  width: 36px;
  min-height: 32px;
  padding: 2px;
  border-radius: 7px;
}

.fill-menu summary {
  gap: 7px;
}

.fill-preview {
  width: 16px;
  height: 16px;
  border: 1px solid #9ca3af;
  border-radius: 4px;
  background: #fff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.65);
}

.fill-preview.empty {
  background:
    linear-gradient(135deg, transparent 45%, #ef4444 45%, #ef4444 55%, transparent 55%),
    #fff;
}

.toolbar-menu {
  position: relative;
}

.toolbar-menu summary {
  display: inline-flex;
  align-items: center;
  min-height: 32px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.toolbar-menu summary::-webkit-details-marker {
  display: none;
}

.toolbar-menu summary::after {
  content: "▾";
  margin-left: 8px;
  color: var(--muted);
  font-size: 11px;
}

.toolbar-menu[open] summary {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.menu-panel {
  position: fixed;
  top: var(--menu-top, 76px);
  left: var(--menu-left, 12px);
  z-index: 35;
  display: grid;
  gap: 7px;
  min-width: 190px;
  max-width: calc(100vw - 16px);
  max-height: min(460px, calc(100vh - var(--menu-top, 76px) - 10px));
  overflow: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 45px rgba(15, 23, 42, 0.16);
}

.menu-panel.wide {
  min-width: 235px;
}

.help-panel {
  width: min(360px, calc(100vw - 16px));
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.help-panel strong {
  color: var(--text);
  font-size: 14px;
}

.help-panel ul {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 18px;
}

.fill-panel {
  min-width: 220px;
}

.fill-picker {
  justify-content: space-between;
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.color-grid button {
  width: 100%;
  min-height: 30px;
  border-color: #cbd5e1;
  background: var(--swatch);
}

.color-grid button:hover,
.color-grid button:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.18);
}

.menu-panel button,
.context-menu button {
  justify-content: flex-start;
  width: 100%;
}

.menu-panel .select-label,
.menu-panel .number-label {
  justify-content: space-between;
  width: 100%;
}

.menu-panel select,
.menu-panel input[type="number"] {
  max-width: 135px;
}

.menu-row {
  display: flex;
  gap: 6px;
}

.menu-row button {
  justify-content: center;
}

.formula-row {
  width: 100%;
  gap: 8px;
}

.name-box {
  width: 84px;
  padding: 0 10px;
  text-transform: uppercase;
}

.formula-label {
  display: inline-grid;
  flex: 0 0 34px;
  width: 34px;
  height: 34px;
  min-height: 34px;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--panel-soft);
  color: var(--muted);
  font-weight: 700;
}

.formula-bar {
  flex: 1 1 280px;
  min-width: 0;
  width: 100%;
  padding: 0 11px;
}

.formula-help {
  position: fixed;
  top: var(--formula-help-top, 116px);
  left: var(--formula-help-left, 112px);
  z-index: 36;
  display: grid;
  gap: 8px;
  width: min(420px, calc(100vw - 16px));
  max-height: min(520px, calc(100vh - var(--formula-help-top, 116px) - 10px));
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 45px rgba(15, 23, 42, 0.18);
}

.formula-help.hidden {
  display: none;
}

.formula-help strong {
  font-size: 14px;
}

.formula-help p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.formula-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 7px;
}

.formula-list button {
  display: grid;
  justify-items: start;
  gap: 3px;
  min-height: 78px;
  padding: 8px;
  text-align: left;
  white-space: normal;
}

.formula-list span {
  font-weight: 700;
}

.formula-list small {
  color: var(--muted);
  font-size: 12px;
}

.formula-list code {
  max-width: 100%;
  overflow: hidden;
  color: #1d4ed8;
  font-size: 12px;
  text-overflow: ellipsis;
}

.work-area {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 8px;
  min-width: 0;
  min-height: 0;
  padding: 10px 12px 8px;
}

.grid-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
}

.sheet-table {
  position: relative;
  border-collapse: separate;
  border-spacing: 0;
  min-width: 100%;
  table-layout: fixed;
  background: #fff;
  font-size: 14px;
}

.sheet-table th,
.sheet-table td {
  border-right: 1px solid #e4e8ef;
  border-bottom: 1px solid #e4e8ef;
}

.sheet-table th {
  position: sticky;
  z-index: 3;
  height: var(--header-h);
  background: #f4f7fb;
  color: #4b5563;
  font-weight: 600;
  text-align: center;
  user-select: none;
}

.corner-header {
  left: 0;
  top: 0;
  z-index: 6;
  width: 48px;
  min-width: 48px;
}

.col-header {
  top: 0;
  min-width: 96px;
}

.row-header {
  left: 0;
  z-index: 4;
  width: 48px;
  min-width: 48px;
}

.col-header.selected,
.row-header.selected {
  background: #e8f0ff;
  color: #173b78;
}

.sheet-cell {
  position: relative;
  width: 96px;
  min-width: 96px;
  height: 32px;
  padding: 5px 7px;
  overflow: hidden;
  background: #fff;
  color: var(--text);
  line-height: 1.35;
  vertical-align: middle;
  white-space: nowrap;
  outline: none;
}

.sheet-cell.wrap {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.sheet-cell.selected {
  background-image: linear-gradient(rgba(37, 99, 235, 0.11), rgba(37, 99, 235, 0.11));
  box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.35);
}

.sheet-cell.active {
  z-index: 2;
  box-shadow: inset 0 0 0 2px var(--accent-line), 0 0 0 1px var(--accent-line);
}

.sheet-cell.search-hit {
  background-image: repeating-linear-gradient(
    135deg,
    rgba(245, 158, 11, 0.18),
    rgba(245, 158, 11, 0.18) 8px,
    rgba(245, 158, 11, 0.3) 8px,
    rgba(245, 158, 11, 0.3) 16px
  );
}

.sheet-cell.negative {
  color: var(--danger);
}

.sheet-cell.error {
  color: var(--danger);
  font-weight: 700;
}

.sheet-cell.filtered {
  opacity: 0.42;
}

.resize-handle {
  position: absolute;
  right: -3px;
  bottom: 0;
  width: 7px;
  height: 100%;
  cursor: col-resize;
}

.row-header .resize-handle {
  top: auto;
  right: 0;
  bottom: -3px;
  width: 100%;
  height: 7px;
  cursor: row-resize;
}

.chart-layer {
  display: grid;
  gap: 7px;
  min-height: 0;
  max-height: 284px;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.chart-layer.hidden {
  display: none;
}

.chart-layer-title {
  padding: 8px 10px 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.chart-list {
  display: flex;
  gap: 10px;
  min-width: 0;
  overflow-x: auto;
  padding: 0 10px 10px;
}

.chart-card {
  position: relative;
  flex: 0 0 320px;
  z-index: 5;
  width: 320px;
  height: 220px;
  min-height: 220px;
  border: 1px solid rgba(148, 163, 184, 0.75);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.11);
  overflow: hidden;
}

.chart-card.selected {
  outline: 3px solid rgba(37, 99, 235, 0.28);
}

.chart-title {
  display: flex;
  align-items: center;
  height: 34px;
  padding: 0 10px;
  border-bottom: 1px solid var(--border);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.chart-card svg {
  display: block;
  width: 100%;
  height: calc(100% - 34px);
}

.sheet-footer {
  min-height: 45px;
  padding: 7px 12px;
  gap: 8px;
  border-top: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.92);
}

.sheet-tabs {
  display: flex;
  flex: 1;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: 2px;
}

.sheet-tab {
  min-width: 92px;
  max-width: 190px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.statusbar {
  min-height: 34px;
  gap: 14px;
  padding: 0 12px;
  border-top: 1px solid var(--border);
  background: #f8fafc;
  color: var(--muted);
  font-size: 13px;
}

.statusbar span {
  min-width: max-content;
}

.notice {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  color: var(--ok);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.modal {
  position: fixed;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(15, 23, 42, 0.32);
}

.modal.hidden {
  display: none;
}

.modal-panel {
  display: grid;
  gap: 13px;
  width: min(460px, 100%);
  padding: 20px;
  border-radius: 10px;
  background: #fff;
  box-shadow: 0 28px 80px rgba(15, 23, 42, 0.24);
}

.modal-panel h2 {
  margin: 0;
  font-size: 20px;
}

.modal-panel label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
}

.modal-panel input[type="text"] {
  width: 100%;
  padding: 0 10px;
}

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

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

.context-menu {
  position: fixed;
  z-index: 40;
  display: grid;
  gap: 4px;
  min-width: 178px;
  padding: 7px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 18px 45px rgba(15, 23, 42, 0.2);
}

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

.context-menu hr {
  width: 100%;
  height: 1px;
  margin: 3px 0;
  border: 0;
  background: var(--border);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.print-area {
  display: none;
}

@media (max-width: 980px) {
  body {
    overflow-y: auto;
    overflow-x: hidden;
  }

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

  .topbar {
    gap: 7px;
  }

  .toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .brand-row {
    align-items: flex-start;
  }

  .brand {
    min-width: 0;
  }

  .language-switch {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .work-area {
    padding: 8px 8px 0;
  }

  .sheet-footer,
  .statusbar {
    padding-left: 8px;
    padding-right: 8px;
  }

  .sheet-actions {
    max-width: 44vw;
    overflow-x: auto;
  }
}

@media (max-width: 640px) {
  .app-shell {
    min-height: 620px;
  }

  .brand-row {
    flex-direction: column;
    gap: 8px;
  }

  .language-switch {
    width: 100%;
    justify-content: flex-start;
  }

  .formula-row {
    align-items: stretch;
  }

  .name-box {
    width: 74px;
  }

  .sheet-table {
    font-size: 13px;
  }

  .sheet-cell {
    height: 34px;
  }

  .statusbar {
    overflow-x: auto;
  }

  .chart-card {
    width: 280px;
  }
}

@media print {
  body {
    overflow: visible;
    background: #fff;
  }

  .app-shell {
    display: none !important;
  }

  .modal,
  .context-menu,
  .formula-help {
    display: none !important;
  }

  .print-area {
    display: block;
    color: #000;
  }

  .print-area h1 {
    margin: 0 0 10px;
    font-size: 16px;
  }

  .print-table {
    width: auto;
    max-width: 100%;
    border-collapse: collapse;
    font-family: Arial, sans-serif;
    font-size: 10px;
  }

  .print-table th,
  .print-table td {
    min-width: 60px;
    min-height: 20px;
    padding: 4px 6px;
    border: 1px solid #cbd5e1;
  }

  .print-table th {
    background: #f1f5f9;
    font-weight: 700;
    text-align: center;
  }

  @page {
    size: A4;
    margin: 12mm;
  }
}
