/* ------------------------------------------------------------------
   app.css — the shell around the document: a composing desk.
   Warm paper, ink, hairline rules; the only saturated colour is the
   press magenta, used for focus and live state.
   ------------------------------------------------------------------ */

@font-face {
  font-family: "Golos Text";
  src: url("/static/fonts/GolosText-Variable.ttf") format("truetype-variations");
  font-weight: 400 900;
  font-style: normal;
  font-display: swap;
}

:root {
  --paper: #efede6;
  --paper-deep: #e7e4db;
  --surface: #fbf9f4;
  --field: #fdfcf8;
  --ink: #191813;
  --ink-2: #46433b;
  --muted: #625e53;
  --line: #d6d1c4;
  --line-strong: #b5afa0;
  --accent: #b0145b;
  --danger: #9c1f1f;
  --ok: #2f6b4f;
  --radius: 2px;
  --ui: "Golos Text", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "PT Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  --topbar-h: 52px;
  --shadow-pop: 0 1px 2px rgba(26, 25, 21, 0.08), 0 14px 34px -14px rgba(26, 25, 21, 0.34);
}

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

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--ui);
  font-size: 14px;
  line-height: 1.45;
  font-feature-settings: "ss01" 1;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior: none;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
  background: none;
  border: 0;
  padding: 0;
}

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

::selection {
  background: rgba(176, 20, 91, 0.16);
}

/* --- top bar ------------------------------------------------------ */

.app {
  display: grid;
  grid-template-rows: var(--topbar-h) minmax(0, 1fr);
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 16px 0 20px;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
  position: relative;
  z-index: 30;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--ink);
  flex: none;
}

.brand-mark {
  width: 13px;
  height: 13px;
  color: var(--accent);
  flex: none;
}

.brand-name {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.012em;
  line-height: 1;
}

.topbar-spacer {
  flex: 1;
}

.topbar-nav {
  display: flex;
  align-items: center;
  gap: 2px;
}

.tlink {
  height: 30px;
  padding: 0 10px;
  border-radius: var(--radius);
  color: var(--ink-2);
  font-size: 13px;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.tlink:hover {
  background: rgba(26, 25, 21, 0.055);
  color: var(--ink);
}

.tlink:active {
  background: rgba(26, 25, 21, 0.09);
}

.tlink svg {
  width: 14px;
  height: 14px;
  flex: none;
}

/* --- workspace ---------------------------------------------------- */

.workspace {
  display: grid;
  grid-template-columns: minmax(360px, 46fr) 54fr;
  min-height: 0;
}

/* --- composer (input side) ---------------------------------------- */

.composer {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 14px 16px 16px 20px;
  gap: 10px;
}

.composer-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  min-height: 26px;
}

.tab {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  padding: 5px 10px 6px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: var(--radius) var(--radius) 0 0;
  background: var(--field);
  color: var(--ink-2);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.3;
  max-width: 100%;
  transform: translateY(1px);
}

.tab-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tab-mark {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
  transform: translateY(-1px);
}

.counter {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.02em;
  color: var(--muted);
  padding-bottom: 4px;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.composer-meta {
  display: flex;
  align-items: center;
  gap: 14px;
  padding-bottom: 5px;
}

.composer-meta .counter {
  padding-bottom: 0;
}

.file-link {
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-2);
  padding: 2px 0;
  text-decoration: underline;
  text-decoration-color: var(--line-strong);
  text-underline-offset: 2.5px;
  text-decoration-thickness: 1px;
}

