:root {
  --green: #217346;
  --green-dark: #185c37;
  --border: #d4d4d4;
  --grid-line: #e1e1e1;
  --header-bg: #f3f3f3;
  --header-sel: #d3e8dc;
  --sel-bg: rgba(33, 115, 70, 0.12);
  --text: #222;
  --muted: #666;
  --panel-bg: #fbfbfb;
  --mark: #fff3bf;
  --row-h: 24px;
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { display: flex; flex-direction: column; background: #fff; font-size: 14px; overflow: hidden; }
button, select, input { font: inherit; }

/* ------------------------------------------------------------ hlavička */

.app-header {
  display: flex; align-items: center; gap: 16px;
  background: var(--green); color: #fff; padding: 6px 12px;
}
.brand { font-weight: 600; font-size: 16px; white-space: nowrap; }
.logo { display: inline-block; transform: scale(1.2); margin-right: 4px; }
.file-actions { display: flex; gap: 4px; flex-wrap: wrap; }
.file-actions button {
  background: transparent; color: #fff; border: 1px solid transparent; border-radius: 4px;
  padding: 3px 10px; cursor: pointer;
}
.file-actions button:hover { background: var(--green-dark); border-color: rgba(255,255,255,.3); }
.save-state { margin-left: auto; font-size: 12px; opacity: .85; white-space: nowrap; }

/* ------------------------------------------------------------ nástroje */

.toolbar {
  display: flex; align-items: center; gap: 3px; flex-wrap: wrap;
  padding: 5px 10px; background: #f8f8f8; border-bottom: 1px solid var(--border);
}
.toolbar button, .toolbar select, .color-btn {
  min-width: 30px; height: 30px; padding: 0 8px; background: #fff;
  border: 1px solid var(--border); border-radius: 4px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
.toolbar button:hover, .color-btn:hover { background: #e9f3ed; border-color: #9cc5ad; }
.toolbar button.on { background: var(--header-sel); border-color: var(--green); color: var(--green-dark); }
.toolbar .sep { width: 1px; height: 22px; background: var(--border); margin: 0 6px; }
.color-btn { position: relative; overflow: hidden; }
.color-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.swatch-a { font-weight: 700; border-bottom: 4px solid #c00000; line-height: 1; padding: 0 2px; }
.swatch-fill { color: #fff2cc; text-shadow: 0 0 1px #888; }

/* ------------------------------------------------------------ řádek vzorců */

.formula-bar {
  display: flex; align-items: center; gap: 6px; padding: 4px 8px;
  border-bottom: 1px solid var(--border);
}
#nameBox { width: 90px; height: 26px; border: 1px solid var(--border); padding: 0 6px; text-align: center; }
.fx { color: var(--muted); font-style: italic; font-family: Georgia, serif; padding: 0 6px; border-left: 1px solid var(--border); border-right: 1px solid var(--border); }
#formulaInput {
  flex: 1; height: 26px; border: 1px solid var(--border); padding: 0 8px;
  font-family: Consolas, "Cascadia Mono", Menlo, monospace;
}
#formulaInput:focus, #nameBox:focus { outline: 2px solid var(--green); outline-offset: -1px; }

/* ------------------------------------------------------------ mřížka */

.workspace { flex: 1; display: flex; min-height: 0; }
.grid-wrap { flex: 1; overflow: auto; position: relative; outline: none; }
#grid {
  border-collapse: separate; border-spacing: 0; table-layout: fixed;
  font-size: 14px; user-select: none;
}
#grid th, #grid td {
  height: var(--row-h); border-right: 1px solid var(--grid-line); border-bottom: 1px solid var(--grid-line);
  padding: 0 4px; white-space: nowrap; overflow: hidden; text-overflow: clip;
}
#grid thead th {
  position: sticky; top: 0; z-index: 3; background: var(--header-bg); color: #444;
  font-weight: normal; text-align: center; border-color: var(--border);
}
#grid th.row-h {
  position: sticky; left: 0; z-index: 2; background: var(--header-bg); color: #444;
  font-weight: normal; text-align: center; border-color: var(--border); width: 46px;
}
#grid thead th.corner { left: 0; z-index: 4; cursor: pointer; }
#grid thead th.col-h { position: sticky; cursor: pointer; }
#grid th.hsel { background: var(--header-sel); color: var(--green-dark); font-weight: 600; }
#grid td { cursor: cell; position: relative; }
#grid td.num { text-align: right; }
#grid td.ctr { text-align: center; }
#grid td.err { text-align: center; color: #c92a2a; }
#grid td.a-left { text-align: left; }
#grid td.a-center { text-align: center; }
#grid td.a-right { text-align: right; }
#grid td.formula-view { font-family: Consolas, Menlo, monospace; font-size: 12px; color: #1c4f9c; text-align: left; }
#grid td.sel { background-image: linear-gradient(var(--sel-bg), var(--sel-bg)); }
#grid td.active { outline: 2px solid var(--green); outline-offset: -2px; z-index: 1; }
#grid td.hl {
  box-shadow: inset 0 0 0 2px var(--hl);
  background-image: linear-gradient(color-mix(in srgb, var(--hl) 14%, transparent), color-mix(in srgb, var(--hl) 14%, transparent));
}
#grid td.hl-step { animation: pulse 1s ease-in-out infinite alternate; }
#grid td.dep { box-shadow: inset 0 0 0 2px #868e96; }
@keyframes pulse {
  from { box-shadow: inset 0 0 0 2px var(--hl); }
  to { box-shadow: inset 0 0 0 4px var(--hl); background-color: color-mix(in srgb, var(--hl) 30%, transparent); }
}
.resizer { position: absolute; right: -3px; top: 0; bottom: 0; width: 6px; cursor: col-resize; z-index: 5; }
#cellEditor {
  position: absolute; display: none; z-index: 10; border: 2px solid var(--green);
  padding: 0 3px; font-size: 14px; outline: none; background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
}

