/* StartQL — дизайн-система. Тёмная тема по умолчанию, светлая — [data-theme="light"] */

:root {
  --bg: #0b0d12;
  --surface: #12151c;
  --surface-2: #181c25;
  --surface-3: #202533;
  --border: #252b38;
  --border-strong: #323a4b;
  --text: #e8ebf2;
  --text-2: #b4bccd;
  --muted: #7f889c;
  --accent: #7c6cff;
  --accent-hover: #8d7fff;
  --accent-soft: rgba(124, 108, 255, 0.14);
  --accent-text: #b3aaff;
  --mint: #2dd4a7;
  --mint-soft: rgba(45, 212, 167, 0.13);
  --danger: #ff5d6c;
  --danger-soft: rgba(255, 93, 108, 0.12);
  --warn: #fbbf24;
  --warn-soft: rgba(251, 191, 36, 0.14);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.25);
  --radius: 14px;
  --radius-sm: 9px;
  --topbar-h: 60px;
  --bottomnav-h: 64px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --mono: 'JetBrains Mono', 'Fira Code', ui-monospace, Consolas, monospace;
  color-scheme: dark;
}

[data-theme="light"] {
  --bg: #f5f6fa;
  --surface: #ffffff;
  --surface-2: #f1f3f8;
  --surface-3: #e7eaf2;
  --border: #e2e5ee;
  --border-strong: #cfd4e0;
  --text: #141824;
  --text-2: #3f4658;
  --muted: #6b7386;
  --accent: #5b4bff;
  --accent-hover: #4a39f5;
  --accent-soft: rgba(91, 75, 255, 0.1);
  --accent-text: #4a39f5;
  --mint: #0fa77f;
  --mint-soft: rgba(15, 167, 127, 0.1);
  --danger: #e5364a;
  --danger-soft: rgba(229, 54, 74, 0.08);
  --warn: #c98a00;
  --warn-soft: rgba(251, 191, 36, 0.16);
  --shadow: 0 10px 30px rgba(20, 24, 36, 0.1);
  --shadow-sm: 0 2px 8px rgba(20, 24, 36, 0.06);
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent-text); }
code, pre, kbd { font-family: var(--mono); }
[hidden] { display: none !important; }

::selection { background: var(--accent-soft); }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 10px; border: 2px solid transparent; background-clip: padding-box; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Кнопки и поля ---------- */

.btn-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: 500 14px/1 var(--font);
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s, border-color .15s, transform .08s, color .15s;
  text-decoration: none;
  user-select: none;
}
.btn-x:hover { background: var(--surface-3); border-color: var(--border-strong); }
.btn-x:active { transform: translateY(1px); }
.btn-x:disabled { opacity: .5; cursor: not-allowed; }
.btn-x:focus-visible, .input-x:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn-x .bi { font-size: 16px; }