.file-link:hover {
  color: var(--ink);
  text-decoration-color: var(--ink);
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.composer-body {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

.editor {
  flex: 1;
  width: 100%;
  min-height: 0;
  padding: 16px 18px 24px;
  border: 1px solid var(--line);
  border-radius: 0 var(--radius) var(--radius) var(--radius);
  background: var(--field);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.72;
  resize: none;
  caret-color: var(--accent);
  tab-size: 4;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.editor::placeholder {
  color: var(--muted);
  opacity: 1;
}

.editor:focus {
  outline: none;
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(176, 20, 91, 0.11);
}

.drop {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border: 1px dashed var(--ink);
  border-radius: var(--radius);
  background: rgba(251, 249, 244, 0.92);
  color: var(--ink);
  font-size: 13px;
  letter-spacing: 0.01em;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
  z-index: 2;
}

.composer[data-drag="true"] .drop {
  opacity: 1;
}

.composer-foot {
  display: flex;
  align-items: center;
  gap: 10px;
}

.hint {
  margin-left: auto;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
  letter-spacing: 0.02em;
  white-space: nowrap;
}

/* --- buttons ------------------------------------------------------ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 38px;
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 13.5px;
  font-weight: 600;
  letter-spacing: 0.002em;
  line-height: 1;
  white-space: nowrap;
  transition: background-color 120ms ease, border-color 120ms ease, color 120ms ease;
}

.btn-primary {
  background: var(--ink);
  border-color: var(--ink);
  color: #fbf9f4;
}

.btn-primary:hover {
  background: #2c2a24;
  border-color: #2c2a24;
}

.btn-primary:active {
  background: #000;
}

.btn-secondary {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--ink);
}

.btn-secondary:hover {
  border-color: var(--ink);
  background: var(--surface);
}

.btn-secondary:active {
  background: #f1eee6;
}

.btn[disabled] {
  opacity: 0.45;
  pointer-events: none;
}

.btn[data-busy="true"] {
  opacity: 0.8;
  pointer-events: none;
}

.spinner {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 1.5px solid currentColor;
  border-top-color: transparent;
  animation: spin 700ms linear infinite;
  flex: none;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* --- proof (output side) ------------------------------------------ */

.proof {
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-left: 1px solid var(--line);
  background: var(--paper-deep);
  position: relative;
}

.proof-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 20px;
  height: 38px;
  border-bottom: 1px solid rgba(26, 25, 21, 0.09);
  flex: none;
}

.proof-label {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--muted);
}

.spec {
  font-family: var(--mono);
  font-size: 11px;
  letter-spacing: 0.02em;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.proof-stage {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 26px 24px 34px;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}

.sheet-wrap {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: 820px;
}

/* The iframe keeps its unscaled layout box, so the clip has to live inside
   the wrapper — outside it the crop marks would be cut off. */
.sheet-clip {
  position: absolute;
  inset: 0;
  overflow: hidden;
  box-shadow: 0 1px 1px rgba(26, 25, 21, 0.06), 0 12px 30px -12px rgba(26, 25, 21, 0.32);
}

.preview {
  display: block;
  border: 0;
  background: transparent;
  transform-origin: 0 0;
}

.crop {
  position: absolute;
  width: 11px;
  height: 11px;
  border: 0 solid rgba(26, 25, 21, 0.42);
  pointer-events: none;
}

.crop.tl {
  top: -9px;
  left: -9px;
  border-top-width: 1px;
  border-left-width: 1px;
}

.crop.tr {
  top: -9px;
  right: -9px;
  border-top-width: 1px;
  border-right-width: 1px;
}

.crop.bl {
  bottom: -9px;
  left: -9px;
  border-bottom-width: 1px;
  border-left-width: 1px;
}

.crop.br {
  bottom: -9px;
  right: -9px;
  border-bottom-width: 1px;
  border-right-width: 1px;
}

.proof[data-busy="true"] .crop {
  border-color: var(--accent);
}

.progress {
  position: absolute;
  top: 38px;
  left: 0;
  right: 0;
  height: 1px;
  overflow: hidden;
  opacity: 0;
  transition: opacity 160ms ease;
  z-index: 3;
}

.proof[data-busy="true"] .progress {
  opacity: 1;
}

.progress::after {
  content: "";
  position: absolute;
  inset: 0;
  width: 34%;
  background: var(--accent);
  animation: slide 1.05s ease-in-out infinite;
}

@keyframes slide {
  0% {
    transform: translateX(-100%);
  }
  100% {
    transform: translateX(320%);
  }
}

/* --- settings popover --------------------------------------------- */

.pop {
  position: absolute;
  top: calc(var(--topbar-h) - 6px);
  right: 16px;
  width: 296px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
  z-index: 40;
}

.pop[hidden] {
  display: none;
}

.pop-field + .pop-field {
  margin-top: 16px;
}

.pop-label {
  display: block;
  margin-bottom: 7px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}

.seg {
  display: flex;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
}

.seg button {
  flex: 1;
  height: 30px;
  border-radius: 1px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}

.seg button:hover {
  color: var(--ink);
  background: rgba(26, 25, 21, 0.04);
}

.seg button[aria-pressed="true"] {
  background: var(--ink);
  color: #fbf9f4;
}

.pop-note {
  margin: 14px 0 0;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  line-height: 1.45;
  color: var(--muted);
}

.pop-extra {
  display: none;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  gap: 4px;
}

.pop-extra button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 9px 8px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink-2);
}

.pop-extra button:hover {
  background: rgba(26, 25, 21, 0.05);
  color: var(--ink);
}

/* --- alert & toast ------------------------------------------------ */

.alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 9px 11px;
  border: 1px solid #e0bcbc;
  border-radius: var(--radius);
  background: #fbf0ee;
  color: var(--danger);
  font-size: 12.5px;
  line-height: 1.4;
}

.alert[hidden] {
  display: none;
}

.alert strong {
  font-weight: 600;
}

.alert button {
  margin-left: auto;
  color: inherit;
  opacity: 0.7;
  font-size: 15px;
  line-height: 1;
  padding: 0 2px;
}

.alert button:hover {
  opacity: 1;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 10px);
  display: flex;
  align-items: center;
  gap: 9px;
  width: max-content;
  max-width: calc(100vw - 32px);
  padding: 10px 14px;
  background: var(--ink);
  color: #f7f5ee;
  border-radius: var(--radius);
  font-size: 13px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.16), 0 16px 36px -16px rgba(0, 0, 0, 0.6);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 60;
}

