/* =========================================================
   ТЕМЫ
   ========================================================= */
:root,
[data-theme="dark"] {
    --bg:         #0f1115;
    --bg-soft:    #151922;
    --bg-cell:    #0f1115;
    --bg-hover:   #1a1f2b;
    --line:       #262b36;
    --line-head:  #323949;
    --text:       #e6e9ef;
    --text-dim:   #8b93a3;
    --accent:     #4c8dff;
    --accent-soft:rgba(76, 141, 255, .14);
    --shadow:     0 18px 40px rgba(0, 0, 0, .5);
    --scroll:     #2c3341;
    --scroll-hov: #3a4354;
    --frozen-shadow: 6px 0 10px -6px rgba(0, 0, 0, .45);
    --frozen-shadow-x: 8px 0 12px -6px rgba(0, 0, 0, .5);
    --bg-panel:   #1b1e24;
    --border:     #2f343d;
    --input-bg:   #10141b;
    --ok:         #4ad08a;
    --err:        #ff6b6b;
}

[data-theme="light"] {
    --bg:         #ffffff;
    --bg-soft:    #f4f6fa;
    --bg-cell:    #ffffff;
    --bg-hover:   #eef2f9;
    --line:       #e2e6ee;
    --line-head:  #c9d0dd;
    --text:       #1b2130;
    --text-dim:   #6b7488;
    --accent:     #2f6fed;
    --accent-soft:rgba(47, 111, 237, .10);
    --shadow:     0 18px 40px rgba(28, 38, 60, .16);
    --scroll:     #c3cbd9;
    --scroll-hov: #a9b4c6;
    --frozen-shadow: 6px 0 10px -6px rgba(28, 38, 60, .18);
    --frozen-shadow-x: 8px 0 12px -6px rgba(28, 38, 60, .24);
    --bg-panel:   #ffffff;
    --border:     #dfe3e8;
    --input-bg:   #ffffff;
    --ok:         #1e9e63;
    --err:        #d43737;
}

:root {
    --head-h: 56px;
    --grid-h: 32px;
}

html { transition: background-color .18s ease; }
body, .topbar, .menu, .cell, .cell--head, .toolbar { transition: background-color .18s ease, color .18s ease; }

/* =========================================================
   БАЗА
   ========================================================= */
* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.4 -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

/* =========================================================
   ШАПКА
   ========================================================= */
.topbar {
    position: relative;
    z-index: 100;
    height: var(--head-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    background: var(--bg-soft);
    border-bottom: 1px solid var(--line);
}

.brand {
    color: var(--text);
    text-decoration: none;
    font-weight: 600;
    letter-spacing: .3px;
    font-size: 16px;
}

.topbar__right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
}

.burger {
    width: 40px; height: 40px;
    display: flex; flex-direction: column;
    justify-content: center; align-items: center; gap: 5px;
    padding: 0;
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
}
.burger:hover { background: var(--bg-hover); }
.burger span {
    display: block; width: 20px; height: 2px;
    background: var(--text);
    border-radius: 2px;
    transition: transform .22s ease, opacity .18s ease, background-color .18s ease;
}
.burger[aria-expanded="true"] span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.burger[aria-expanded="true"] span:nth-child(2) { opacity: 0; }
.burger[aria-expanded="true"] span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.menu {
    position: absolute;
    top: calc(var(--head-h) - 4px);
    left: 12px;
    min-width: 230px;
    padding: 6px;
    background: var(--bg-soft);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
    animation: menuIn .16s ease;
}
.menu[hidden] { display: none; }

@keyframes menuIn {
    from { opacity: 0; transform: translateY(-6px); }
    to   { opacity: 1; transform: translateY(0); }
}

.menu__list { list-style: none; margin: 0; padding: 0; }

.menu__link {
    display: block;
    padding: 10px 14px;
    border-radius: 8px;
    color: var(--text);
    text-decoration: none;
    font-size: 14px;
}
.menu__link:hover,
.menu__link:focus-visible { background: var(--accent-soft); outline: none; }

.menu__sep {
    height: 1px;
    margin: 6px 8px;
    background: var(--line);
}

.theme-switch { display: flex; gap: 4px; padding: 4px; }

.theme-switch__btn {
    flex: 1 1 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 10px;
    font: inherit;
    font-size: 13px;
    color: var(--text-dim);
    background: transparent;
    border: 1px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    transition: background-color .15s ease, color .15s ease, border-color .15s ease;
}
.theme-switch__btn:hover { background: var(--bg-hover); color: var(--text); }
.theme-switch__btn[aria-pressed="true"] {
    color: var(--text);
    background: var(--bg);
    border-color: var(--line-head);
    font-weight: 600;
}
.theme-switch__ico { font-size: 10px; line-height: 1; }

/* =========================================================
   ПАНЕЛЬ ПРОЕКТА
   ========================================================= */
.toolbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 16px;
    background: var(--bg-soft);
    border-bottom: 1px solid var(--line);
}
.toolbar__project {
    margin-left: auto;
    font-size: 13px;
    color: var(--text-dim);
    max-width: 40ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* =========================================================
   РАБОЧАЯ ОБЛАСТЬ
   ========================================================= */
.stage {
    height: calc(100vh - var(--head-h));
    padding: 10px 0 0;
    overflow: hidden;
}
body:has(.toolbar) .stage {
    height: calc(100vh - var(--head-h) - 60px);
}

.stage--with-toolbar { height: calc(100vh - var(--head-h) - 110px); }

.grid {
    height: 100%;
    overflow: auto;
    border-top: 1px solid var(--line);
    overscroll-behavior: contain;
}

.grid__inner {
    display: grid;
    grid-auto-flow: row;
    width: max-content;
    min-width: 100%;
}

.grid__row {
    display: flex;
    width: max-content;
    min-width: 100%;
    height: var(--grid-h);
}
.grid__row--head { position: sticky; top: 0; z-index: 30; }

.cell {
    flex: 0 0 var(--w);
    width: var(--w);
    height: 100%;
    display: flex;
    align-items: center;
    padding: 0 8px;
    border-right: 1px solid var(--line);
    border-bottom: 1px solid var(--line);
    background: var(--bg-cell);
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    font-size: 13px;
    color: var(--text);
}

.cell--head {
    background: var(--bg-soft);
    border-bottom: 1px solid var(--line-head);
    color: var(--text-dim);
    font-size: 12px;
    font-weight: 600;
    justify-content: center;
}

.c-spacer { background: var(--bg-soft); border-right-color: var(--line-head); padding: 0; }
.c-narrow { padding: 0; }
.c-unit   { justify-content: center; color: var(--text-dim); }
.c-period { justify-content: flex-end; font-variant-numeric: tabular-nums; }
.c-period-value { justify-content: flex-end; font-variant-numeric: tabular-nums; }

.grid__row--head .c-spacer,
.grid__row--head .c-narrow { background: var(--bg-soft); }

.cell:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

.c-marker, .c-param { border-right-color: var(--line-head); }

/* ---------- Уровни строк блока ---------- */
.cell--lvl1 {
    font-weight: 700;
    background: var(--accent-soft);
}
.cell--lvl2 {
    font-weight: 400;
    padding-left: 1em;
}
.cell--lvl2.c-unit,
.cell--lvl2.c-param,
.cell--lvl2.c-period { padding-left: 8px; }

.cell--lvl3 {
    font-weight: 300;
    font-size: 12px;
    padding-left: 2em;
}
.cell--lvl3.c-unit,
.cell--lvl3.c-param,
.cell--lvl3.c-period { padding-left: 8px; }

.cell--filled { color: var(--text); }

/* ---------- Инпуты внутри ячеек ---------- */
.cellinput {
    width: 100%;
    height: calc(var(--grid-h) - 8px);
    padding: 0 6px;
    font: inherit;
    font-size: 12px;
    color: var(--text);
    background: var(--input-bg);
    border: 1px solid var(--line);
    border-radius: 5px;
    outline: none;
    transition: border-color .12s ease, box-shadow .12s ease;
}
.cellinput:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}
.cellinput[inputmode="numeric"] { text-align: right; font-variant-numeric: tabular-nums; }

/* Скроллбар */
.grid::-webkit-scrollbar { height: 12px; width: 12px; }
.grid::-webkit-scrollbar-track { background: var(--bg); }
.grid::-webkit-scrollbar-thumb {
    background: var(--scroll);
    border-radius: 6px;
    border: 3px solid var(--bg);
}
.grid::-webkit-scrollbar-thumb:hover { background: var(--scroll-hov); }

@media (max-width: 640px) {
    :root { --head-h: 52px; }
    .cell { font-size: 12px; padding: 0 6px; }
    .cell--lvl2 { padding-left: .6em; }
    .cell--lvl3 { padding-left: 1.2em; }
}

/* =========================================================
   ЗАКРЕПЛЁННАЯ ЗОНА
   ========================================================= */
.cell--frozen {
    position: sticky;
    left: var(--left, 0);
    z-index: 20;
    background: var(--bg-cell);
    transition: background-color .18s ease, color .18s ease;
}
.grid__row--head .cell--frozen {
    z-index: 40;
    background: var(--bg-soft);
}
.cell--frozen.c-spacer,
.cell--frozen.c-narrow { background: var(--bg-soft); }

.grid__row--body .cell--frozen.c-edge { box-shadow: var(--frozen-shadow); }
body.is-scrolled-x .grid__row--body .cell--frozen.c-edge { box-shadow: var(--frozen-shadow-x); }

/* =========================================================
   МОДАЛЬНЫЕ ОКНА
   ========================================================= */
.fnmodal,
.modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.fnmodal[hidden],
.modal[hidden] { display: none; }

.fnmodal__overlay,
.modal__overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, .6);
    backdrop-filter: blur(2px);
}

