*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }

/* ===== ТЕМЫ ===== */
:root, [data-theme="midnight"] {
  --bg: #0a0a0f;
  --surface: #16161a;
  --surface2: #24242b;
  --text: #e6e6e6;
  --text-dim: #808080;
  --accent: #3b82f6;
  --accent-dark: #1e40af;
  --danger: #f85149;
  --warn: #ea9d34;
  --border: #333;
}
[data-theme="dracula"] {
  --bg: #1e1f29;
  --surface: #282a36;
  --surface2: #3a3d4d;
  --text: #f8f8f2;
  --text-dim: #6272a4;
  --accent: #bd93f9;
  --accent-dark: #7c5ec4;
  --danger: #ff5555;
  --warn: #f1fa8c;
  --border: #44475a;
}
[data-theme="nord"] {
  --bg: #242933;
  --surface: #2e3440;
  --surface2: #3b4252;
  --text: #eceff4;
  --text-dim: #7b88a1;
  --accent: #88c0d0;
  --accent-dark: #5e81ac;
  --danger: #bf616a;
  --warn: #ebcb8b;
  --border: #4c566a;
}
[data-theme="monokai"] {
  --bg: #1e1f1c;
  --surface: #272822;
  --surface2: #3a3b32;
  --text: #f8f8f2;
  --text-dim: #75715e;
  --accent: #f92672;
  --accent-dark: #a3194e;
  --danger: #ff5555;
  --warn: #fd971f;
  --border: #49483e;
}
[data-theme="sepia"] {
  --bg: #e8e0d5;
  --surface: #f4efe6;
  --surface2: #dfd5c5;
  --text: #3b3229;
  --text-dim: #8a7d6d;
  --accent: #8b5a2b;
  --accent-dark: #5c3a1a;
  --danger: #b33a3a;
  --warn: #c4912a;
  --border: #d1c5b4;
}
[data-theme="slate"] {
  --bg: #2d3748;
  --surface: #4a5568;
  --surface2: #718096;
  --text: #edf2f7;
  --text-dim: #a0aec0;
  --accent: #4299e1;
  --accent-dark: #2b6cb0;
  --danger: #f56565;
  --warn: #ecc94b;
  --border: #5a6b8c;
}
[data-theme="light"] {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface2: #e0e0e0;
  --text: #1d1d1f;
  --text-dim: #666666;
  --accent: #0b6ef3;
  --accent-dark: #005bb5;
  --danger: #d32f2f;
  --warn: #ea9d34;
  --border: #d1d1d6;
}

/* ===== БАЗА ===== */
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  font-size: 16px;
  transition: background 0.2s, color 0.2s;
}

header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap;
  padding: calc(0.75rem + env(safe-area-inset-top)) 1rem;
  background: var(--surface); color: var(--text);
  border-bottom: 1px solid var(--border);
  /* Закрепляем шапку */
  position: sticky;
  top: 0;
  z-index: 1000;
}

/* Запрещаем сайту скроллиться влево-вправо */
html, body {
  overflow-x: hidden;
}
header .brand { color: var(--text); text-decoration: none; font-weight: 600; }
header nav { display: flex; align-items: center; gap: .75rem; }
header nav .who { color: var(--text-dim); font-size: .9rem; }
header nav form.inline { display: inline; margin: 0; }
header nav button {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: .3rem .6rem; cursor: pointer; font: inherit;
}
header nav button:hover { background: var(--surface2); }

