:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --surface: var(--tg-theme-bg-color, #ffffff);
  --text: var(--tg-theme-text-color, #1c1c1e);
  --hint: var(--tg-theme-hint-color, #8e8e93);
  --accent: var(--tg-theme-button-color, #2ea6ff);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #2ea6ff);
  --danger: #e5484d;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

.banner {
  padding: 10px 16px;
  font-size: 13px;
  text-align: center;
  background: color-mix(in srgb, var(--accent) 16%, var(--surface));
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
}

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

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 25%, transparent);
}

.topbar-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

h1 {
  margin: 0;
  font-size: 20px;
}

/* --- квотомер ------------------------------------------------------------- */

.meter {
  height: 5px;
  border-radius: 3px;
  overflow: hidden;
  background: color-mix(in srgb, var(--hint) 22%, transparent);
}

.meter span {
  display: block;
  height: 100%;
  width: 0;
  border-radius: 3px;
  background: var(--accent);
  transition: width 0.25s ease;
}

.meter.warn span {
  background: #f5a524;
}

.quota-caption {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 0;
  font-size: 12px;
  color: var(--hint);
}

.quota-caption.warn {
  color: #f5a524;
}

.sync-status.error {
  color: var(--danger);
}

/* --- список документов ---------------------------------------------------- */

.docs {
  list-style: none;
  margin: 0;
  padding: 12px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.doc-row {
  display: flex;
  align-items: stretch;
  gap: 8px;
}

.doc-open {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 12px 14px;
  text-align: left;
  border: none;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}

.doc-open:active {
  filter: brightness(0.96);
}

.doc-title {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.doc-meta {
  font-size: 12px;
  color: var(--hint);
}

.doc-delete {
  border: none;
  border-radius: 12px;
  background: var(--surface);
  padding: 0 14px;
  font-size: 15px;
  cursor: pointer;
  color: var(--danger);
}

.empty {
  margin: 48px 24px;
  text-align: center;
  color: var(--hint);
}

/* --- кнопки --------------------------------------------------------------- */

button.primary-button,
button.secondary-button {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

button.primary-button {
  background: var(--accent);
  color: var(--accent-text);
}

button.primary-button:disabled {
  opacity: 0.6;
}

button.secondary-button {
  background: transparent;
  color: var(--link);
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
}

.back-button {
  border: none;
  background: transparent;
  color: var(--link);
  font-size: 22px;
  line-height: 1;
  padding: 4px 8px 4px 0;
  cursor: pointer;
}

/* --- редактор ------------------------------------------------------------- */

.title-input {
  flex: 1;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 17px;
  font-weight: 600;
  padding: 4px 0;
}

.title-input:focus {
  outline: none;
}

.tabs {
  display: flex;
  gap: 4px;
  background: color-mix(in srgb, var(--hint) 14%, transparent);
  border-radius: 9px;
  padding: 3px;
}

.tab {
  flex: 1;
  border: none;
  border-radius: 7px;
  padding: 7px 0;
  background: transparent;
  color: var(--hint);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.tab.active {
  background: var(--surface);
  color: var(--text);
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}

.toolbar::-webkit-scrollbar {
  display: none;
}

.toolbar button {
  flex: 0 0 auto;
  min-width: 34px;
  height: 30px;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.toolbar button:active {
  background: color-mix(in srgb, var(--hint) 18%, transparent);
}

.toolbar-sep {
  width: 1px;
  height: 18px;
  margin: 0 4px;
  background: color-mix(in srgb, var(--hint) 30%, transparent);
}

.editor-host {
  flex: 1;
  padding: 8px 6px 24px;
}

.editor-host .cm-editor {
  min-height: 60dvh;
}

.preview {
  flex: 1;
  align-self: stretch;
  margin: 12px 16px 32px;
  padding: 16px;
  border-radius: 12px;
  background: var(--surface);
  overflow-wrap: break-word;
}

.preview h1,
.preview h2,
.preview h3,
.preview h4 {
  margin: 0.8em 0 0.4em;
  line-height: 1.25;
}

.preview h1 {
  font-size: 1.6em;
}

.preview h2 {
  font-size: 1.35em;
}

.preview h3 {
  font-size: 1.15em;
}

.preview code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.9em;
  background: color-mix(in srgb, var(--hint) 16%, transparent);
  border-radius: 4px;
  padding: 1px 4px;
}

.preview pre {
  background: color-mix(in srgb, var(--hint) 12%, transparent);
  border-radius: 8px;
  padding: 10px 12px;
  overflow-x: auto;
}

.preview pre code {
  background: transparent;
  padding: 0;
}

.preview blockquote {
  margin: 0.6em 0;
  padding: 0.1em 0 0.1em 12px;
  border-left: 3px solid color-mix(in srgb, var(--hint) 45%, transparent);
  color: var(--hint);
}

.preview table {
  border-collapse: collapse;
  margin: 0.6em 0;
}

.preview th,
.preview td {
  border: 1px solid color-mix(in srgb, var(--hint) 40%, transparent);
  padding: 5px 10px;
}

.preview a {
  color: var(--link);
}

.editor-footer {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid color-mix(in srgb, var(--hint) 25%, transparent);
}

.editor-footer .primary-button {
  flex: 1;
}

/* --- шторка «Оформление» ---------------------------------------------------- */

.sheet-overlay {
  position: fixed;
  inset: 0;
  z-index: 15;
  display: flex;
  align-items: flex-end;
  background: rgb(0 0 0 / 35%);
}

.sheet {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-height: 88dvh;
  border-radius: 16px 16px 0 0;
  background: var(--surface);
  box-shadow: 0 -8px 32px rgb(0 0 0 / 25%);
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 10px;
  border-bottom: 1px solid color-mix(in srgb, var(--hint) 22%, transparent);
}

.sheet-header h2 {
  margin: 0;
  font-size: 17px;
}

.sheet-body {
  overflow-y: auto;
  padding: 4px 16px calc(16px + env(safe-area-inset-bottom));
}

/* --- шторка «Отправить» ----------------------------------------------------- */

.send-option {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  margin-top: 6px;
  padding: 12px 10px;
  border: none;
  border-radius: 12px;
  background: transparent;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.send-option:active {
  background: color-mix(in srgb, var(--hint) 14%, transparent);
}

.send-icon {
  font-size: 22px;
}

.send-option-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.send-option-title {
  font-size: 15px;
  font-weight: 600;
}

.send-option-hint {
  font-size: 12px;
  color: var(--hint);
}

.style-section {
  margin-top: 12px;
}

.style-section h3 {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--hint);
}

.style-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 40px;
}

.style-label {
  font-size: 14px;
}

.style-select {
  max-width: 60%;
  padding: 7px 10px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.style-slider {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: 60%;
}

.style-slider input {
  flex: 1;
  min-width: 0;
}

.style-slider output {
  min-width: 52px;
  text-align: right;
  font-size: 13px;
  color: var(--hint);
  font-variant-numeric: tabular-nums;
}

.style-checkbox {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.segmented {
  display: flex;
  gap: 2px;
  padding: 3px;
  border-radius: 9px;
  background: color-mix(in srgb, var(--hint) 14%, transparent);
}

.segmented button {
  border: none;
  border-radius: 7px;
  padding: 6px 10px;
  background: transparent;
  color: var(--hint);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.segmented button.active {
  background: var(--surface);
  color: var(--text);
}

.style-sample {
  margin: 10px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: color-mix(in srgb, var(--hint) 10%, transparent);
  overflow-wrap: break-word;
}

/* Точки выбора цвета из кураторской палитры (shared/style.ts → PALETTE). */
.color-dots {
  display: inline-flex;
  gap: 6px;
}

.color-dot {
  width: 28px;
  height: 28px;
  border: 2px solid transparent;
  border-radius: 50%;
  padding: 0;
  background: transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.color-dot.active {
  border-color: var(--accent);
}

.color-dot .dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  display: block;
}

.dot-text { background: var(--text); }
.dot-gray { background: #555555; }
.dot-blue { background: #2b6cb0; }
.dot-green { background: #2e7d32; }
.dot-burgundy { background: #8c2f39; }

/* Мини-превью в образце: приблизительно повторяет PDF-рендер. */
.sample-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.85em;
}

.sample-table th,
.sample-table td {
  padding: 2px 6px;
  text-align: left;
}

.sample-table th {
  font-weight: 700;
}

.table-grid th,
.table-grid td {
  border: 0.5px solid #999999;
}

.table-grid th,
.table-lines th,
.table-zebra th {
  background: #efefef;
}

.table-lines {
  border-top: 0.6px solid #999999;
  border-bottom: 0.6px solid #999999;
}

.table-lines th,
.table-lines td,
.table-zebra th,
.table-zebra td {
  border-bottom: 0.6px solid #999999;
}

.table-zebra tbody tr:nth-child(even) {
  background: #f7f7f7;
}

/* --- пресеты ---------------------------------------------------------------- */

.preset-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.preset-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.preset-pick {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 1px solid color-mix(in srgb, var(--hint) 25%, transparent);
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.preset-pick.active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
}

.preset-dot {
  flex: 0 0 auto;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--hint) 55%, transparent);
}

.preset-pick.active .preset-dot {
  border-color: var(--accent);
  background: radial-gradient(circle, var(--accent) 0 45%, transparent 50%);
}

.preset-name {
  font-weight: 600;
}

.preset-summary {
  font-size: 12px;
  color: var(--hint);
}

.preset-rename,
.preset-delete {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: transparent;
  font-size: 14px;
  cursor: pointer;
}

.preset-delete {
  color: var(--danger);
}

.preset-rename-input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.preset-new {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.preset-new-input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px;
  border: 1px solid color-mix(in srgb, var(--hint) 35%, transparent);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.style-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 16px;
}

/* --- тост ----------------------------------------------------------------- */

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(24px + env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--text) 92%, transparent);
  color: var(--surface);
  font-size: 14px;
  box-shadow: 0 8px 24px rgb(0 0 0 / 25%);
}

.toast-action {
  flex: 0 0 auto;
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  font: inherit;
  font-weight: 600;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
}
