/* ============================================================================
 * tables.css — Инструмент «Таблицы» (Excel-подобный). Только токены, 3 темы,
 * mobile-first. Скоуп — классы .tbl-*. Сетка скроллится внутри контейнера со
 * залипающими шапкой/первым столбцом.
 * ==========================================================================*/

#tab-tables.tab-content { padding: 0; height: 100%; }
/* Контейнер должен нести высоту, иначе .tbl-v2 height:100% резолвится в auto и
   сетка раздувается на все строки, а бар листов уезжает за нижний край. */
#tablesContainer { height: 100%; min-height: 0; }
.tbl-v2 { display: flex; flex-direction: column; height: 100%; min-height: 0; }

/* ── Бар редактора ─────────────────────────────────────────────────────────*/
.tbl-v2-bar {
    display: flex; align-items: center; gap: 8px;
    padding: 10px 14px; border-bottom: 1px solid var(--border);
    background: var(--surface); flex: 0 0 auto; flex-wrap: wrap;
}
.tbl-bar-spacer { flex: 1 1 auto; }
.tbl-title-input {
    font: inherit; font-size: 15px; font-weight: 600;
    color: var(--text); background: var(--bg);
    border: 1px solid var(--border); border-radius: 8px;
    padding: 7px 11px; min-width: 220px; max-width: 360px;
}
.tbl-title-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-tint); }
.tbl-save-state { font-size: 12px; color: var(--text-muted); min-width: 80px; text-align: right; }