.btn-primary-x { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary-x:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-mint { background: var(--mint); border-color: var(--mint); color: #06241b; }
.btn-mint:hover { filter: brightness(1.06); background: var(--mint); border-color: var(--mint); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); border-color: transparent; }
.btn-danger-x { color: var(--danger); }
.btn-danger-x:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-gold { background: linear-gradient(135deg, #fcd34d, #f59e0b); border: none; color: #2a1d00; font-weight: 600; }
.btn-gold:hover { filter: brightness(1.05); }
.btn-sm-x { height: 32px; padding: 0 10px; font-size: 13px; }
.btn-lg-x { height: 46px; padding: 0 20px; font-size: 15px; }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
  font-size: 17px;
  transition: background .15s, color .15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.mobile-only { display: none; }
@media (max-width: 860px) { .icon-btn.mobile-only { display: inline-grid; } }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

.input-x {
  width: 100%;
  height: 42px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: 15px var(--font);
  transition: border-color .15s, box-shadow .15s;
}
.input-x::placeholder { color: var(--muted); }
.input-x:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
select.input-x { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 18px, calc(100% - 13px) 18px; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 32px; }
.field-label { display: block; font-size: 13px; font-weight: 500; color: var(--text-2); margin-bottom: 6px; }
.field-hint { font-size: 12px; color: var(--muted); margin-top: 6px; }
.field { margin-bottom: 16px; }

kbd {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 5px;
  border: 1px solid var(--border-strong);
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 11px;
  line-height: 1.5;
}

.badge-x {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-3);
  color: var(--text-2);
}
.badge-x.live { background: var(--mint-soft); color: var(--mint); }
.badge-x.live::before { content: ''; width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: pulse 1.6s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- Каркас приложения ---------- */

.app { display: flex; flex-direction: column; height: 100dvh; }

.topbar {
  height: var(--topbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 30;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.02em;
  color: var(--text);
  text-decoration: none;
  margin-right: 12px;
}
.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--accent), #b06cff);
  color: #fff;
  font-size: 16px;
  box-shadow: 0 4px 14px rgba(124, 108, 255, .35);
}
.brand b { color: var(--accent-text); font-weight: 700; }

.top-nav { display: flex; gap: 4px; }
.top-nav a {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  text-decoration: none;
  font-weight: 500;
  font-size: 14px;
  transition: background .15s, color .15s;
}
.top-nav a:hover { background: var(--surface-2); color: var(--text); }
.top-nav a.active { background: var(--accent-soft); color: var(--accent-text); }

.topbar-spacer { flex: 1; }

.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 6px 4px 4px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
  color: var(--text);
  font: 500 14px var(--font);
}
.user-chip:hover { border-color: var(--border-strong); }
.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #2dd4a7, #3b82f6);
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  flex-shrink: 0;
}
.user-chip .name { max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.menu {
  position: absolute;
  top: calc(var(--topbar-h) - 4px);
  right: 12px;
  min-width: 240px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  z-index: 50;
  animation: pop .14s ease;
}
.menu-head { padding: 10px 12px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
.menu-head .n { font-weight: 600; }
.menu-head .m { font-size: 12px; color: var(--muted); }
.menu button, .menu a {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: 14px var(--font);
  cursor: pointer;
  text-align: left;
  text-decoration: none;
}
.menu button:hover, .menu a:hover { background: var(--surface-2); }
.menu .danger { color: var(--danger); }
@keyframes pop { from { opacity: 0; transform: translateY(-4px) scale(.98); } }

.main { flex: 1; min-height: 0; position: relative; }
.view { height: 100%; display: none; }
.view.active { display: flex; }

.bottom-nav { display: none; }

/* ---------- Рабочая область с БД ---------- */

.db-sidebar {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 10px;
}
.panel-title { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.db-search { padding: 0 12px 10px; }
.db-search .input-x { height: 36px; font-size: 14px; }
.db-list { flex: 1; overflow-y: auto; padding: 0 8px 12px; }
.db-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-2);
  font-size: 14px;
  transition: background .12s, color .12s;
}
.db-item:hover { background: var(--surface-2); color: var(--text); }
.db-item.active { background: var(--accent-soft); color: var(--accent-text); font-weight: 600; }
.db-item .bi-database { opacity: .7; }
.db-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.db-item .icon-btn { width: 28px; height: 28px; font-size: 14px; opacity: 0; }
.db-item:hover .icon-btn, .db-item.active .icon-btn { opacity: 1; }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }

.ws-main { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; }

.ws-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  min-height: 56px;
}
.ws-title { display: flex; align-items: center; gap: 10px; min-width: 0; font-weight: 600; font-size: 16px; }
.ws-title .bi { color: var(--accent-text); }
.ws-title span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ws-toolbar .grow { flex: 1; }

.ws-body { flex: 1; min-height: 0; display: flex; }

