:root {
  color-scheme: light;
  --ink: #111827;
  --muted: #64748b;
  --line: #d7dee8;
  --panel: #f6f7f9;
  --accent: #95c11f;
  --accent-dark: #84ad18;
  --warning: #ff7816;
  --danger: #ef4444;
  --paper: #ffffff;
  --shadow: 0 2px 8px rgba(15, 23, 42, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Arial, Helvetica, sans-serif;
  color: var(--ink);
  background: var(--panel);
}

body.modal-open {
  overflow: hidden;
}

.app-frame {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
}

.sidebar {
  min-height: 100vh;
  padding: 32px 12px;
  background: var(--paper);
  border-right: 1px solid #e5eaf1;
  box-shadow: 2px 0 14px rgba(15, 23, 42, 0.06);
}

.brand {
  display: flex;
  justify-content: center;
  margin-bottom: 36px;
}

.brand img {
  width: min(210px, 82%);
  height: auto;
}

.side-nav {
  display: grid;
  gap: 6px;
  padding: 16px;
  border: 1px solid #e1e7ef;
  border-radius: 8px;
}

.side-link {
  display: block;
  padding: 10px 12px;
  border-radius: 6px;
  color: #475569;
  text-decoration: none;
  font-size: 15px;
}

.side-link.is-active {
  color: var(--ink);
  background: #eef6d8;
  font-weight: 700;
}

.side-link[aria-disabled="true"] {
  opacity: 0.65;
  pointer-events: none;
}

.app-shell {
  min-height: 100vh;
  padding: 28px 32px;
}

.workspace {
  max-width: 1500px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.language-picker {
  display: flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
}

.language-picker select {
  width: auto;
  min-width: 118px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 28px;
  font-weight: 700;
}

h2 {
  font-size: 18px;
}

h3 {
  margin: 0;
  font-size: 15px;
}

.topbar p,
.status,
.preview-empty {
  color: var(--muted);
}

.icon-button,
.secondary-button,
.primary-button,
.danger-button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  min-height: 42px;
  padding: 0 16px;
  cursor: pointer;
}

.icon-button {
  width: 44px;
  padding: 0;
  font-size: 22px;
}

.primary-button {
  width: 100%;
  border-color: var(--accent);
  background: var(--accent);
  color: white;
  font-weight: 700;
}

.primary-button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.danger-button {
  border-color: var(--danger);
  background: var(--danger);
  color: #fff;
}

.danger-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.main-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 18px;
  align-items: start;
}

.drop-panel {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  box-shadow: var(--shadow);
}

.config-dialog[hidden] {
  display: none;
}

.config-dialog {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 24px;
}

.config-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(22, 28, 36, 0.62);
}

.config-panel {
  position: relative;
  z-index: 1;
  width: min(1180px, calc(100vw - 48px));
  max-height: calc(100vh - 48px);
  overflow: auto;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 24px 70px rgba(15, 23, 42, 0.34);
}

.field-label,
legend,
label {
  font-size: 14px;
  font-weight: 700;
}

select,
input {
  width: 100%;
  min-height: 42px;
  border: 1px solid #bfc7d2;
  border-radius: 6px;
  padding: 8px 12px;
  background: white;
  color: var(--ink);
  font-size: 15px;
}

#template-select optgroup {
  color: var(--muted);
  font-weight: 700;
}

#template-select option {
  color: var(--ink);
  font-weight: 400;
}

#template-select option.raw-format-match {
  background: #eef6d8;
  color: #1f3a07;
  font-weight: 700;
}

.drop-zone {
  position: relative;
  display: grid;
  place-items: center;
  gap: 4px;
  min-height: 165px;
  margin: 16px 0;
  border: 2px dashed #94a3b8;
  border-radius: 8px;
  background: var(--panel);
  text-align: center;
  cursor: pointer;
}

.drop-zone input {
  display: none;
}

.drop-zone.is-dragging {
  border-color: var(--accent);
  background: #eef6d8;
}

.preview-wrap {
  position: relative;
  height: min(68vh, 760px);
  min-height: 430px;
  margin: 12px 0 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: #f0f2f5;
}

.pdf-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  align-items: center;
  margin: 0 0 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  font-size: 14px;
}

.pdf-meta[hidden] {
  display: none;
}

.preview-wrap object {
  width: 100%;
  height: 100%;
}

.preview-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}

.preview-empty[hidden],
.preview-wrap.has-pdf .preview-empty {
  display: none;
}

.history-panel {
  margin-top: 16px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
}

.history-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.history-head span,
.history-empty {
  color: var(--muted);
  font-size: 13px;
}

.history-list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.history-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border: 1px solid #e1e7ef;
  border-radius: 6px;
  background: var(--paper);
}

