:root {
  --bg: #f3f5f0;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #667085;
  --line: #d9ded6;
  --soft-line: #e7ebe4;
  --accent: #2364aa;
  --accent-strong: #164d86;
  --success: #2f7d57;
  --page-width: 210mm;
  --page-height: 297mm;
  --page-margin-top: 20mm;
  --page-margin-right: 20mm;
  --page-margin-bottom: 20mm;
  --page-margin-left: 20mm;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
}

button,
select,
input {
  font: inherit;
}

button,
select {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 7px;
  min-height: 34px;
}

button {
  cursor: pointer;
  padding: 0 11px;
  white-space: nowrap;
}

button:hover,
select:hover {
  border-color: #b8c3b8;
  background: #fbfcfa;
}

button:focus-visible,
select:focus-visible,
input:focus-visible,
.page:focus-visible {
  outline: 3px solid rgba(35, 100, 170, 0.28);
  outline-offset: 2px;
}

button.active,
.lang-button.active,
.menu-tab.active {
  border-color: var(--accent);
  background: #eaf2fb;
  color: var(--accent-strong);
}

.app {
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.97);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(14px);
}

.compactbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px clamp(12px, 3vw, 28px) 8px;
}

.brand,
.bar-right,
.quickbar,
.quick-group,
.menubar,
.menu-row {
  display: flex;
  align-items: center;
}

