:root {
  --ink: #18202f;
  --muted: #687385;
  --line: #d8dee8;
  --panel: #ffffff;
  --field: #f7f9fc;
  --surface: #edf1f5;
  --accent: #16735b;
  --accent-dark: #0e5b47;
  --gold: #b98227;
  --danger: #b42318;
  --page: #fffdf8;
  --shadow: 0 18px 50px rgba(25, 34, 48, 0.16);
  --soft-shadow: 0 10px 28px rgba(25, 34, 48, 0.08);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
  border: 0;
  cursor: pointer;
}

button:disabled {
  cursor: progress;
  opacity: 0.72;
}

.hidden {
  display: none !important;
}

.auth-gate {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(rgba(19, 35, 48, 0.74), rgba(19, 35, 48, 0.64)),
    url("https://images.unsplash.com/photo-1450101499163-c8848c66ca85?auto=format&fit=crop&w=1800&q=80") center/cover;
}

.auth-card {
  width: min(520px, 100%);
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 34px;
}

.auth-card h1 {
  margin: 0;
  font-size: 2rem;
  line-height: 1.12;
}

.auth-copy,
.subcopy,
.section-heading p,
.roadmap-panel li {
  color: var(--muted);
  line-height: 1.45;
}

.auth-copy {
  margin: 14px 0 22px;
}

.form-message {
  min-height: 22px;
  margin: 12px 0 0;
  color: var(--danger);
  font-weight: 700;
}

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

.workspace-panel {
  height: 100vh;
  overflow: auto;
  padding: 24px;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.brand-panel {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 18px;
  padding-bottom: 20px;
}

.eyebrow {
  margin: 0 0 7px;
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 850;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.45rem, 2.1vw, 2.05rem);
  line-height: 1.08;
  letter-spacing: 0;
}

h2 {
  margin: 0;
  font-size: 0.98rem;
  letter-spacing: 0;
}

.subcopy {
  margin: 10px 0 0;
}

.insight-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}

.metric-tile {
  min-height: 78px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfcfe;
  padding: 13px;
}

.metric-tile span {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 800;
}

.metric-tile strong {
  display: block;
  margin-top: 7px;
  font-size: 1.35rem;
}

.panel-section {
  border-top: 1px solid var(--line);
  padding: 21px 0;
}

.section-heading,
.action-bar,
.actions,
.mode-tabs,
.signature-wrap,
.two-col {
  display: flex;
  align-items: center;
}

.section-heading,
.action-bar {
  justify-content: space-between;
  gap: 14px;
}

.section-heading {
  margin-bottom: 14px;
}

.section-heading p {
  margin: 4px 0 0;
  font-size: 0.78rem;
}

.compact {
  margin-bottom: 12px;
}

.field {
  display: grid;
  gap: 7px;
  margin-bottom: 13px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 760;
}

input,
textarea,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--field);
  color: var(--ink);
  font-weight: 500;
  padding: 11px 12px;
  outline: 0;
}

input[type="file"] {
  padding: 9px;
}

textarea {
  resize: vertical;
  line-height: 1.5;
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(22, 115, 91, 0.14);
}

.two-col {
  gap: 12px;
}

.two-col .field {
  flex: 1;
}

.text-button {
  background: transparent;
  color: var(--accent);
  font-weight: 850;
}

.remove-asset {
  width: max-content;
  border-radius: 7px;
  background: #fff1f0;
  color: var(--danger);
  padding: 7px 9px;
  font-size: 0.76rem;
  font-weight: 850;
}

.remove-asset:hover {
  background: #ffd8d2;
}

.primary-action,
.actions button {
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  padding: 11px 15px;
  font-weight: 850;
}

.primary-action {
  width: 100%;
  background: var(--accent);
}

.icon-button {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 50%;
  background: #edf6f2;
  color: var(--accent-dark);
  font-size: 1.2rem;
}