.schema-panel {
  width: 250px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 10px 8px 16px;
  background: var(--surface);
}
.schema-panel.collapsed { display: none; }
.schema-table { margin-bottom: 2px; }
.schema-table-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}
.schema-table-head:hover { background: var(--surface-2); }
.schema-table-head .chev { transition: transform .15s; color: var(--muted); font-size: 12px; }
.schema-table.open .chev { transform: rotate(90deg); }
.schema-table-head .count { margin-left: auto; font-size: 12px; color: var(--muted); font-weight: 400; }
.schema-table-head .kind { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--warn); }
.schema-cols { display: none; padding: 2px 0 6px 28px; }
.schema-table.open .schema-cols { display: block; }
.schema-col {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 3px 8px;
  border-radius: 6px;
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text-2);
  cursor: pointer;
}
.schema-col:hover { background: var(--surface-2); color: var(--text); }
.schema-col .t { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.schema-col .pk { color: var(--warn); margin-right: 4px; }
.schema-empty { padding: 20px 12px; color: var(--muted); font-size: 13px; text-align: center; }

.editor-col { flex: 1; min-width: 0; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }

.editor-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  box-shadow: var(--shadow-sm);
  flex-shrink: 0;
}
.editor-card:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.editor-card .CodeMirror { height: 260px; }
.editor-card.resizable { resize: vertical; }

.run-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; flex-shrink: 0; }
.run-bar .status { margin-left: auto; font-size: 13px; color: var(--muted); }
.run-bar .status.ok { color: var(--mint); }

/* Результаты */
.results { display: flex; flex-direction: column; gap: 12px; }
.result-card { border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.result-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 8px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-2);
}
.result-head .grow { flex: 1; }
.result-head b { color: var(--text); }
.table-wrap { overflow: auto; max-height: 460px; }
table.data {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-size: 13.5px;
}
table.data th {
  position: sticky;
  top: 0;
  background: var(--surface-2);
  color: var(--text-2);
  font-weight: 600;
  text-align: left;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  z-index: 1;
}
table.data td {
  padding: 7px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  font-variant-numeric: tabular-nums;
}
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover td { background: var(--surface-2); }
table.data td.num { text-align: right; font-family: var(--mono); font-size: 12.5px; }
table.data td.null { color: var(--muted); font-style: italic; }
table.data .rownum { color: var(--muted); width: 1%; text-align: right; font-size: 12px; }
.result-note { padding: 10px 14px; font-size: 13px; color: var(--warn); border-top: 1px solid var(--border); }
.result-empty { padding: 18px 14px; color: var(--muted); font-size: 14px; }

.notice {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 12px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 14px;
  animation: pop .18s ease;
}
.notice .bi { font-size: 18px; line-height: 1.2; }
.notice.error { border-color: color-mix(in srgb, var(--danger) 40%, transparent); background: var(--danger-soft); }
.notice.error .bi { color: var(--danger); }
.notice.success { border-color: color-mix(in srgb, var(--mint) 40%, transparent); background: var(--mint-soft); }
.notice.success .bi { color: var(--mint); }
.notice.info .bi { color: var(--accent-text); }
.notice pre { margin: 4px 0 0; white-space: pre-wrap; font-size: 13px; color: var(--text-2); }
.notice .title { font-weight: 600; }

/* Панель сохранённых запросов / истории */
.side-panel {
  width: 280px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--border);
  background: var(--surface);
  min-height: 0;
}
.side-panel.collapsed { display: none; }
.seg {
  display: flex;
  gap: 4px;
  padding: 4px;
  margin: 0 12px 10px;
  border-radius: 10px;
  background: var(--surface-2);
}
.seg button {
  flex: 1;
  height: 30px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--text-2);
  font: 500 13px var(--font);
  cursor: pointer;
}
.seg button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow-sm); }
.side-list { flex: 1; overflow-y: auto; padding: 0 10px 12px; }
.q-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 10px;
  margin-bottom: 8px;
  cursor: pointer;
  background: var(--surface);
  transition: border-color .12s, background .12s;
}
.q-item:hover { border-color: var(--border-strong); background: var(--surface-2); }
.q-item.active { border-color: var(--accent); }
.q-item-head { display: flex; align-items: center; gap: 6px; }
.q-item-head .n { flex: 1; font-weight: 500; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.q-item-head .icon-btn { width: 26px; height: 26px; font-size: 13px; }
.q-item code {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.q-item .when { font-size: 11px; color: var(--muted); margin-top: 4px; }
.side-empty { padding: 24px 12px; color: var(--muted); font-size: 13px; text-align: center; }

/* Пустое состояние */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 20px;
  gap: 14px;
}
.empty-illu {
  width: 88px;
  height: 88px;
  border-radius: 26px;
  display: grid;
  place-items: center;
  font-size: 40px;
  background: var(--accent-soft);
  color: var(--accent-text);
}
.empty-state h2 { margin: 0; font-size: 22px; letter-spacing: -0.02em; }
.empty-state p { margin: 0; color: var(--muted); max-width: 420px; }
.empty-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }

