:root {
  --bg: #0b0d10;
  --panel: #14171c;
  --panel-2: #191d24;
  --border: #262b33;
  --text: #e7e9ec;
  --text-dim: #8b929e;
  --accent: #18b36a;
  --accent-dim: #126e46;
  --danger: #e0455f;
  --gold: #ffcf6b;
  /* Нейтральный фон вокруг полотна — специально не входит в палитру 32 цветов
     и не близок ни к #000000, ни к #FFFFFF, чтобы пиксели по краю холста
     не сливались с фоном. */
  --canvas-bg: #34313f;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  overflow: hidden;
  position: fixed;
  inset: 0;
}

.hidden { display: none !important; }

.screen {
  height: 100%;
  width: 100%;
}

button {
  font-family: inherit;
  cursor: pointer;
  user-select: none;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- Экран авторизации ---------- */

#auth-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    radial-gradient(circle at 20% 20%, rgba(24, 179, 106, 0.08), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(190, 0, 117, 0.08), transparent 40%),
    var(--bg);
}

.auth-card {
  width: min(340px, 100%);
  padding: 32px 28px 28px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.auth-logo {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
}

.logo-px {
  width: 14px;
  height: 14px;
  border-radius: 3px;
}

.auth-card h1 {
  margin: 0 0 6px;
  font-size: 22px;
  letter-spacing: -0.02em;
}

.auth-sub {
  margin: 0 0 22px;
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1.5;
}

.tabs {
  display: flex;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
  margin-bottom: 20px;
}

.tab {
  flex: 1;
  padding: 8px 0;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  transition: background 0.15s, color 0.15s;
}

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

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
}

.field input {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 12px;
  color: var(--text);
  font-size: 16px; /* >=16px чтобы iOS Safari не зумил при фокусе */
}

.field input:focus-visible {
  border-color: var(--accent);
}

.btn-primary {
  margin-top: 6px;
  background: var(--accent);
  color: #06140d;
  border: none;
  border-radius: 8px;
  padding: 12px 0;
  font-size: 14px;
  font-weight: 700;
  transition: background 0.15s, transform 0.05s;
}

.btn-primary:hover { background: #1fce7c; }
.btn-primary:active { transform: scale(0.98); }

.form-error {
  min-height: 16px;
  margin: 2px 0 0;
  color: var(--danger);
  font-size: 12px;
}

/* ---------- Экран холста ---------- */

#app-screen {
  display: flex;
  flex-direction: column;
}

.topbar {
  height: 52px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  gap: 10px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 18px;
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.brand-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent);
  flex: 0 0 auto;
}

.coords {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-dim);
  min-width: 120px;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.user-pill {
  font-family: var(--mono);
  font-size: 12px;
  padding: 6px 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text-dim);
  max-width: 130px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-pill.unlimited {
  color: var(--gold);
  border-color: #5a4a1f;
}

.user-pill.unlimited #username-label::before {
  content: "★ ";
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: 12px;
  transition: border-color 0.15s, color 0.15s;
  flex: 0 0 auto;
}

.btn-ghost:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.canvas-wrap {
  position: relative;
  flex: 1 1 auto;
  overflow: hidden;
  background: var(--canvas-bg);
  background-image:
    linear-gradient(rgba(255,255,255,0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.035) 1px, transparent 1px);
  background-size: 24px 24px;
  cursor: grab;
  touch-action: none; /* весь жест обрабатываем сами: пан/пинч-зум/тап */
}

.canvas-wrap.dragging {
  cursor: grabbing;
}

#view-canvas {
  position: absolute;
  top: 0;
  left: 0;
}

.side-controls {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
}

.side-controls button {
  width: 34px;
  height: 34px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 15px;
  line-height: 1;
}

.side-controls button:hover {
  border-color: var(--accent);
}

.side-controls .download-btn {
  margin-top: 4px;
  border-top: 1px solid var(--border);
  padding-top: 4px;
  color: var(--accent);
}

.toast {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translate(-50%, -12px);
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 8px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
  max-width: 88vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.palette-bar {
  flex: 0 0 auto;
  background: var(--panel);
  border-top: 1px solid var(--border);
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom, 0px));
}

.palette-row {
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: 100%;
}