.editor-panel {
  min-width: 0;
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.action-bar {
  min-height: 78px;
  padding: 16px 22px;
  background: rgba(255, 255, 255, 0.88);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(10px);
}

.mode-tabs {
  background: #dfe6ed;
  border-radius: 8px;
  padding: 4px;
}

.tab {
  min-width: 82px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  padding: 9px 13px;
  font-weight: 850;
}

.tab.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 4px 14px rgba(24, 33, 47, 0.1);
}

.actions {
  gap: 9px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.actions button:last-child {
  background: var(--accent);
}

.content-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 310px;
}

.edit-area,
.preview-area,
.report-panel {
  min-height: 0;
  overflow: auto;
  padding: 24px;
}

.body-field {
  max-width: 920px;
  margin: 0 auto;
}

#letterBody {
  min-height: calc(100vh - 170px);
  background: #fff;
  box-shadow: var(--shadow);
  border: 0;
  padding: 26px;
  font-size: 1rem;
}

.preview-area {
  display: grid;
  place-items: start center;
}

.pages-container {
  display: grid;
  gap: 24px;
  width: min(794px, 100%);
}

.page-preview {
  width: 100%;
  min-height: 1123px;
  background: var(--page);
  box-shadow: var(--shadow);
  position: relative;
  overflow: hidden;
}

.letterhead-img {
  width: 100%;
  display: block;
  max-height: 210px;
  object-fit: cover;
}

.letterhead-fallback {
  min-height: 112px;
  display: flex;
  align-items: center;
  padding: 28px 64px;
  border-bottom: 1px solid #e7e0d2;
  color: var(--accent-dark);
  font-size: 1.25rem;
}

.letter-content {
  padding: 52px 64px 72px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.02rem;
  line-height: 1.62;
}

.letter-meta {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 28px;
}

.letter-meta p,
.closing-block p,
.subject-line,
.recipient-block {
  margin: 0 0 16px;
  white-space: pre-line;
}

.subject-line {
  margin-top: 20px;
}

.body-copy p {
  margin: 0 0 16px;
}

.closing-block {
  margin-top: 44px;
  break-inside: avoid;
  page-break-inside: avoid;
}

.continuation-line,
.page-count {
  color: #7a6f60;
  font-family: Inter, ui-sans-serif, system-ui, sans-serif;
  font-size: 0.78rem;
}

.continuation-line {
  margin: 0 0 18px;
  text-align: right;
}

.page-count {
  position: absolute;
  right: 64px;
  bottom: 28px;
  margin: 0;
}

.signature-wrap {
  height: 112px;
  gap: 14px;
  margin: 4px 0;
}

.signature-only {
  height: auto;
  display: grid;
  justify-items: start;
  align-items: start;
  gap: 8px;
  margin-top: 32px;
  break-inside: avoid;
  page-break-inside: avoid;
}

.signature-img {
  max-width: 190px;
  max-height: 90px;
  object-fit: contain;
}

.seal-img {
  max-width: 115px;
  max-height: 105px;
  object-fit: contain;
  opacity: 0.9;
}

.sign-name {
  font-weight: 700;
}

.report-panel {
  border-left: 1px solid var(--line);
  background: #f8fafc;
}

.history-list {
  display: grid;
  gap: 10px;
}

.history-item {
  width: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: inherit;
  padding: 10px;
  box-shadow: var(--soft-shadow);
  transition: border-color 0.16s ease, box-shadow 0.16s ease, transform 0.16s ease;
}

.history-item:hover {
  border-color: var(--accent);
  box-shadow: 0 12px 30px rgba(22, 115, 91, 0.16);
  transform: translateY(-1px);
}

.history-open {
  min-width: 0;
  background: transparent;
  color: inherit;
  padding: 2px;
  text-align: left;
}

.history-open:focus,
.history-delete:focus {
  outline: 2px solid rgba(22, 115, 91, 0.35);
  outline-offset: 2px;
}