/* Доска */
.board-view { flex-direction: column; padding: 16px; gap: 12px; overflow: hidden; }
.board-head { display: flex; align-items: center; gap: 12px; }
.board-head h2 { margin: 0; font-size: 18px; }
.board-card { flex: 1; min-height: 0; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.board-card .CodeMirror { height: 100%; font-size: 18px; }
.board-protected, .board-protected .CodeMirror, .board-protected textarea { user-select: none; -webkit-user-select: none; }
.board-protected .CodeMirror-cursor, .board-protected .CodeMirror-selected { visibility: hidden; }

/* ---------- CodeMirror ---------- */

.CodeMirror {
  font-family: var(--mono);
  font-size: 15px;
  line-height: 1.6;
  background: var(--surface) !important;
  color: var(--text);
}
.CodeMirror-gutters { background: var(--surface) !important; border-right: 1px solid var(--border) !important; }
.CodeMirror-linenumber { color: var(--muted) !important; opacity: .7; }
.CodeMirror-cursor { border-left: 2px solid var(--accent) !important; }
.CodeMirror-selected, .CodeMirror-focused .CodeMirror-selected { background: var(--accent-soft) !important; }
.CodeMirror-activeline-background { background: rgba(127, 127, 127, .06) !important; }
.CodeMirror pre.CodeMirror-placeholder { color: var(--muted); }
.cm-s-startql .cm-keyword { color: #c792ea; font-weight: 600; }
.cm-s-startql .cm-builtin, .cm-s-startql .cm-type { color: #82aaff; }
.cm-s-startql .cm-string, .cm-s-startql .cm-string-2 { color: #c3e88d; }
.cm-s-startql .cm-number { color: #f78c6c; }
.cm-s-startql .cm-comment { color: #6b7394; font-style: italic; }
.cm-s-startql .cm-operator { color: #89ddff; }
.cm-s-startql .cm-variable-2 { color: #ffcb6b; }
.cm-s-startql .cm-atom { color: #ff9cac; }
[data-theme="light"] .cm-s-startql .cm-keyword { color: #7c3aed; }
[data-theme="light"] .cm-s-startql .cm-builtin, [data-theme="light"] .cm-s-startql .cm-type { color: #2563eb; }
[data-theme="light"] .cm-s-startql .cm-string, [data-theme="light"] .cm-s-startql .cm-string-2 { color: #15803d; }
[data-theme="light"] .cm-s-startql .cm-number { color: #c2410c; }
[data-theme="light"] .cm-s-startql .cm-comment { color: #8a92a6; }
[data-theme="light"] .cm-s-startql .cm-operator { color: #0369a1; }
[data-theme="light"] .cm-s-startql .cm-variable-2 { color: #a16207; }
.CodeMirror-hints {
  background: var(--surface) !important;
  border: 1px solid var(--border) !important;
  border-radius: 10px;
  box-shadow: var(--shadow) !important;
  font-family: var(--mono) !important;
  font-size: 13px !important;
  padding: 4px !important;
  z-index: 2000;
}
.CodeMirror-hint { color: var(--text) !important; border-radius: 6px; padding: 4px 8px !important; }
li.CodeMirror-hint-active { background: var(--accent) !important; color: #fff !important; }

/* ---------- Модалки (Bootstrap) под тему ---------- */

.modal-content {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
}
.modal-header, .modal-footer { border-color: var(--border); }
.modal-header { padding: 18px 20px 12px; border-bottom: 0; }
.modal-body { padding: 8px 20px 12px; }
.modal-footer { padding: 12px 20px 18px; border-top: 0; gap: 8px; }
.modal-footer > * { margin: 0; }
.modal-title { font-weight: 600; font-size: 18px; }
.modal-backdrop.show { opacity: .6; }
.btn-close { filter: var(--close-filter, invert(1) grayscale(1)); }
[data-theme="light"] .btn-close { filter: none; }
.template-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.template-opt {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
  background: var(--surface-2);
  transition: border-color .12s, background .12s;
}
.template-opt:hover { border-color: var(--border-strong); }
.template-opt.active { border-color: var(--accent); background: var(--accent-soft); }
.template-opt .t { font-weight: 600; overflow-wrap: anywhere; }
.template-opt .d { font-size: 12px; color: var(--muted); }

/* ---------- Тосты ---------- */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 3000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast-x {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 240px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface-3);
  color: var(--text);
  border: 1px solid var(--border-strong);
  box-shadow: var(--shadow);
  font-size: 14px;
  pointer-events: auto;
  animation: toastIn .2s ease;
}
.toast-x.success .bi { color: var(--mint); }
.toast-x.error .bi { color: var(--danger); }
.toast-x.hide { animation: toastOut .2s ease forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(8px); } }
@keyframes toastOut { to { opacity: 0; transform: translateY(8px); } }

/* ---------- Уроки ---------- */

.lessons-view { overflow-y: auto; display: none; }
.lessons-view.active { display: block; }
.lessons-wrap { max-width: 1040px; margin: 0 auto; padding: 28px 20px 48px; }

.lessons-hero {
  position: relative;
  overflow: hidden;
  border-radius: 22px;
  padding: 28px;
  background:
    radial-gradient(600px 240px at 100% 0%, rgba(176, 108, 255, .28), transparent 60%),
    radial-gradient(500px 200px at 0% 100%, rgba(45, 212, 167, .2), transparent 60%),
    var(--surface);
  border: 1px solid var(--border);
  display: flex;
  gap: 24px;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-bottom: 28px;
}
.lessons-hero h1 { margin: 0 0 6px; font-size: 28px; letter-spacing: -0.03em; }
.lessons-hero p { margin: 0; color: var(--text-2); max-width: 520px; }
.hero-progress { min-width: 240px; }
.hero-progress .nums { display: flex; align-items: baseline; gap: 6px; margin-bottom: 8px; }
.hero-progress .big { font-size: 32px; font-weight: 700; letter-spacing: -0.03em; }
.progress-track { height: 8px; border-radius: 99px; background: var(--surface-3); overflow: hidden; }
.progress-fill { height: 100%; border-radius: 99px; background: linear-gradient(90deg, var(--accent), var(--mint)); transition: width .5s ease; }

.module { margin-bottom: 26px; }
.module-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 12px; }
.module-num {
  width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent-text);
  font-weight: 700; font-size: 14px; flex-shrink: 0;
  align-self: center;
}
.module-head h2 { margin: 0; font-size: 19px; letter-spacing: -0.02em; }
.module-head p { margin: 0; color: var(--muted); font-size: 14px; }
.lesson-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); gap: 12px; }
.lesson-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  text-decoration: none;
  color: var(--text);
  transition: border-color .15s, transform .15s, box-shadow .15s;
}
.lesson-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: var(--shadow); }
.lesson-card .top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.lesson-card .title { font-weight: 600; font-size: 15.5px; }
.lesson-card .meta { display: flex; gap: 12px; font-size: 13px; color: var(--muted); }
.lesson-card .dots { display: flex; gap: 4px; }
.lesson-card .dot { width: 18px; height: 5px; border-radius: 99px; background: var(--surface-3); }
.lesson-card .dot.on { background: var(--mint); }
.lesson-card.done { border-color: color-mix(in srgb, var(--mint) 45%, var(--border)); }
.check-circle { color: var(--mint); font-size: 18px; }

/* Страница урока */
.lesson-page { max-width: 1320px; margin: 0 auto; padding: 18px 20px 48px; }
.crumbs { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); margin-bottom: 10px; flex-wrap: wrap; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--text); }
.lesson-title-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.lesson-title-row h1 { margin: 0; font-size: 26px; letter-spacing: -0.03em; }

.lesson-tabs { display: none; }

.lesson-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr); gap: 20px; align-items: start; }

.theory {
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  padding: 22px 24px;
  font-size: 15.5px;
  line-height: 1.7;
  color: var(--text-2);
  position: sticky;
  top: 16px;
  max-height: calc(100dvh - var(--topbar-h) - 32px);
  overflow-y: auto;
}
.theory h4 { color: var(--text); font-size: 16px; margin: 22px 0 8px; }
.theory p { margin: 0 0 12px; }
.theory ul { padding-left: 20px; margin: 0 0 12px; }
.theory li { margin-bottom: 4px; }
.theory b { color: var(--text); }
.theory code { font-size: 13.5px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--accent-text); }
.theory pre {
  margin: 0 0 14px;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.6;
}
.theory pre code { padding: 0; background: none; color: var(--text); }
.lesson-tip {
  margin: 14px 0;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--warn-soft);
  border-left: 3px solid var(--warn);
  color: var(--text);
  font-size: 14.5px;
}

.practice { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.stepper { display: flex; gap: 8px; flex-wrap: wrap; }
.step {
  min-width: 40px;
  height: 36px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  font: 600 14px var(--font);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.step:hover { border-color: var(--border-strong); }
.step.current { border-color: var(--accent); color: var(--accent-text); background: var(--accent-soft); }
.step.solved { color: var(--mint); }
.step.solved.current { border-color: var(--mint); background: var(--mint-soft); }

.task-card {
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.task-label { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); display: flex; align-items: center; gap: 8px; }
.task-prompt { font-size: 16px; line-height: 1.6; color: var(--text); }
.task-prompt code { font-size: 14px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--accent-text); }
.task-card .editor-card .CodeMirror { height: 170px; }
.hint-box { padding: 10px 14px; border-radius: 12px; background: var(--accent-soft); font-size: 14px; color: var(--text); }
.solution-box pre { margin: 6px 0 0; white-space: pre-wrap; font-size: 13.5px; }

.dataset-box { border: 1px solid var(--border); border-radius: 14px; background: var(--surface); }
.dataset-box summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 500;
  font-size: 14px;
}
.dataset-box summary::-webkit-details-marker { display: none; }
.dataset-box summary .chev { margin-left: auto; transition: transform .15s; color: var(--muted); }
.dataset-box[open] summary .chev { transform: rotate(180deg); }
.dataset-tables { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; padding: 0 14px 14px; }
.ds-table { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; background: var(--surface-2); }
.ds-table .n { font-family: var(--mono); font-weight: 600; font-size: 13px; display: flex; align-items: center; justify-content: space-between; }
.ds-table .n button { border: 0; background: none; color: var(--accent-text); font-size: 12px; cursor: pointer; padding: 0; }
.ds-table .c { font-family: var(--mono); font-size: 12px; color: var(--muted); }

.lesson-nav { display: flex; justify-content: space-between; gap: 12px; margin-top: 28px; flex-wrap: wrap; }
.lesson-nav a { flex: 1; min-width: 200px; max-width: 420px; padding: 14px 16px; border-radius: 14px; border: 1px solid var(--border); background: var(--surface); text-decoration: none; color: var(--text); }
.lesson-nav a:hover { border-color: var(--accent); }
.lesson-nav a .k { font-size: 12px; color: var(--muted); }
.lesson-nav a.next { text-align: right; margin-left: auto; }

.success-burst { animation: burst .5s ease; }
@keyframes burst { 0% { transform: scale(.96); } 60% { transform: scale(1.02); } 100% { transform: scale(1); } }

/* ---------- Мобильная адаптация ---------- */

.drawer-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(5, 7, 12, .55);
  backdrop-filter: blur(2px);
  z-index: 40;
}

@media (max-width: 1180px) {
  .side-panel { position: fixed; top: var(--topbar-h); right: 0; bottom: 0; z-index: 45; box-shadow: var(--shadow); transform: translateX(100%); transition: transform .22s ease; }
  .side-panel.collapsed { display: flex; }
  .side-panel.open { transform: none; }
  .drawer-backdrop.show { display: block; }
}

@media (max-width: 1024px) {
  .schema-panel { position: fixed; top: var(--topbar-h); left: 0; bottom: 0; z-index: 45; width: min(300px, 85vw); box-shadow: var(--shadow); transform: translateX(-100%); transition: transform .22s ease; }
  .schema-panel.collapsed { display: block; }
  .schema-panel.open { transform: none; }
  .lesson-layout { grid-template-columns: 1fr; }
  .theory { position: static; max-height: none; }
  .lesson-tabs { display: flex; margin: 0 0 14px; }
  .lesson-layout[data-tab="theory"] .practice { display: none; }
  .lesson-layout[data-tab="practice"] .theory { display: none; }
}

@media (max-width: 860px) {
  :root { --topbar-h: 56px; }
  body { font-size: 15px; }
  .top-nav, .topbar .hide-mobile { display: none !important; }
  .brand { margin-right: 0; font-size: 17px; }
  .user-chip .name { display: none; }
  .user-chip { padding: 3px; }

  .main { padding-bottom: calc(var(--bottomnav-h) + env(safe-area-inset-bottom)); }
  .bottom-nav {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: blur(12px);
    border-top: 1px solid var(--border);
    z-index: 35;
  }
  .bottom-nav a, .bottom-nav button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--muted);
    text-decoration: none;
    font: 500 11px var(--font);
    border: 0;
    background: none;
    cursor: pointer;
  }
  .bottom-nav .bi { font-size: 21px; }
  .bottom-nav a.active { color: var(--accent-text); }
  .bottom-nav .gold { color: var(--warn); }

  .db-sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    bottom: 0;
    z-index: 45;
    width: min(300px, 85vw);
    box-shadow: var(--shadow);
    transform: translateX(-100%);
    transition: transform .22s ease;
  }
  .db-sidebar.open { transform: none; }
  .db-item .icon-btn { opacity: 1; }

  .ws-toolbar { padding: 8px 10px; gap: 4px; min-height: 52px; }
  .ws-title { font-size: 15px; }
  .editor-col { padding: 12px 10px; }
  .editor-card .CodeMirror { height: 200px; }
  .CodeMirror { font-size: 14px; }
  .run-bar .btn-x { flex: 1; }
  .run-bar .status { width: 100%; margin-left: 0; text-align: center; order: 5; }
  .run-bar .kbd-hint { display: none; }
  .table-wrap { max-height: 60vh; }
  table.data { font-size: 13px; }
  table.data td, table.data th { padding: 7px 10px; }

  .board-view { padding: 10px; }
  .board-card .CodeMirror { font-size: 15px; }

  .lessons-wrap { padding: 16px 12px 32px; }
  .lessons-hero { padding: 20px; border-radius: 18px; }
  .lessons-hero h1 { font-size: 23px; }
  .hero-progress { min-width: 0; width: 100%; }
  .lesson-grid { grid-template-columns: 1fr; }
  .lesson-page { padding: 12px 12px 32px; }
  .lesson-title-row h1 { font-size: 21px; }
  .theory { padding: 16px; font-size: 15px; }
  .task-card { padding: 14px; }
  .task-card .run-bar .btn-x { flex: 1 1 40%; }
  .lesson-nav a { max-width: none; }

  .toasts { left: 12px; right: 12px; bottom: calc(var(--bottomnav-h) + 12px + env(safe-area-inset-bottom)); }
  .toast-x { min-width: 0; max-width: none; }

  .modal-dialog { margin: auto 10px; }
  .input-x { font-size: 16px; } /* iOS не зумит поле */
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