.cooldown-box {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 0 0 auto;
}

.cooldown-ring {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text);
  transition: border-color 0.2s;
  flex: 0 0 auto;
}

.cooldown-ring.waiting {
  border-color: var(--text-dim);
  color: var(--text-dim);
}

.cooldown-label {
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}

.current-color {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 2px solid var(--text);
  box-shadow: 0 0 0 3px rgba(255,255,255,0.12);
}

.palette {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 2px 8px;
  flex: 1 1 auto;
  min-width: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.palette::-webkit-scrollbar {
  height: 6px;
}
.palette::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

.swatch {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 8px;
  border: 2px solid var(--border);
  transition: transform 0.1s, border-color 0.15s;
}

.swatch:hover {
  transform: translateY(-2px);
}

.swatch.selected {
  border-color: var(--text);
  box-shadow: 0 0 0 3px rgba(255,255,255,0.12);
}

/* ---------- Планшеты / телефоны ---------- */

@media (max-width: 640px) {
  .coords { display: none; }
}

@media (max-width: 480px) {
  .topbar { padding: 0 10px; height: 48px; }
  .brand { font-size: 13px; gap: 6px; }
  .user-pill { font-size: 11px; padding: 5px 8px; max-width: 96px; }
  .btn-ghost { padding: 6px 10px; font-size: 11px; }

  .side-controls { right: 10px; bottom: 10px; }
  .side-controls button { width: 40px; height: 40px; font-size: 17px; }

  .palette-bar { padding: 10px 10px calc(10px + env(safe-area-inset-bottom, 0px)); }
  .palette-row { gap: 10px; }
  .cooldown-box { gap: 7px; }
  .cooldown-ring { width: 30px; height: 30px; font-size: 10px; }
  .cooldown-label { display: none; } /* на узких экранах достаточно кольца с секундами */
  .current-color { width: 34px; height: 34px; border-radius: 8px; }
  .swatch { width: 30px; height: 30px; border-radius: 7px; }
}

@media (pointer: coarse) {
  .swatch { width: 36px; height: 36px; }
  .side-controls button { width: 40px; height: 40px; }
}

/* ---------- Подпись автора на экране входа ---------- */

.auth-footer {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 16px;
  text-align: center;
  font-size: 12px;
  color: var(--text-dim);
  padding: 0 16px;
}

.auth-footer a {
  color: var(--text-dim);
  text-decoration: underline;
  text-decoration-color: var(--border);
  transition: color 0.15s;
}

.auth-footer a:hover {
  color: var(--accent);
}

/* ---------- Согласие с условиями при регистрации ---------- */

.terms-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: -2px;
}

.checkbox-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 12px;
  line-height: 1.45;
  color: var(--text-dim);
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  flex: 0 0 auto;
  width: 15px;
  height: 15px;
  margin-top: 2px;
  accent-color: var(--accent);
  cursor: pointer;
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  margin: 0;
  font: inherit;
  font-size: 12px;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

.link-btn:hover {
  color: #1fce7c;
}

.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-primary:disabled:hover {
  background: var(--accent);
}

/* ---------- Модальное окно (Условия / Конфиденциальность) ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 7, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}

.modal-box {
  width: min(560px, 100%);
  max-height: min(78vh, 640px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
}

.modal-close {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 20px;
  line-height: 1;
  padding: 4px 9px;
  border-radius: 6px;
}

.modal-close:hover {
  color: var(--text);
  background: var(--panel-2);
}

.modal-body {
  padding: 16px 18px;
  overflow-y: auto;
  font-size: 13px;
  line-height: 1.6;
}

.modal-body h3 {
  font-size: 13px;
  margin: 18px 0 6px;
  color: var(--text);
}

.modal-body h3:first-child {
  margin-top: 0;
}

.modal-body p,
.modal-body li {
  color: var(--text-dim);
}

.modal-body ul {
  margin: 4px 0;
  padding-left: 18px;
}

.modal-footer {
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  flex: 0 0 auto;
}

.modal-footer .btn-primary {
  padding: 9px 20px;
  margin-top: 0;
}

@media (max-width: 480px) {
  .modal-box { max-height: 86vh; }
  .modal-header h2 { font-size: 14px; }
}