.brand {
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.brand-name {
  font-weight: 700;
  letter-spacing: 0;
}

.brand-byline {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
}

.bar-right {
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.language-switch {
  display: inline-flex;
  gap: 4px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #f8faf7;
}

.language-switch button {
  border-color: transparent;
  min-height: 30px;
}

.quickbar {
  gap: 8px;
  overflow-x: auto;
  padding: 7px clamp(12px, 3vw, 28px);
  border-top: 1px solid var(--soft-line);
  scrollbar-width: thin;
}

.quick-group {
  gap: 5px;
  min-width: max-content;
  padding-right: 8px;
  border-right: 1px solid var(--soft-line);
}

.quick-group:last-child {
  border-right: 0;
}

.text-quick select {
  min-width: 92px;
}

.font-size-field input {
  width: 112px;
  min-width: 112px;
  min-height: 34px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  text-align: right;
}

.text-quick label,
.menu-row label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.88rem;
  white-space: nowrap;
}

.icon-row button,
.text-quick button {
  min-width: 34px;
  padding: 0 8px;
  text-align: center;
}

input[type="color"] {
  width: 34px;
  height: 30px;
  padding: 2px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
}

select {
  padding: 0 28px 0 9px;
}

.menubar {
  gap: 4px;
  overflow-x: auto;
  padding: 0 clamp(12px, 3vw, 28px) 8px;
  scrollbar-width: thin;
}

.menu-tab {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  min-height: 30px;
  padding: 0 12px;
}

.menu-tab::after {
  content: "";
  display: inline-block;
  width: 0;
  height: 0;
  margin-left: 7px;
  border-left: 4px solid transparent;
  border-right: 4px solid transparent;
  border-top: 5px solid currentColor;
  transform: translateY(-1px);
}

.menu-tab.active::after {
  transform: translateY(-1px) rotate(180deg);
}

.menu-panel {
  border-top: 1px solid var(--soft-line);
  padding: 12px clamp(12px, 3vw, 28px) 14px;
  background: #fbfcfa;
  box-shadow: 0 16px 30px rgba(31, 41, 51, 0.08);
  max-height: min(54vh, 360px);
  overflow: auto;
}

.menu-section {
  max-width: 1280px;
  margin: 0 auto;
}

.menu-row {
  gap: 6px;
  flex-wrap: wrap;
}

.menu-row + .menu-row {
  margin-top: 7px;
}

.menu-row select {
  min-width: 112px;
}

.format-field input {
  width: 72px;
  min-height: 34px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  text-align: right;
}

.margin-fields {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.margin-fields > span:first-child {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
}

.margin-fields label {
  gap: 4px;
}

.help-section {
  max-width: 860px;
}

.help-content h2,
.help-content h3,
.help-content p {
  margin: 0;
}

.help-content h2 {
  font-size: 1rem;
  margin-bottom: 8px;
}

.help-content h3 {
  font-size: 0.92rem;
  margin-top: 14px;
  margin-bottom: 6px;
}

.help-content p {
  color: var(--muted);
  line-height: 1.5;
  margin-bottom: 7px;
}

.workspace {
  overflow: auto;
}

.document-stage {
  min-height: calc(100vh - 156px);
  padding: clamp(16px, 4vw, 46px) clamp(10px, 3vw, 32px) 64px;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.page {
  width: min(var(--page-width), calc(100vw - 24px));
  min-height: var(--page-height);
  padding: var(--page-margin-top) var(--page-margin-right) var(--page-margin-bottom) var(--page-margin-left);
  background-color: #fff;
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0,
    transparent calc(var(--page-height) - 2px),
    rgba(35, 100, 170, 0.16) calc(var(--page-height) - 2px),
    rgba(35, 100, 170, 0.16) var(--page-height)
  );
  box-shadow: 0 18px 45px rgba(31, 41, 51, 0.14), 0 0 0 1px rgba(31, 41, 51, 0.06);
  border-radius: 3px;
  color: #111827;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.page:empty::before {
  content: attr(data-placeholder);
  color: #9aa4b2;
  pointer-events: none;
}

.page h1,
.page h2,
.page h3 {
  line-height: 1.18;
  margin: 0.9em 0 0.45em;
}

.page h1 {
  font-size: 2rem;
}

.page h2 {
  font-size: 1.55rem;
}

.page h3 {
  font-size: 1.25rem;
}

.page p {
  margin: 0 0 0.75em;
}

.page ul,
.page ol {
  margin: 0 0 0.85em 1.6em;
  padding: 0;
}

.page table {
  width: 100%;
  border-collapse: collapse;
  margin: 1em 0;
  table-layout: fixed;
}

.page td,
.page th {
  border: 1px solid #aeb8c2;
  padding: 8px;
  min-width: 42px;
  vertical-align: top;
}

.page th {
  background: #eef3f8;
  font-weight: 700;
}

.page img {
  max-width: 100%;
  height: auto;
  cursor: pointer;
}

.page img.selected-image {
  outline: 3px solid rgba(35, 100, 170, 0.45);
  outline-offset: 3px;
}

.statusbar {
  min-height: 36px;
  display: flex;
  gap: 18px;
  align-items: center;
  justify-content: flex-start;
  padding: 8px clamp(12px, 3vw, 28px);
  background: #fff;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.9rem;
  flex-wrap: wrap;
}

.statusbar span {
  display: inline-flex;
  align-items: center;
  min-height: 22px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 52px;
  transform: translateX(-50%) translateY(12px);
  z-index: 100;
  max-width: min(560px, calc(100vw - 24px));
  padding: 11px 14px;
  border-radius: 8px;
  background: #1f2933;
  color: #fff;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  box-shadow: 0 12px 28px rgba(31, 41, 51, 0.2);
}

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

.context-menu {
  position: fixed;
  z-index: 120;
  min-width: 178px;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 16px 36px rgba(31, 41, 51, 0.18);
}

.context-menu button {
  display: block;
  width: 100%;
  min-height: 32px;
  border-color: transparent;
  background: transparent;
  text-align: left;
}

.context-menu button:hover,
.context-menu button:focus-visible {
  background: #eef3f8;
}

.context-menu hr {
  height: 1px;
  margin: 5px 4px;
  border: 0;
  background: var(--soft-line);
}

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

body.is-fullscreen .topbar {
  box-shadow: 0 10px 26px rgba(31, 41, 51, 0.08);
}

body.is-fullscreen .fullscreen-button {
  border-color: var(--success);
  color: var(--success);
  background: #edf8f1;
}

@media (max-width: 980px) {
  .compactbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .bar-right {
    justify-content: space-between;
    width: 100%;
    flex-wrap: wrap;
  }

  .quickbar {
    align-items: stretch;
  }
}

@media (max-width: 760px) {
  .language-switch {
    flex: 1 1 190px;
  }

  .language-switch button {
    flex: 1;
  }

  .fullscreen-button {
    flex: 1 1 170px;
  }

  .menu-panel {
    max-height: 58vh;
  }

  .document-stage {
    padding-left: 8px;
    padding-right: 8px;
  }

  .page {
    min-height: 78vh;
  }

  .statusbar {
    gap: 10px;
  }
}

@media print {
  body {
    background: #fff;
  }

  .topbar,
  .statusbar,
  .toast,
  .context-menu,
  input[type="file"] {
    display: none !important;
  }

  .app {
    display: block;
  }

  .workspace,
  .document-stage {
    display: block;
    padding: 0;
    overflow: visible;
    min-height: 0;
  }

  .page {
    width: auto;
    min-height: 0;
    padding: 0;
    box-shadow: none;
    border: 0;
    background-image: none;
  }

  @page {
    size: A4;
    margin: 20mm;
  }
}
