@font-face {
  font-family: "TH Sarabun New";
  src: url("./assets/fonts/THSarabunNew-Regular.ttf") format("truetype");
  font-style: normal;
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "TH Sarabun New";
  src: url("./assets/fonts/THSarabunNew-Bold.ttf") format("truetype");
  font-style: normal;
  font-weight: 700 900;
  font-display: swap;
}

@font-face {
  font-family: "TH Sarabun New";
  src: url("./assets/fonts/THSarabunNew-Italic.ttf") format("truetype");
  font-style: italic;
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "TH Sarabun New";
  src: url("./assets/fonts/THSarabunNew-BoldItalic.ttf") format("truetype");
  font-style: italic;
  font-weight: 700 900;
  font-display: swap;
}

:root {
  color-scheme: light;
  --thai-font: "TH Sarabun New", "THSarabunITY", "TH Sarabun PSK", "Sarabun", "Noto Sans Thai", system-ui, sans-serif;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-muted: #eef3f1;
  --ink: #1c2430;
  --muted: #637083;
  --line: #dbe1e8;
  --line-strong: #b8c2ce;
  --accent: #0f766e;
  --accent-strong: #0b5f59;
  --blue: #1d4ed8;
  --amber: #b45309;
  --red: #b42318;
  --shadow: 0 12px 28px rgba(30, 41, 59, 0.10);
  --radius: 8px;
}

* {
  box-sizing: border-box;
}

html {
  min-width: 320px;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--thai-font);
  letter-spacing: 0;
  font-size: 19px;
}

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

button {
  cursor: pointer;
}

.icon-sprite {
  display: none;
}