.history-open strong {
  display: block;
  font-size: 0.9rem;
}

.history-open span {
  display: block;
  margin-top: 5px;
  color: var(--muted);
  font-size: 0.78rem;
}

.history-delete {
  border-radius: 7px;
  background: #fff1f0;
  color: var(--danger);
  padding: 7px 9px;
  font-size: 0.76rem;
  font-weight: 850;
}

.history-delete:hover {
  background: #ffd8d2;
}

.empty-state {
  border: 1px dashed #c3cad6;
  border-radius: 8px;
  color: var(--muted);
  padding: 14px;
  line-height: 1.45;
}

.roadmap-panel {
  margin-top: 22px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}

.roadmap-panel ul {
  margin: 12px 0 0;
  padding-left: 18px;
}

.roadmap-panel li {
  margin-bottom: 8px;
}

.print-document {
  min-height: 100vh;
  display: grid;
  place-items: start center;
  padding: 24px;
}

.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;
}

@media (max-width: 1180px) {
  .content-grid {
    grid-template-columns: 1fr;
  }

  .report-panel {
    border-left: 0;
    border-top: 1px solid var(--line);
  }
}

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

  .workspace-panel,
  .editor-panel {
    height: auto;
  }

  .editor-panel {
    min-height: 100vh;
  }

  .action-bar,
  .letter-meta {
    align-items: stretch;
    flex-direction: column;
  }

  .mode-tabs,
  .actions {
    width: 100%;
  }

  .tab,
  .actions button {
    flex: 1;
  }

  .letter-content {
    padding: 38px 30px 52px;
  }
}

@media (max-width: 520px) {
  .workspace-panel,
  .edit-area,
  .preview-area,
  .report-panel {
    padding: 16px;
  }

  .insight-grid,
  .two-col {
    grid-template-columns: 1fr;
    display: grid;
  }

  .auth-card {
    padding: 24px;
  }
}

@media print {
  * {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  body {
    background: #fff;
  }

  .print-document {
    display: block;
    min-height: auto;
    padding: 0;
  }

  .workspace-panel,
  .action-bar,
  .edit-area,
  .report-panel {
    display: none !important;
  }

  .app-shell,
  .editor-panel,
  .content-grid,
  .preview-area,
  .pages-container {
    display: block;
    height: auto;
    padding: 0;
    overflow: visible;
    width: auto;
    gap: 0;
  }

  .page-preview {
    width: 210mm;
    height: 297mm;
    min-height: 297mm;
    box-shadow: none;
    margin: 0;
    overflow: hidden;
    page-break-after: always;
    break-after: page;
  }

  .page-preview:last-child {
    page-break-after: auto;
    break-after: auto;
  }

  .letterhead-img {
    max-height: 44mm;
    object-fit: cover;
  }

  .letterhead-fallback {
    min-height: 30mm;
    padding: 8mm 18mm;
    font-size: 16pt;
  }

  .letter-content {
    padding: 20mm 18mm 18mm;
    font-size: 10.6pt;
    line-height: 1.42;
  }

  .letter-meta {
    margin-bottom: 12mm;
  }

  .letter-meta p,
  .closing-block p,
  .subject-line,
  .recipient-block,
  .body-copy p {
    margin-bottom: 4.2mm;
  }

  .subject-line {
    margin-top: 6mm;
  }

  .closing-block {
    margin-top: 12mm;
    break-inside: avoid;
    page-break-inside: avoid;
  }

  .continuation-line {
    margin-bottom: 5mm;
  }

  .page-count {
    right: 18mm;
    bottom: 8mm;
  }

  .signature-wrap {
    height: 24mm;
  }

  .signature-only {
    height: auto;
    gap: 2mm;
  }

  .signature-img {
    max-width: 45mm;
    max-height: 21mm;
  }

  .seal-img {
    max-width: 28mm;
    max-height: 24mm;
  }

  @page {
    size: A4;
    margin: 0;
  }
}