.toast[data-show="true"] {
  opacity: 1;
  transform: translate(-50%, 0);
}

.toast-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  flex: none;
}

/* Тост не ловит клики, но ссылку внутри него нажать можно и на телефоне. */
.toast-link {
  pointer-events: auto;
  color: #ffd9e8;
  font-weight: 600;
  white-space: nowrap;
  text-decoration: underline;
  text-underline-offset: 2px;
  text-decoration-thickness: 1px;
  padding: 6px 2px;
  margin: -6px 0;
}

.toast-link:hover {
  color: #fff;
}

.toast-link:focus-visible {
  outline: 2px solid #ffd9e8;
  outline-offset: 1px;
  border-radius: 1px;
}

/* --- mobile view switch ------------------------------------------- */

.viewswitch {
  display: none;
}

/* --- auth screen -------------------------------------------------- */

.auth {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 28px 20px;
  background: var(--paper);
}

.auth-card {
  width: 100%;
  max-width: 316px;
}

.auth-brand {
  margin-bottom: 34px;
}

.auth h1 {
  margin: 0 0 6px;
  font-size: 25px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.15;
}

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

.field {
  display: block;
}

.field input {
  width: 100%;
  height: 46px;
  padding: 0 13px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius);
  background: var(--field);
  font-size: 15px;
  caret-color: var(--accent);
}

.field input::placeholder {
  color: #8c877a;
}

.field input:focus {
  outline: none;
  border-color: var(--ink);
  box-shadow: 0 0 0 3px rgba(176, 20, 91, 0.11);
}

.auth .btn {
  width: 100%;
  height: 44px;
  margin-top: 12px;
}

.auth-err {
  margin-top: 12px;
  color: var(--danger);
  font-size: 12.5px;
  line-height: 1.4;
}

.auth-err:empty {
  margin: 0;
}

.auth-foot {
  margin-top: 40px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.03em;
  color: var(--muted);
}

/* --- narrow screens ----------------------------------------------- */

@media (max-width: 900px) {
  :root {
    --topbar-h: 54px;
  }

  .app {
    grid-template-rows: var(--topbar-h) minmax(0, 1fr);
  }

  .topbar {
    padding: 0 10px 0 14px;
    gap: 10px;
  }

  .brand-name {
    font-size: 16px;
  }

  .topbar-nav .tlink[data-optional] {
    display: none;
  }

  .viewswitch {
    display: flex;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    gap: 2px;
    padding: 2px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: rgba(251, 249, 244, 0.7);
  }

  .viewswitch button {
    height: 28px;
    padding: 0 13px;
    border-radius: 1px;
    font-size: 12.5px;
    font-weight: 600;
    color: var(--muted);
  }

  .viewswitch button[aria-pressed="true"] {
    background: var(--ink);
    color: #fbf9f4;
  }

  .workspace {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
  }

  .composer,
  .proof {
    grid-area: 1 / 1;
  }

  .composer {
    padding: 10px 0 0;
    gap: 0;
    pointer-events: none;
  }

  .composer > * {
    pointer-events: auto;
  }

  .composer-head {
    padding: 0 14px 8px;
    min-height: 0;
  }

  .tab {
    padding: 4px 8px 4px;
    font-size: 11.5px;
  }

  .composer-body {
    padding: 0;
  }

  .editor {
    border-left: 0;
    border-right: 0;
    border-radius: 0;
    padding: 12px 14px 96px;
    font-size: 13.5px;
    line-height: 1.68;
  }

  .editor:focus {
    border-color: var(--ink);
    box-shadow: none;
  }

  .composer-foot {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    gap: 8px;
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: rgba(239, 237, 230, 0.96);
    backdrop-filter: blur(6px);
    z-index: 25;
  }

  .composer-foot .btn {
    height: 46px;
  }

  .composer-foot .btn-primary {
    flex: 1;
  }

  .hint {
    display: none;
  }

  .alert {
    margin: 0 14px 8px;
  }

  .proof-bar {
    display: none;
  }

  .proof {
    border-left: 0;
  }

  .proof-stage {
    padding: 16px 12px calc(16px + env(safe-area-inset-bottom));
  }

  .progress {
    top: 0;
  }

  .pop {
    top: calc(var(--topbar-h) - 4px);
    right: 10px;
    left: 10px;
    width: auto;
    padding: 14px;
  }

  .pop-extra {
    display: flex;
    flex-direction: column;
  }

  .toast {
    bottom: calc(78px + env(safe-area-inset-bottom));
  }

  /* one pane at a time; the action bar stays put */
  body[data-view="text"] .proof {
    display: none;
  }

  body[data-view="sheet"] .composer-head,
  body[data-view="sheet"] .composer-body,
  body[data-view="sheet"] .alert,
  body[data-view="sheet"] .proof-bar {
    display: none;
  }
}

@media (max-width: 900px) {
  body[data-view="sheet"] .proof-stage {
    padding-bottom: calc(84px + env(safe-area-inset-bottom));
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