.tbl-btn {
    font: inherit; font-size: 13px; cursor: pointer;
    color: var(--text); background: var(--surface);
    border: 1px solid var(--border); border-radius: 8px;
    padding: 7px 13px; transition: background .12s, border-color .12s;
}
.tbl-btn:hover { background: var(--fill); }
.tbl-btn--primary { background: var(--accent); color: #fff; border-color: var(--accent); }
.tbl-btn--primary:hover { background: var(--accent-deep); }
.tbl-btn--ghost { background: transparent; }
.tbl-btn--sm { padding: 5px 10px; font-size: 12px; }
.tbl-btn--danger { color: var(--status-failed); border-color: var(--border); }
.tbl-btn--danger:hover { background: var(--status-failed); color: #fff; border-color: var(--status-failed); }
.tbl-status-sel, .tbl-status {
    font: inherit; font-size: 12px; cursor: pointer;
    border: 1px solid var(--border); border-radius: 7px;
    padding: 5px 8px; color: var(--text); background: var(--surface);
}
.tbl-status--draft { background: var(--fill); color: var(--text-muted); }
.tbl-status--final { background: var(--accent-tint); color: var(--accent-deep); border-color: var(--accent-tint); }

/* ── Галерея ───────────────────────────────────────────────────────────────*/
.tbl-v2-gallery { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 16px 18px 28px; }
.tbl-galtop { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.tbl-galtabs { display: flex; gap: 6px; }
.tbl-galtab {
    font: inherit; font-size: 13px; cursor: pointer;
    background: transparent; border: 1px solid transparent; border-radius: 8px;
    padding: 7px 14px; color: var(--text-muted);
}
.tbl-galtab:hover { background: var(--fill); color: var(--text); }
.tbl-galtab.active { background: var(--accent-tint); color: var(--accent-deep); font-weight: 600; }
.tbl-search-input {
    font: inherit; font-size: 13px;
    border: 1px solid var(--border); border-radius: 9px;
    padding: 8px 13px; min-width: 240px; color: var(--text); background: var(--surface);
}
.tbl-search-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-tint); }

.tbl-cat { margin-bottom: 22px; }
.tbl-cat-title { font-size: 13px; font-weight: 700; color: var(--text); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.tbl-cat-count { color: var(--text-muted); font-weight: 500; }
.tbl-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 12px; }
.tbl-tile {
    display: flex; align-items: flex-start; gap: 12px; text-align: left;
    font: inherit; cursor: pointer;
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
    padding: 14px; transition: border-color .12s, box-shadow .12s, transform .08s;
}
.tbl-tile:hover { border-color: var(--accent); box-shadow: 0 4px 16px rgba(0,0,0,.06); transform: translateY(-1px); }
.tbl-tile-ico {
    flex: 0 0 auto; width: 40px; height: 40px; border-radius: 10px;
    display: flex; align-items: center; justify-content: center;
    background: var(--accent-tint); color: var(--accent-deep); font-size: 20px;
}
.tbl-tile--blank .tbl-tile-ico { background: var(--fill); color: var(--text-muted); }
.tbl-tile-body { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.tbl-tile-name { font-size: 14px; font-weight: 600; color: var(--text); }
.tbl-tile-desc { font-size: 12px; color: var(--text-muted); line-height: 1.35; }

/* ── Мои таблицы ───────────────────────────────────────────────────────────*/
.tbl-my { display: flex; flex-direction: column; gap: 8px; }
.tbl-my-row {
    display: flex; align-items: center; gap: 12px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px;
}
.tbl-my-ico {
    flex: 0 0 auto; width: 34px; height: 34px; border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    background: var(--accent-tint); color: var(--accent-deep); font-size: 17px;
}
.tbl-my-main { flex: 1 1 auto; min-width: 0; }
.tbl-my-title { font-size: 14px; font-weight: 600; color: var(--text); }
.tbl-my-id { color: var(--text-muted); font-weight: 500; font-size: 12px; }
.tbl-my-meta { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

.tbl-hint { color: var(--text-muted); font-size: 13px; padding: 16px 4px; }

/* ── Редактор: тулбар, листы ───────────────────────────────────────────────*/
.tbl-v2-editor { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; }
.tbl-toolbar {
    display: flex; align-items: center; gap: 6px;
    padding: 7px 12px; border-bottom: 1px solid var(--border); background: var(--surface); flex: 0 0 auto;
}
.tbl-tbtn {
    font: inherit; font-size: 13px; cursor: pointer;
    background: var(--surface); border: 1px solid var(--border); border-radius: 7px;
    padding: 5px 11px; color: var(--text);
}
.tbl-tbtn:hover { background: var(--fill); }
.tbl-tsep { width: 1px; height: 20px; background: var(--border); margin: 0 4px; }
.tbl-toolbar { flex-wrap: wrap; }
.tbl-fbtn {
    font: inherit; font-size: 13px; cursor: pointer; min-width: 30px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 7px;
    padding: 5px 9px; color: var(--text); line-height: 1;
}
.tbl-fbtn:hover { background: var(--fill); }
.tbl-fbtn.active { background: var(--accent-tint); border-color: var(--accent); color: var(--accent-deep); }
.tbl-fbtn b { font-weight: 800; }
.tbl-fbtn i { font-style: italic; }
.tbl-fbtn u { text-decoration: underline; }
.tbl-fbtn[disabled] { opacity: .45; cursor: not-allowed; }
.tbl-fbtn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tbl-sel {
    font: inherit; font-size: 12px; cursor: pointer; max-width: 150px;
    border: 1px solid var(--border); border-radius: 7px; padding: 5px 7px;
    background: var(--surface); color: var(--text);
}
.tbl-sel:hover { background: var(--fill); }
.tbl-sel:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tbl-sel--sm { max-width: 58px; }
.tbl-color-wrap { position: relative; display: inline-flex; align-items: center; }
.tbl-color-wrap input[type="color"] {
    width: 30px; height: 28px; padding: 0; border: 1px solid var(--border);
    border-radius: 7px; background: var(--surface); cursor: pointer;
}
.tbl-color-wrap .tbl-color-lbl { position: absolute; bottom: 1px; left: 0; right: 0; text-align: center; font-size: 8px; pointer-events: none; color: var(--text-muted); }

/* Контекст-меню (правый клик по ячейке/шапке строки или колонки) */
.tbl-ctx {
    position: fixed; z-index: 9999; min-width: 190px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
    box-shadow: 0 8px 28px rgba(0,0,0,.16); padding: 5px; display: none;
}
.tbl-ctx-item {
    display: block; width: 100%; text-align: left; font: inherit; font-size: 13px;
    cursor: pointer; background: transparent; border: none; border-radius: 6px;
    padding: 7px 11px; color: var(--text);
}
.tbl-ctx-item:hover { background: var(--accent-tint); color: var(--accent-deep); }
.tbl-ctx-item--danger:hover { background: var(--status-failed); color: #fff; }
.tbl-ctx-sep { height: 1px; background: var(--border); margin: 4px 2px; }

/* Хват изменения ширины колонки */
.tbl-colresize {
    position: absolute; top: 0; right: -3px; width: 7px; height: 100%;
    cursor: col-resize; z-index: 5;
}
.tbl-colhead { position: relative; }

/* Воронка-фильтр в шапке столбца (строка 0) */
.tbl-funnel {
    display: inline-block; margin-left: 6px; cursor: pointer; font-size: 10px;
    color: var(--text-muted); border-radius: 4px; padding: 0 3px;
}
.tbl-funnel:hover { background: var(--surface); color: var(--accent-deep); }
.tbl-funnel.active { color: var(--accent); font-weight: 700; }
/* Тогл сворачивания группы строк/столбцов */
.tbl-grp-tog {
    display: inline-flex; align-items: center; justify-content: center;
    width: 14px; height: 14px; margin-right: 4px; cursor: pointer;
    border: 1px solid var(--accent); border-radius: 3px; color: var(--accent-deep);
    font-size: 11px; line-height: 1; background: var(--surface); vertical-align: middle;
}
.tbl-grp-tog:hover { background: var(--accent-tint); }

/* Поповер фильтра / диалога сортировки */
.tbl-pop {
    position: fixed; z-index: 9999; display: none; width: 260px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
    box-shadow: 0 10px 32px rgba(0,0,0,.18); padding: 8px;
}
.tbl-pop-title { font-size: 13px; font-weight: 700; color: var(--text); padding: 2px 4px 8px; }
.tbl-pop-search { margin-bottom: 6px; }
.tbl-pop-search input { width: 100%; box-sizing: border-box; font: inherit; font-size: 12px; padding: 6px 8px; border: 1px solid var(--border); border-radius: 7px; background: var(--bg); color: var(--text); }
.tbl-pop-list { max-height: 220px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; padding: 4px; margin-bottom: 8px; }
.tbl-pop-row { display: flex; align-items: center; gap: 7px; padding: 5px 6px; font-size: 13px; color: var(--text); cursor: pointer; border-radius: 6px; }
.tbl-pop-row:hover { background: var(--fill); }
.tbl-pop-all { border-bottom: 1px solid var(--border); margin-bottom: 4px; padding-bottom: 7px; }
.tbl-pop-btns { display: flex; justify-content: flex-end; gap: 6px; }
.tbl-sort-lvl { gap: 6px; }
.tbl-sort-lbl { font-size: 12px; color: var(--text-muted); min-width: 54px; }
.tbl-sort-col { flex: 1 1 auto; }

/* Кнопка удаления активного листа */
.tbl-sheet-del {
    margin-left: 6px; font-size: 12px; line-height: 1; cursor: pointer;
    background: transparent; border: none; color: var(--text-muted); padding: 0 2px;
}
.tbl-sheet-del:hover { color: var(--status-failed); }
.tbl-cellref {
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
    color: var(--text-muted); min-width: 54px; padding: 4px 8px;
    background: var(--fill); border-radius: 6px;
}

.tbl-sheets {
    display: flex; align-items: center; gap: 4px; flex-wrap: nowrap; overflow-x: auto;
    padding: 6px 10px; border-top: 1px solid var(--border); background: var(--surface); flex: 0 0 auto;
}
.tbl-sheet {
    font: inherit; font-size: 12.5px; cursor: pointer; white-space: nowrap;
    background: var(--fill); border: 1px solid transparent; border-radius: 7px 7px 0 0;
    padding: 6px 13px; color: var(--text-muted);
}
.tbl-sheet:hover { color: var(--text); }
.tbl-sheet.active { background: var(--surface); color: var(--accent-deep); font-weight: 600; border-color: var(--border); border-bottom-color: var(--surface); }
.tbl-sheet-add {
    font: inherit; font-size: 16px; line-height: 1; cursor: pointer;
    background: transparent; border: none; color: var(--text-muted); padding: 4px 9px; border-radius: 6px;
}
.tbl-sheet-add:hover { background: var(--fill); color: var(--text); }

/* ── Сетка ─────────────────────────────────────────────────────────────────*/
.tbl-grid-wrap { flex: 1 1 auto; min-height: 0; width: 100%; overflow: auto; outline: none; background: var(--bg); position: relative; }
/* min-width:100% — узкий шаблон (мало колонок) всё равно заполняет область,
   поэтому смена шаблона не «прыгает» внешним размером. */
.tbl-grid { border-collapse: separate; border-spacing: 0; table-layout: fixed; font-size: 13px; min-width: 100%; }
.tbl-grid th, .tbl-grid td {
    border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
    height: 30px; padding: 0; box-sizing: border-box;
    border-radius: 0;   /* Шаг 3: ячейки строго прямоугольные */
}
/* Шапка колонок (A,B,C) — залипает сверху */
.tbl-grid thead th {
    position: sticky; top: 0; z-index: 3;
    background: var(--fill); color: var(--text-muted); font-weight: 600;
    text-align: center; font-size: 12px;
}
/* Угол */
.tbl-corner { position: sticky; left: 0; top: 0; z-index: 4 !important; background: var(--fill); }
/* Нумерация строк — залипает слева */
.tbl-rowhead {
    position: sticky; left: 0; z-index: 2;
    background: var(--fill); color: var(--text-muted); font-weight: 600;
    text-align: center; font-size: 12px;
}
.tbl-cell {
    background: var(--surface); color: var(--text);
    padding: 0 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    cursor: cell; vertical-align: middle; max-width: 0;
}
.tbl-cell--head { background: var(--accent-tint); color: var(--accent-deep); font-weight: 600; }
/* Объединённая ячейка: по умолчанию по центру, текст переносится */
.tbl-cell--merged { text-align: center; vertical-align: middle; white-space: normal; }
/* Шаг 2: ховер ячеек/шапок по токенам (не перекрывает выделение и правку) */
.tbl-grid td.tbl-cell:hover:not(.sel):not(.sel-active):not(.editing) { background: var(--fill); }
.tbl-grid td.tbl-cell--head:hover:not(.sel):not(.sel-active):not(.editing) { background: var(--accent-mid); color: #fff; }
.tbl-grid th.tbl-colhead:hover, .tbl-grid th.tbl-rowhead:hover { background: var(--accent-tint); color: var(--accent-deep); }
/* Шаг 3: прямоугольное выделение — заливка + ring без скруглений */
.tbl-cell.sel { background: var(--accent-tint); }
.tbl-cell--head.sel { background: var(--accent-mid); color: #fff; }
.tbl-cell.sel-active {
    box-shadow: inset 0 0 0 2px var(--accent); background: var(--surface); border-radius: 0;
}
.tbl-cell--head.sel-active { background: var(--accent-tint); }
.tbl-cell.editing { box-shadow: inset 0 0 0 2px var(--accent); padding: 0; border-radius: 0; }
.tbl-cell-input {
    width: 100%; height: 100%; box-sizing: border-box;
    border: none; outline: none; font: inherit; font-size: 13px;
    padding: 0 8px; color: var(--text); background: var(--surface);
}
/* Перебиваем глобальный input[type=text]{border-radius:999px !important} (pill):
   нужна специфичность выше (0,1,1), поэтому добавляем предок .tbl-grid. */
.tbl-grid input.tbl-cell-input { border-radius: 0 !important; }

/* ── Тёмная тема: кнопки на акценте используют тёмный ink (не белый) ────────*/
[data-theme="dark"] .tbl-btn--primary { color: var(--on-accent); }
[data-theme="dark"] .tbl-btn--primary:hover { background: var(--accent-mid); }
[data-theme="dark"] .tbl-cell--head.sel { background: var(--accent-mid); color: var(--on-accent); }

/* ── Мобильный: компактнее; редактирование возможно, но узкие колонки ───────*/
@media (max-width: 768px) {
    .tbl-v2-bar { gap: 6px; padding: 8px 10px; }
    .tbl-title-input { min-width: 130px; font-size: 14px; }
    .tbl-v2-gallery { padding: 12px 12px 24px; }
    .tbl-tiles { grid-template-columns: 1fr; }
    .tbl-search-input { min-width: 0; width: 100%; }
    .tbl-galtop { gap: 8px; }
    .tbl-grid { font-size: 12px; }
}