select.theme-select {
  background: var(--surface2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px;
  padding: .3rem .5rem; font: inherit; cursor: pointer; outline: none;
}

main { max-width: 900px; margin: 0 auto; padding: 1rem; }

h1 { font-size: 1.5rem; margin: .25rem 0 .5rem; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.btn, button {
  display: inline-block; padding: .5rem .9rem;
  border: none; border-radius: 8px;
  background: var(--accent); color: #fff;
  text-decoration: none; cursor: pointer; font: inherit;
  transition: opacity 0.2s;
}
.btn:hover, button:hover { opacity: 0.85; text-decoration: none; }
.btn.small { padding: .3rem .6rem; font-size: .875rem; }
.btn.secondary { background: var(--surface2); color: var(--text); }
.btn.danger, button.danger { background: var(--danger); }

.page-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; flex-wrap: wrap; margin-bottom: 0.5rem;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
}
.card {
  background: var(--surface); border-radius: 12px; padding: 1rem;
  border: 1px solid var(--border);
  display: flex; flex-direction: column; gap: .75rem;
}
.card-link { color: inherit; text-decoration: none; }
.card-link h2 { margin: 0 0 .25rem; font-size: 1.1rem; color: var(--accent); }
.card-link p { margin: 0; color: var(--text-dim); overflow-wrap: anywhere; }
.card-actions { display: flex; gap: .5rem; margin-top: auto; flex-wrap: wrap; }
.card-actions form { margin: 0; }

form.stack {
  display: flex; flex-direction: column; gap: .75rem;
  max-width: 500px;
}
form.stack label { display: flex; flex-direction: column; gap: .25rem; font-size: .95rem; }
input, textarea {
  font: inherit; padding: .5rem .6rem;
  border: 1px solid var(--border); border-radius: 6px;
  width: 100%; background: var(--surface); color: var(--text);
}
input:focus, textarea:focus { outline: none; border-color: var(--accent); }
.row { display: flex; gap: .5rem; flex-wrap: wrap; }

.auth {
  max-width: 380px; margin: 3rem auto;
  background: var(--surface); padding: 1.5rem; border-radius: 12px;
  border: 1px solid var(--border);
}
.auth h1 { margin-top: 0; }
.auth form { display: flex; flex-direction: column; gap: .75rem; margin-bottom: 1rem; }

.flash {
  background: var(--surface2); border: 1px solid var(--border);
  padding: .5rem .75rem; border-radius: 8px; margin-bottom: 1rem;
}
.flash p { margin: 0; }

.tabs {
  display: flex; gap: .25rem;
  border-bottom: 2px solid var(--border); margin-bottom: 1rem;
  overflow-x: auto;
}
.tab {
  background: transparent; color: var(--text-dim);
  border: none; padding: .6rem 1rem;
  border-bottom: 2px solid transparent; margin-bottom: -2px;
  cursor: pointer; font: inherit; white-space: nowrap;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.stub {
  color: var(--text-dim); font-style: italic;
  padding: 1rem; background: var(--surface);
  border-radius: 8px; margin: 0; border: 1px solid var(--border);
}

.back { color: var(--text-dim); text-decoration: none; font-size: .9rem; }
.back:hover { color: var(--text); text-decoration: none; }

/* ===== TRACKER ===== */
.tracker { display: flex; flex-direction: column; gap: .75rem; }

.tr-toolbar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.tr-toolbar button {
  min-width: 44px; min-height: 40px;
  background: var(--surface2); color: var(--text); border: none;
  border-radius: 8px; font: inherit; cursor: pointer;
}
.tr-round { margin-left: auto; color: var(--text-dim); font-size: .9rem; }
.tr-round b { color: var(--accent); }

.tr-log {
  background: var(--surface); border-left: 3px solid var(--accent);
  padding: .5rem .75rem; border-radius: 6px;
  font-size: .875rem; color: var(--text); border-top: 1px solid var(--border); border-right: 1px solid var(--border); border-bottom: 1px solid var(--border);
}

.tr-combatants { display: flex; flex-direction: column; gap: .5rem; }
.tr-empty { text-align: center; color: var(--text-dim); padding: 2rem; font-style: italic; }

.tr-card {
  background: var(--surface); border-radius: 10px; padding: .75rem;
  display: flex; flex-direction: column; gap: .5rem;
  border: 1px solid var(--border); border-left: 4px solid transparent;
}
.tr-card.current { border-left-color: var(--accent); background: var(--surface2); }

.tr-card-head { display: flex; gap: .35rem; align-items: center; }
.tr-init { width: 56px; flex-shrink: 0; }
.tr-name { flex: 1; min-width: 0; }
.tr-del {
  width: 36px; height: 36px; flex-shrink: 0;
  background: var(--danger); color: #fff; border: none;
  border-radius: 6px; font: inherit; cursor: pointer;
}

.tr-hp-grid {
  display: grid; grid-template-columns: auto 1fr auto auto auto auto;
  gap: 0.35rem; align-items: center;
}
.tr-hp-grid label { font-size: 0.75rem; color: var(--text-dim); font-weight: 600; }
.tr-hp-grid input { text-align: center; font-weight: 600; min-width: 0; padding: 0.4rem; }
.tr-hp-grid button {
  padding: 0; width: 34px; height: 34px; display: flex;
  justify-content: center; align-items: center; border-radius: 6px; border: none;
}
.tr-hp-grid button[data-delta="-1"] { background: var(--danger); color: #fff; }
.tr-hp-grid button[data-delta="1"] { background: var(--accent); color: #fff; }

.tr-triple { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }
.tr-counter { display: grid; grid-template-columns: 1fr auto auto; gap: 0.25rem; align-items: center; }
.tr-counter label { grid-column: 1 / -1; font-size: 0.7rem; color: var(--text-dim); font-weight: 600; margin-bottom: 2px; }
.tr-counter input { text-align: center; min-width: 0; padding: 0.4rem; }
.tr-counter button {
  padding: 0; width: 32px; height: 34px; display: flex;
  justify-content: center; align-items: center; border-radius: 6px; border: none;
}
.tr-counter button[data-delta="-1"] { background: var(--danger); color: #fff; }
.tr-counter button[data-delta="1"] { background: var(--accent); color: #fff; }

.tr-effects { display: flex; flex-wrap: wrap; gap: .35rem; }
.tr-chip {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .3rem .5rem; border-radius: 14px;
  color: #fff; font-size: .8rem; cursor: pointer;
}
.tr-chip-accent { background: var(--accent); }
.tr-chip-danger { background: var(--danger); }
.tr-chip-warn { background: var(--warn); }
.tr-chip-x {
  background: rgba(0,0,0,.25); border-radius: 8px;
  padding: 0 .35rem; font-weight: bold;
}
.tr-add-effect {
  align-self: flex-start; padding: .35rem .7rem;
  background: var(--accent); color: #fff; border: none;
  border-radius: 6px; font-size: .85rem; cursor: pointer;
}

.tr-bottom {
  position: sticky; bottom: 0;
  display: flex; gap: .35rem;
  background: var(--bg); padding: 0.5rem calc(0.5rem + env(safe-area-inset-bottom));
}
.tr-bottom button {
  padding: .75rem 1rem; border: none; border-radius: 8px;
  background: var(--surface2); color: var(--text); font: inherit; cursor: pointer;
}
.tr-bottom .tr-add { background: var(--accent); color: #fff; min-width: 56px; }
.tr-bottom .tr-next { flex: 1; background: var(--accent); color: #fff; font-weight: 600; }

.tr-tabs {
  display: flex; gap: 0.5rem; margin-bottom: 1rem;
  border-bottom: 2px solid var(--border);
}
.tr-tabs button {
  padding: 0.5rem 1rem; background: transparent; border: none;
  border-bottom: 2px solid transparent; font: inherit; font-weight: 600;
  color: var(--text-dim); cursor: pointer;
}
.tr-tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

.tr-log-list {
  display: flex; flex-direction: column; gap: 0.35rem;
  background: var(--surface); padding: 1rem; border-radius: 8px;
  border: 1px solid var(--border);
}
.tr-log-row {
  font-size: 0.875rem; padding: 0.25rem 0;
  border-bottom: 1px solid var(--border);
}
.tr-log-row:last-child { border-bottom: none; }
.tr-log-row span {
  display: inline-block; min-width: 60px;
  color: var(--text-dim); font-family: monospace; font-size: 0.75rem;
}

/* ===== МОДАЛКИ ===== */
.tr-modal {
  position: fixed; inset: 0; z-index: 1000;
  background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
  padding-bottom: env(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}
.tr-modal-panel {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 14px;
  padding: 1rem; width: 100%; max-width: 420px;
  max-height: calc(100dvh - 2rem); overflow-y: auto;
  display: flex; flex-direction: column; gap: .75rem;
}
.tr-modal-panel.tr-small { max-width: 320px; }
.tr-modal-panel h3 { margin: 0 0 .25rem; font-size: 1.1rem; }

.tr-modal-panel button {
  padding: .5rem .75rem; border: none; border-radius: 6px;
  background: var(--surface2); color: var(--text); font: inherit; cursor: pointer;
}
.tr-modal-panel .tr-modal-close { background: var(--surface2); }
.tr-btn-accent { background: var(--accent) !important; color: #fff !important; }
.tr-btn-danger { background: var(--danger) !important; color: #fff !important; }
.tr-btn-cancel { background: var(--surface2) !important; color: var(--text) !important; }

.tr-dice-formula {
  width: 100%; padding: .6rem; border: 1px solid var(--border);
  border-radius: 6px; font-family: ui-monospace, Menlo, monospace;
  font-size: 1.1rem; text-align: center;
  background: var(--surface2); color: var(--text);
}
.tr-dice-grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: .35rem; }
.tr-dice-grid button { padding: .5rem; font-weight: 600; }
.tr-dice-adv { display: flex; gap: .35rem; }
.tr-dice-adv button { flex: 1; }
.tr-dice-adv button.active { background: var(--accent); color: #fff; }
.tr-dice-actions { display: grid; grid-template-columns: 1fr 1fr; gap: .35rem; }
.tr-dice-actions button[data-roll="hit"] { background: var(--accent); color: #fff; }
.tr-dice-actions button[data-roll="dmg"] { background: var(--danger); color: #fff; }
.tr-dice-result {
  padding: .75rem; background: var(--surface2); border-radius: 6px;
  text-align: center; font-weight: 600; min-height: 2.5rem;
}
.tr-dice-apply { display: grid; grid-template-columns: 1fr 1fr; gap: .35rem; }
.tr-dice-apply button:first-child { background: var(--danger); color: #fff; }
.tr-dice-apply button:last-child { background: var(--accent); color: #fff; }

.tr-calc-display {
  font-size: 2.5rem; font-weight: 700; text-align: center;
  padding: .5rem; background: var(--surface2); border-radius: 8px;
  font-family: ui-monospace, Menlo, monospace;
}
.tr-calc-keys { display: grid; grid-template-columns: repeat(3, 1fr); gap: .35rem; }
.tr-calc-keys button { padding: 1rem; font-size: 1.2rem; font-weight: 600; }

.tr-modal-actions { display: flex; flex-wrap: wrap; gap: .35rem; }
.tr-modal-actions button { flex: 1; min-width: 90px; }

.tr-effects-actions { display: flex; gap: .35rem; }
.tr-effects-actions input { flex: 1; padding: .5rem; border: 1px solid var(--border); border-radius: 6px; }
.tr-effects-list { display: flex; flex-direction: column; gap: .35rem; max-height: 40vh; overflow-y: auto; }
.tr-effects-list button { text-align: left; padding: .6rem .75rem; }

.tr-effect-rounds { display: flex; align-items: center; gap: .5rem; }
.tr-effect-rounds input { width: 80px; text-align: center; font-size: 1.2rem; font-weight: 600; }
.tr-effect-rounds button { min-width: 44px; min-height: 44px; }

/* ===== ЗАМЕТКИ ===== */
.head-actions { display: flex; gap: 0.5rem; }
.notes-container { display: flex; flex-direction: column; gap: 1rem; }

.notes-tabs {
  display: flex; gap: 0.35rem; overflow-x: auto; padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--border); -webkit-overflow-scrolling: touch;
}
.notes-tabs::-webkit-scrollbar { display: none; }

.note-tab {
  padding: 0.4rem 0.8rem; background: var(--surface2); border: 1px solid var(--border); border-radius: 6px;
  white-space: nowrap; cursor: pointer; font: inherit; font-size: 0.9rem;
  color: var(--text-dim); user-select: none;
}
.note-tab.active { background: var(--accent); color: #fff; font-weight: 600; border-color: var(--accent); }

.note-entry { display: none; }
.note-entry.active { display: flex; flex-direction: column; gap: 0.75rem; }

.note-header { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
.note-title {
  flex: 1; min-width: 200px; font-size: 1.1rem; font-weight: 600;
  padding: 0.5rem; border: none; border-bottom: 2px solid var(--border); background: transparent;
  color: var(--text);
}
.note-title:focus { outline: none; border-bottom-color: var(--accent); }

.note-date {
  width: 140px; font-size: 0.9rem; color: var(--text); padding: 0.5rem;
  border: 1px solid var(--border); border-radius: 6px; background: var(--surface2);
}

.note-body {
  width: 100%; min-height: 150px; padding: 1rem; font: inherit;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  color: var(--text); resize: none; overflow: hidden; line-height: 1.5;
}
.note-body:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px rgba(11, 110, 243, 0.1); }

.img-modal-content { background: transparent; display: flex; flex-direction: column; align-items: center; width: 100%; max-width: 800px; }
.img-modal-content .btn { background: var(--surface); color: var(--text); max-width: 300px; font-weight: bold; }
.img-btn { font-size: 1.1rem; padding: 0.3rem 0.6rem; min-width: 40px; }

/* Утилиты */
.text-accent { color: var(--accent) !important; }
.text-danger { color: var(--danger) !important; }

@media (max-width: 480px) {
  main { padding: .75rem; }
  h1 { font-size: 1.25rem; }
  .auth { margin: 1.5rem auto; padding: 1rem; }
  .note-title { min-width: 100px; font-size: 1rem; }
  .note-header { gap: 0.3rem; flex-direction: column; align-items: stretch; }
  .note-date { width: 100%; }
  .note-del { align-self: flex-end; }
}

.tab-pencil {
  font-size: 0.8em;
  opacity: 0.6;
  margin-left: 4px;
}

/* --- Закрепление вкладок --- */

.tabs, .notes-tabs, .tr-tabs {
  position: sticky;
  /* Высота шапки примерно 54px, прилипаем под ней */
  top: 52px;
  background: var(--bg);
  z-index: 99;
  padding-top: 0.5rem;
  margin-top: 0;
  margin-bottom: 0.5rem;
}

/* Убираем лишние отступы у заголовка страницы */
.page-head {
  margin-bottom: 0.5rem;
  margin-top: 0.5rem;
}

/* На мобилках прячем имя пользователя, чтобы шапка не переносилась на 2 строки */
@media (max-width: 600px) {
  header { padding: 0.5rem; }
  header nav .who { display: none; }
  .tabs, .notes-tabs, .tr-tabs { top: 48px; }
}

/* --- Заглушка (можно удалить из HTML, но на всякий случай) --- */
.stub { display: none; }

/* ===== NPC ===== */
.circle-container {
  width: 100%;
  max-width: 600px;
  margin: 0 auto;
  position: relative;
}

#npc-svg {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  display: block;
}

.legend-container {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1rem;
  max-width: 600px;
  margin-left: auto;
  margin-right: auto;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--text-dim);
}

.legend-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: inline-block;
}

/* Контролы над кругом */
.circle-controls {
  display: flex; align-items: center; gap: 1rem; margin-bottom: 1rem;
  flex-wrap: wrap;
}
.circle-controls .hint {
  font-size: 0.85rem; color: var(--text-dim); font-style: italic;
}

/* Быстрое добавление */
.quick-add-form {
  display: flex; gap: 0.5rem; margin-bottom: 1rem;
}
.quick-add-form input {
  flex: 1; max-width: 300px;
}

/* Таблица NPC */
.npc-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.npc-table th, .npc-table td {
  padding: 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
.npc-table th {
  background: var(--surface2);
  font-weight: 600;
}
.npc-table tr:last-child td {
  border-bottom: none;
}

/* Эффекты наведения на узлы графа */
.svg-node circle {
  transition: fill 0.2s;
}
.svg-node:hover circle {
  fill: var(--accent-dark);
}

/* Стили для инпутов в модалках */
.theme-input {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 0.5rem;
  border-radius: 6px;
  font: inherit;
}
.theme-input:focus {
  outline: none;
  border-color: var(--accent);
}
textarea.theme-input {
  resize: vertical;
}

/* ===== iOS / iPadOS FIXES ===== */

/* Убираем синюю вспышку при тапе на iOS */
* {
  -webkit-tap-highlight-color: transparent;
}

/* Запрет двойного тапа для зума на кнопках */
button, .btn, .tab {
  touch-action: manipulation;
}

/* Запрет зума при фокусе на поля ввода на мобильных */
@media screen and (max-width: 768px) {
  input, textarea, select {
    font-size: 16px !important;
  }
}