svg {
  display: block;
  width: 18px;
  height: 18px;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
  flex: 0 0 auto;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-shell[hidden],
.login-screen[hidden],
.page[hidden] {
  display: none !important;
}

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: linear-gradient(135deg, #eef7f5 0%, #f7f9fb 48%, #eef2f8 100%);
}

.login-panel {
  width: min(430px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px;
  display: grid;
  gap: 14px;
}

.login-panel h1 {
  margin: 0 0 4px;
  font-size: 1.9rem;
  line-height: 1.1;
}

.login-mark {
  width: 48px;
  height: 48px;
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  display: grid;
  place-items: center;
}

.login-mark svg {
  width: 26px;
  height: 26px;
}

.login-button {
  width: 100%;
  margin-top: 4px;
}

.form-error {
  margin: 0;
  color: var(--red);
  font-weight: 700;
}

.topbar {
  min-height: 78px;
  padding: 14px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand-block {
  min-width: 220px;
}

.topbar h1,
.panel-toolbar h2,
.preview-toolbar h2 {
  margin: 0;
  line-height: 1.15;
}

.topbar h1 {
  font-size: clamp(1.15rem, 1.1rem + 0.5vw, 1.55rem);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

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

.app-nav {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 280px;
}

.app-nav button {
  min-height: 40px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 10px;
  font-weight: 800;
  white-space: nowrap;
}

.app-nav button:hover {
  background: #f5f8fa;
  color: var(--ink);
}

.app-nav button.active {
  background: #ecf8f6;
  border-color: #b8ddd8;
  color: var(--accent-strong);
}

.button,
.icon-button {
  border: 1px solid transparent;
  border-radius: var(--radius);
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 700;
  transition: border-color 160ms ease, background-color 160ms ease, color 160ms ease, transform 160ms ease;
}

.button:active,
.icon-button:active {
  transform: translateY(1px);
}

.button {
  padding: 8px 12px;
}

.button.primary {
  background: var(--accent);
  color: white;
}

.button.primary:hover {
  background: var(--accent-strong);
}

.button.secondary {
  background: #ecf8f6;
  border-color: #b8ddd8;
  color: var(--accent-strong);
}

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

.button.ghost,
.icon-button {
  background: var(--surface);
  border-color: var(--line);
  color: var(--ink);
}

.button.ghost:hover,
.icon-button:hover {
  border-color: var(--line-strong);
  background: #f9fbfc;
}

.icon-button {
  width: 38px;
  padding: 0;
}

.icon-button.danger {
  color: var(--red);
}

.import-button {
  position: relative;
  overflow: hidden;
}

.import-button input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.main-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.workspace {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(250px, 300px) minmax(420px, 1fr) minmax(400px, 0.95fr);
  gap: 14px;
  padding: 14px;
  min-height: 0;
}

.workspace.work-only {
  grid-template-columns: minmax(260px, 320px) minmax(520px, 1fr);
}

.workspace.work-only .preview-panel {
  display: none;
}

.workspace.export-only {
  grid-template-columns: minmax(260px, 330px) minmax(560px, 1fr);
}

.workspace.export-only .editor-panel {
  display: none;
}

.workspace.export-only .preview-panel {
  display: flex;
}

.sidebar,
.editor-panel,
.preview-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-width: 0;
  min-height: 0;
  box-shadow: var(--shadow);
}

.sidebar {
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.search-box {
  min-height: 42px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0 10px;
  background: #fbfcfd;
}

.search-box svg {
  color: var(--muted);
}

.search-box input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.stats-grid div {
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.stats-grid span {
  display: block;
  font-size: 1.35rem;
  line-height: 1;
  font-weight: 800;
}

.stats-grid small {
  color: var(--muted);
}

.list-heading {
  display: grid;
  gap: 2px;
}

.list-heading h2 {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.1;
}

.segment {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #f8fafc;
}

.segment button {
  min-width: 0;
  min-height: 36px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 700;
}

.segment button.active {
  background: var(--surface);
  color: var(--accent-strong);
  box-shadow: 0 1px 4px rgba(15, 23, 42, 0.12);
}

.list-panel {
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.list-item {
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 10px;
  display: grid;
  gap: 4px;
}

.list-item:hover {
  border-color: var(--line-strong);
}

.list-item.active {
  border-color: var(--accent);
  background: #f0faf8;
}

.list-item strong,
.list-item span {
  overflow-wrap: anywhere;
}

.list-item strong {
  line-height: 1.25;
}

.list-item span {
  color: var(--muted);
  font-size: 0.86rem;
}

.status-row {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 24px;
  padding: 2px 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 700;
  background: #fbfcfd;
}

.chip.teal {
  border-color: #b8ddd8;
  color: var(--accent-strong);
  background: #ecf8f6;
}

.chip.amber {
  border-color: #f3d19a;
  color: var(--amber);
  background: #fff7ed;
}

.editor-panel,
.preview-panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.panel-toolbar,
.preview-toolbar {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.panel-toolbar.compact {
  padding: 12px 14px;
}

.panel-toolbar h2,
.preview-toolbar h2 {
  font-size: 1.08rem;
}

.dashboard-page,
.settings-page {
  flex: 1;
  min-height: 0;
  padding: 14px;
  overflow: auto;
}

.dashboard-head {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.dashboard-head h2 {
  margin: 0;
  font-size: 1.4rem;
}

.dashboard-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.metric-tile {
  min-height: 128px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  padding: 16px;
  text-align: left;
  display: grid;
  align-content: center;
  gap: 8px;
}

.metric-tile:hover {
  border-color: var(--accent);
}

.metric-tile span {
  color: var(--accent-strong);
  font-size: 2.6rem;
  font-weight: 900;
  line-height: 1;
}

.metric-tile strong {
  font-size: 1.05rem;
}

.recent-panel,
.settings-panel {
  margin-top: 14px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.recent-list {
  padding: 14px;
  display: grid;
  gap: 8px;
}

.recent-item {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  text-align: left;
  align-items: center;
}

.recent-item strong,
.recent-item span {
  overflow-wrap: anywhere;
}

.recent-item span {
  color: var(--muted);
}

#settingsForm {
  padding: 16px;
}

#editorForm {
  overflow: auto;
  min-height: 0;
  padding: 16px;
}

.form-section {
  margin-bottom: 18px;
}

.form-section h3 {
  margin: 0 0 10px;
  color: var(--ink);
  font-size: 0.98rem;
  line-height: 1.2;
}

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

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

.field {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.field.full {
  grid-column: 1 / -1;
}

.field span,
.inline-label {
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
  color: var(--ink);
  padding: 9px 10px;
  outline: 0;
}

.field textarea {
  min-height: 92px;
  resize: vertical;
}

.field input:focus,
.field textarea:focus,
.field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.12);
}

.field-hint {
  color: var(--muted);
  font-size: 0.86rem;
  line-height: 1.35;
}

.file-picker {
  position: relative;
  min-height: 42px;
  width: fit-content;
  max-width: 100%;
  padding: 9px 12px;
  border: 1px solid #b8ddd8;
  border-radius: var(--radius);
  background: #ecf8f6;
  color: var(--accent-strong);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-weight: 800;
  overflow: hidden;
}

.file-picker input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.inline-list {
  display: grid;
  gap: 8px;
}

.check-row,
.image-row {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fbfcfd;
}

.check-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
}

.check-row span {
  color: var(--ink);
  font-size: 0.92rem;
  overflow-wrap: anywhere;
}

.image-row {
  justify-content: space-between;
}

.image-row img {
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid var(--line);
}

.image-row button {
  border: 0;
  background: transparent;
  color: var(--red);
  font-weight: 800;
}

.sync-card {
  margin: 10px 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fbfcfd;
  padding: 10px 12px;
  display: grid;
  gap: 4px;
  color: var(--muted);
}

.sync-card > div {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--line-strong);
}

.status-dot.online {
  background: var(--accent);
}

.sync-error {
  color: var(--red);
  font-weight: 700;
}

.empty-state {
  display: grid;
  gap: 4px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 16px;
  color: var(--muted);
}

.empty-state strong {
  color: var(--ink);
}

.paper-scroll {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 18px;
  background: #dfe5ec;
}

#printArea {
  display: grid;
  gap: 18px;
  justify-items: center;
}

@media screen and (min-width: 1281px) {
  #printArea {
    zoom: 0.66;
  }
}

@media screen and (max-width: 1280px) and (min-width: 821px) {
  #printArea {
    zoom: 0.82;
  }
}

.report-paper,
.appendix-paper {
  width: 210mm;
  min-height: 297mm;
  background: #fff;
  color: #000;
  padding: 19mm 20mm 16mm;
  box-shadow: 0 14px 30px rgba(15, 23, 42, 0.22);
  font-family: var(--thai-font);
  font-size: 18pt;
  line-height: 1.22;
}

.official-heading,
.official-subtitle,
.official-stars {
  text-align: center;
}

.official-heading {
  font-weight: 700;
  font-size: 18pt;
  line-height: 1.22;
}

.official-subtitle {
  margin-top: 1mm;
  font-weight: 700;
  line-height: 1.22;
}

.official-stars {
  margin: 8mm 0 5mm;
  letter-spacing: 0;
}

.official-line {
  display: grid;
  grid-template-columns: 29mm 1fr;
  column-gap: 5mm;
  align-items: baseline;
  min-height: 7.5mm;
}

.official-line .label,
.block-label {
  font-weight: 700;
}

.official-paragraph {
  margin: 1mm 0 0;
  text-indent: 17mm;
}

.official-block {
  margin-top: 3mm;
}

.official-block .value,
.value-block {
  min-height: 11mm;
  white-space: pre-line;
  overflow-wrap: anywhere;
}

.date-grid {
  display: grid;
  grid-template-columns: 29mm 1fr 22mm 25mm 17mm;
  column-gap: 5mm;
  align-items: baseline;
  min-height: 8mm;
}

.spacer-lg {
  height: 17mm;
}

.closing {
  margin-top: 13mm;
  text-indent: 17mm;
}

.signature-area {
  margin-top: 15mm;
  margin-left: auto;
  width: 78mm;
  text-align: center;
}

.signature-line {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 2mm;
}

.dotted {
  border-bottom: 1px dotted #000;
  min-width: 42mm;
  min-height: 6mm;
  display: inline-block;
}

.admin-area {
  margin-top: 16mm;
}

.comment-lines {
  display: grid;
  gap: 6mm;
  margin-top: 3mm;
}

.comment-line {
  border-bottom: 1px dotted #000;
  min-height: 7mm;
  white-space: pre-line;
}

.review-signature {
  margin-top: 10mm;
  margin-left: auto;
  width: 82mm;
  text-align: center;
}

.appendix-paper h2 {
  margin: 0 0 8mm;
  font-size: 20pt;
  text-align: center;
}

.appendix-item {
  break-inside: avoid;
  margin-bottom: 8mm;
  border-bottom: 1px solid #bbb;
  padding-bottom: 5mm;
}

.appendix-item h3 {
  margin: 0 0 2mm;
  font-size: 18pt;
}

.appendix-meta {
  color: #333;
  font-size: 15pt;
}

.appendix-images {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 5mm;
  margin-top: 4mm;
}

.appendix-images img {
  width: 100%;
  max-height: 72mm;
  object-fit: cover;
  border: 1px solid #aaa;
}

@media (max-width: 1280px) {
  .workspace,
  .workspace.work-only,
  .workspace.export-only {
    grid-template-columns: minmax(230px, 280px) minmax(380px, 1fr);
  }

  .preview-panel {
    grid-column: 1 / -1;
    min-height: 70vh;
  }

  .workspace.export-only .preview-panel {
    grid-column: auto;
  }
}

@media (max-width: 820px) {
  .topbar,
  .dashboard-head,
  .panel-toolbar,
  .preview-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .topbar-actions,
  .app-nav,
  .dashboard-actions,
  .panel-actions {
    width: 100%;
  }

  .brand-block {
    min-width: 0;
  }

  .app-nav {
    justify-content: flex-start;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .app-nav button {
    flex: 0 0 auto;
  }

  .button {
    flex: 1 1 auto;
  }

  .workspace,
  .workspace.work-only,
  .workspace.export-only {
    grid-template-columns: 1fr;
    padding: 10px;
  }

  .dashboard-page,
  .settings-page {
    padding: 10px;
  }

  .dashboard-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .recent-item {
    grid-template-columns: 1fr;
  }

  .sidebar,
  .editor-panel,
  .preview-panel {
    min-height: auto;
  }

  .form-grid,
  .form-grid.three {
    grid-template-columns: 1fr;
  }

  .paper-scroll {
    padding: 10px;
  }

  .report-paper,
  .appendix-paper {
    width: min(210mm, 100%);
    padding: 14mm 10mm;
    font-size: 15pt;
  }

  #printArea {
    zoom: 1;
  }

  .official-line,
  .date-grid {
    grid-template-columns: 1fr;
    gap: 1mm;
  }
}

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

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

  .login-screen,
  .app-shell > .topbar,
  .dashboard-page,
  .settings-page,
  .sidebar,
  .editor-panel,
  .preview-toolbar {
    display: none !important;
  }

  .app-shell,
  .main-area,
  .workspace,
  .preview-panel,
  .paper-scroll,
  #printArea {
    display: block;
    padding: 0;
    margin: 0;
    width: auto;
    min-height: 0;
    border: 0;
    box-shadow: none;
    background: #fff;
    overflow: visible;
  }

  .report-paper,
  .appendix-paper {
    width: 210mm;
    min-height: 297mm;
    padding: 19mm 20mm 16mm;
    box-shadow: none;
    page-break-after: always;
  }

  .appendix-paper:last-child,
  .report-paper:last-child {
    page-break-after: auto;
  }
}