.fnmodal__dialog,
.modal__dialog {
    position: relative;
    width: min(560px, 100%);
    max-height: min(80vh, 720px);
    display: flex;
    flex-direction: column;
    background: var(--bg-panel);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: 0 24px 60px rgba(0, 0, 0, .45);
    overflow: hidden;
    animation: modalIn .16s ease;
}

@keyframes modalIn {
    from { opacity: 0; transform: translateY(8px) scale(.99); }
    to   { opacity: 1; transform: none; }
}

.fnmodal__head,
.modal__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 16px 18px;
    border-bottom: 1px solid var(--border);
}
.fnmodal__title,
.modal__title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: .01em;
}
.fnmodal__close,
.modal__close {
    appearance: none;
    border: 0;
    background: transparent;
    color: inherit;
    font-size: 22px;
    line-height: 1;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    cursor: pointer;
    opacity: .7;
}
.fnmodal__close:hover,
.modal__close:hover { opacity: 1; background: rgba(127, 127, 127, .15); }

.fnmodal__hint {
    margin: 12px 18px 0;
    font-size: 12.5px;
    line-height: 1.45;
    opacity: .7;
}
.fnmodal__rowref { font-weight: 600; opacity: .9; }

.fnmodal__body,
.modal__body {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 10px 12px 14px;
}

.fnmodal__foot,
.modal__foot {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 18px 16px;
    border-top: 1px solid var(--border);
}

/* ---------- Список функций ---------- */
.fnlist { list-style: none; margin: 0; padding: 0; }

.fnlist__item {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-areas:
        "name  params"
        "desc  desc";
    gap: 2px 12px;
    padding: 10px 12px;
    border-radius: 10px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background .12s, border-color .12s;
}
.fnlist__item + .fnlist__item { margin-top: 2px; }

.fnlist__item:hover,
.fnlist__item:focus-visible {
    background: rgba(127, 127, 127, .12);
    border-color: var(--border);
    outline: none;
}

.fnlist__name { grid-area: name; font-size: 14px; font-weight: 600; }
.fnlist__desc { grid-area: desc; font-size: 12px; opacity: .65; line-height: 1.4; }
.fnlist__params {
    grid-area: params;
    font-size: 11px;
    opacity: .55;
    white-space: nowrap;
    align-self: center;
}

/* ---------- Кнопки ---------- */
.btn {
    appearance: none;
    border-radius: 9px;
    padding: 8px 14px;
    font-size: 13px;
    font-family: inherit;
    cursor: pointer;
    border: 1px solid var(--border);
    background: transparent;
    color: inherit;
    transition: background .12s, border-color .12s;
}
.btn:hover { background: rgba(127, 127, 127, .10); }
.btn--primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 500;
}
.btn--primary:hover { opacity: .92; background: var(--accent); }
.btn--ghost:hover { background: rgba(127, 127, 127, .12); }
.btn--auth {
    padding: 7px 14px;
    font-size: 13px;
    max-width: 30ch;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ---------- Формы ---------- */
.form { display: flex; flex-direction: column; gap: 12px; padding: 6px 6px 2px; }
.form__row { display: flex; flex-direction: column; gap: 6px; }
.form__label { font-size: 12.5px; color: var(--text-dim); }
.form__input {
    padding: 9px 12px;
    font: inherit;
    font-size: 13.5px;
    color: var(--text);
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 9px;
    outline: none;
    transition: border-color .12s ease, box-shadow .12s ease;
}
.form__input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}
.form__actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 2px; }
.form__links { font-size: 12.5px; }

.linklike {
    appearance: none;
    background: none;
    border: 0;
    padding: 0;
    font: inherit;
    color: var(--accent);
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.form__msg { min-height: 18px; font-size: 12.5px; line-height: 1.4; }
.form__msg--err { color: var(--err); }
.form__msg--ok  { color: var(--ok); }

/* ---------- Список проектов ---------- */
.projectlist { list-style: none; margin: 0; padding: 0; }
.projectlist__item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 11px 12px;
    border-radius: 10px;
    cursor: pointer;
    border: 1px solid transparent;
    transition: background .12s, border-color .12s;
}
.projectlist__item:hover,
.projectlist__item:focus-visible {
    background: rgba(127, 127, 127, .12);
    border-color: var(--border);
    outline: none;
}
.projectlist__name { font-size: 13.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.projectlist__date { font-size: 11.5px; color: var(--text-dim); white-space: nowrap; }
.projectlist__empty { padding: 14px 12px; font-size: 13px; color: var(--text-dim); }

/* ---------- Flash ---------- */
.flash {
    position: fixed;
    left: 50%;
    bottom: 28px;
    transform: translate(-50%, 20px);
    z-index: 2000;
    padding: 11px 18px;
    font-size: 13px;
    color: var(--text);
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: var(--shadow);
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease, transform .2s ease;
    max-width: min(560px, 90vw);
}
.flash--show { opacity: 1; transform: translate(-50%, 0); }

html.fnmodal-open,
html.fnmodal-open body { overflow: hidden; }