.history-list a {
  min-width: 0;
  overflow: hidden;
  color: #2f5f00;
  font-weight: 700;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-list span {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
}

.history-empty {
  margin-top: 8px;
}

.history-empty[hidden] {
  display: none;
}

.panel-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  position: sticky;
  top: -20px;
  z-index: 2;
  padding: 0 0 12px;
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

fieldset {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 14px 0 0;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
}

.config-panel fieldset,
.layout-preview-panel {
  break-inside: avoid;
}

.config-panel {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0 18px;
}

.config-panel .panel-title,
.config-panel > .field-label,
.config-panel > #template-name {
  grid-column: 1 / -1;
}

.layout-grid-settings,
.layout-preview-panel,
.compact-layout-settings {
  grid-column: 1 / -1;
}

legend {
  padding: 0 6px;
}

legend .tooltip {
  margin-left: 6px;
}

.tooltip {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.tooltip-trigger {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 1px solid var(--accent);
  border-radius: 50%;
  background: #eef6d8;
  color: #3b5600;
  font-size: 13px;
  font-weight: 700;
  cursor: help;
}

.tooltip-content {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 10px);
  z-index: 5;
  min-width: 190px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  box-shadow: var(--shadow);
  font-size: 13px;
  font-weight: 400;
  line-height: 1.45;
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(4px);
  transition: opacity 120ms ease, transform 120ms ease;
  white-space: pre-line;
}

.tooltip-content::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  width: 10px;
  height: 10px;
  background: var(--ink);
  transform: translate(-50%, -50%) rotate(45deg);
}

.tooltip:hover .tooltip-content,
.tooltip:focus-within .tooltip-content {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

fieldset > select,
fieldset > .checkbox {
  align-self: end;
}

.layout-grid-settings {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.compact-layout-settings {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px 10px;
  margin-top: 10px;
  padding: 10px 12px 12px;
}

.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  font-weight: 700;
}

.checkbox input {
  width: auto;
  min-height: auto;
}

label {
  display: grid;
  gap: 5px;
}

.config-panel fieldset label {
  grid-template-rows: 22px minmax(42px, auto);
  align-items: end;
}

.config-panel fieldset label > span {
  align-self: end;
}

.compact-layout-settings label {
  gap: 3px;
}

.config-panel .compact-layout-settings label {
  grid-template-rows: 18px minmax(36px, auto);
}

.compact-layout-settings label,
.compact-layout-settings label span {
  font-size: 13px;
}

.compact-layout-settings input,
.compact-layout-settings select {
  min-height: 36px;
  padding: 6px 10px;
  font-size: 14px;
}

.compact-layout-settings .checkbox {
  align-self: end;
  min-height: 36px;
  padding: 0 2px;
}

.config-panel .compact-layout-settings .checkbox {
  grid-template-rows: auto;
}

.status {
  min-height: 22px;
  margin-top: 10px;
  font-size: 14px;
}

.layout-preview-panel {
  margin-top: 14px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
}

.layout-preview-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.layout-preview-head span {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.layout-preview-stage {
  display: grid;
  place-items: center;
  min-height: 320px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #dce2ea;
  overflow: hidden;
}

.layout-preview-sheet {
  position: relative;
  background: #ffffff;
  border: 1px solid #a8b2c0;
  box-shadow: 0 8px 22px rgba(27, 37, 51, 0.18);
}

.layout-preview-margin {
  position: absolute;
  border: 1px dashed #aab3bf;
  background: rgba(38, 118, 90, 0.04);
  pointer-events: none;
}

.layout-preview-item {
  position: absolute;
  display: grid;
  place-items: center;
  border: 1px solid #26765a;
  background: #eaf6f1;
  overflow: visible;
}

.layout-preview-bleed {
  position: absolute;
  border: 1px dashed #c03d3d;
  background: rgba(192, 61, 61, 0.04);
}

.layout-preview-sample {
  position: absolute;
  inset: 14%;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 3px;
  border: 1px solid #6b9fbc;
  background:
    linear-gradient(135deg, rgba(107, 159, 188, 0.16) 25%, transparent 25% 50%, rgba(107, 159, 188, 0.16) 50% 75%, transparent 75%),
    #ffffff;
  background-size: 14px 14px;
  color: #1f3b4d;
  font-size: 11px;
  text-align: center;
}

.layout-preview-sample strong {
  font-size: 16px;
}

@media (max-width: 1050px) {
  .app-frame {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    padding: 16px;
  }

  .brand {
    margin-bottom: 14px;
  }

  .side-nav {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .main-grid {
    grid-template-columns: 1fr;
  }

  .config-panel {
    grid-template-columns: 1fr;
  }

  .compact-layout-settings {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 620px) {
  .app-shell {
    padding: 12px;
  }

  .config-dialog {
    padding: 10px;
  }

  .config-panel {
    width: calc(100vw - 20px);
    max-height: calc(100vh - 20px);
    padding: 14px;
  }

  .panel-title {
    top: -14px;
  }

  fieldset {
    grid-template-columns: 1fr;
  }

  .layout-grid-settings,
  .compact-layout-settings {
    grid-template-columns: 1fr;
  }
}