/* ------------------------------------------------------------ listy a stav */

.sheetbar {
  display: flex; align-items: stretch; gap: 0; border-top: 1px solid var(--border);
  background: #f3f3f3; height: 32px; font-size: 13px;
}
.sheet-tabs { display: flex; overflow-x: auto; }
.sheet-tab {
  padding: 0 16px; display: flex; align-items: center; cursor: pointer; border-right: 1px solid var(--border);
  white-space: nowrap; color: #444;
}
.sheet-tab:hover { background: #e9e9e9; }
.sheet-tab.active { background: #fff; color: var(--green-dark); font-weight: 600; border-bottom: 3px solid var(--green); }
.sheet-tab input { width: 110px; font-size: 13px; }
#addSheet { border: none; background: transparent; font-size: 18px; width: 34px; cursor: pointer; color: #444; }
#addSheet:hover { background: #e2e2e2; }
.status { margin-left: auto; display: flex; align-items: center; gap: 18px; padding: 0 14px; color: #444; white-space: nowrap; }

/* ------------------------------------------------------------ výuková vrstva */

.learn-panel {
  width: 400px; border-left: 1px solid var(--border); background: var(--panel-bg);
  display: flex; flex-direction: column; min-height: 0;
}
.learn-panel.hidden { display: none; }
.learn-head {
  display: flex; align-items: center; justify-content: space-between; padding: 8px 12px;
  font-weight: 600; border-bottom: 1px solid var(--border); background: #fff;
}
.learn-head button { border: none; background: transparent; cursor: pointer; color: var(--muted); }
.learn-body { overflow-y: auto; padding: 10px 12px 30px; line-height: 1.45; }
.learn-body h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 6px; }
.learn-body h3:first-child { margin-top: 4px; }
.learn-body .muted { color: var(--muted); }
.code {
  font-family: Consolas, "Cascadia Mono", Menlo, monospace; background: #fff; border: 1px solid var(--border);
  border-radius: 4px; padding: 6px 8px; word-break: break-all;
}
.code.big { font-size: 16px; }
.code mark { background: var(--mark); border-radius: 3px; padding: 0 2px; outline: 1px solid #f0c36d; }
.code .ref { font-weight: 600; }
.result-box { font-size: 18px; font-weight: 600; }
.result-box.error { color: #c92a2a; }
.reason { background: #fff5f5; border-left: 3px solid #e03131; padding: 6px 8px; margin-top: 6px; border-radius: 0 4px 4px 0; }
.ok { color: var(--green-dark); }
.bad { color: #c92a2a; }

.stepper { display: flex; align-items: center; gap: 4px; margin-bottom: 8px; }
.stepper button { border: 1px solid var(--border); background: #fff; border-radius: 4px; height: 28px; min-width: 30px; cursor: pointer; }
.stepper button:hover:not(:disabled) { background: #e9f3ed; }
.stepper button:disabled { opacity: .4; cursor: default; }
.stepper .counter { flex: 1; text-align: center; font-size: 13px; color: var(--muted); }
.step-card { background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 10px; }
.step-card .arrow { text-align: center; color: var(--muted); margin: 2px 0; }
.step-card .desc { margin-top: 8px; }
.tip { margin-top: 6px; background: #e7f5ff; border-left: 3px solid #339af0; padding: 5px 8px; border-radius: 0 4px 4px 0; font-size: 13px; }
.tip.warn { background: #fff9db; border-color: #fab005; }
.step-list { list-style: none; padding: 0; margin: 8px 0 0; counter-reset: step; }
.step-list li {
  counter-increment: step; display: flex; gap: 8px; padding: 4px 6px; border-radius: 4px; cursor: pointer;
  font-family: Consolas, Menlo, monospace; font-size: 12.5px; word-break: break-all;
}
.step-list li::before { content: counter(step) "."; color: var(--muted); min-width: 20px; }
.step-list li:hover { background: #f1f3f5; }
.step-list li.current { background: var(--header-sel); }

.tree, .tree ul { list-style: none; margin: 0; padding-left: 18px; }
.tree { padding-left: 0; }
.tree li { position: relative; padding: 2px 0; }
.tree ul li::before { content: ""; position: absolute; left: -12px; top: 0; height: 13px; width: 9px; border-left: 1px solid #bbb; border-bottom: 1px solid #bbb; }
.tree .node { display: inline-flex; align-items: baseline; gap: 6px; }
.tree .lbl { font-family: Consolas, Menlo, monospace; font-weight: 600; background: #fff; border: 1px solid var(--border); border-radius: 4px; padding: 0 6px; }
.tree .kind { color: var(--muted); font-size: 12px; }

.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.chip {
  font-family: Consolas, Menlo, monospace; font-size: 12.5px; border: 1px solid var(--border); background: #fff;
  border-radius: 12px; padding: 1px 9px; cursor: pointer;
}
.chip:hover { border-color: var(--green); }
.fn-help { background: #fff; border: 1px solid var(--border); border-radius: 6px; padding: 8px; }
.fn-help .syntax { font-family: Consolas, Menlo, monospace; font-weight: 600; }
.fn-help .syntax .cur { background: var(--mark); }

.toast {
  position: fixed; left: 50%; bottom: 50px; transform: translateX(-50%) translateY(20px);
  background: #333; color: #fff; padding: 10px 16px; border-radius: 6px; opacity: 0; pointer-events: none;
  transition: all .2s; max-width: 80vw; z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: #c92a2a; }

@media (max-width: 900px) {
  .learn-panel { width: 300px; }
  .save-state { display: none; }
}

.learn-body code { font-family: Consolas, Menlo, monospace; background: #f1f3f5; border-radius: 3px; padding: 0 4px; }
.prio { border-collapse: collapse; width: 100%; font-size: 13px; }
.prio td { padding: 2px 6px; border-bottom: 1px solid #eee; }

/* úchyt pro vyplňování */
#fillHandle {
  position: absolute; display: none; width: 8px; height: 8px; z-index: 2;
  background: var(--green); border: 1px solid #fff; cursor: crosshair;
}
#grid td.fill-prev { outline: 1px dashed #333; outline-offset: -2px; background-image: linear-gradient(rgba(0,0,0,.04), rgba(0,0,0,.04)); }
#fillTip {
  position: absolute; display: none; z-index: 11; pointer-events: none; white-space: nowrap;
  background: #fffbe6; border: 1px solid #c9b458; border-radius: 3px; padding: 1px 6px; font-size: 12px;
  font-family: Consolas, Menlo, monospace; box-shadow: 0 1px 4px rgba(0,0,0,.15);
}
.suggest { list-style: none; margin: 0; padding: 0; }
.suggest li { padding: 4px 6px; border-radius: 4px; cursor: pointer; }
.suggest li:hover, .suggest li.first { background: var(--header-sel); }
.suggest .syntax { font-family: Consolas, Menlo, monospace; font-weight: 600; font-size: 12.5px; }
.suggest .h { color: var(--muted); font-size: 12px; }
.fn-list { display: flex; flex-wrap: wrap; gap: 4px; }
.hit-note { display: block; color: #0ca678; font-size: 12px; font-weight: 600; margin-bottom: 2px; }
.fn-cat { font-size: 12px; color: var(--muted); margin: 8px 0 3px; }
/* Mezery navíc musí být vidět (PROČISTIT, DÉLKA…). */
#grid td { white-space: pre; }
.code, .step-list li { white-space: pre-wrap; }
/* přelitá oblast dynamického pole */
#spillBox { position: absolute; display: none; z-index: 2; pointer-events: none; border: 2px dashed #1971c2; }
#formulaInput.ghost { color: #999; }
