:root {
  --bg: #0f1419;
  --bg-panel: #151b24;
  --bg-elevated: #1a2230;
  --bg-input: #121820;
  --border: #2a3544;
  --border-strong: #3d4f66;
  --text: #e8ecf1;
  --text-muted: #8b97ab;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --radius: 8px;
  --font: "Inter", system-ui, sans-serif;
  --content-max: 720px;
}

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

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

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

.app-header--wysiwyg {
  grid-template-columns: 1fr 1fr;
}

.wysiwyg-workspace {
  max-width: 210mm;
}

.app-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 12px 16px;
  padding: 10px 14px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  justify-self: start;
}

.header-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  justify-self: end;
}

.back-link {
  color: var(--text-muted);
  text-decoration: none;
  font-size: 12px;
  font-weight: 600;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  white-space: nowrap;
}

.back-link:hover {
  color: var(--text);
  border-color: var(--border-strong);
}

.brand {
  font-weight: 700;
  font-size: 15px;
  white-space: nowrap;
}

.draft-status {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}

.header-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.header-divider {
  width: 1px;
  height: 24px;
  background: var(--border);
  align-self: center;
}

.view-tabs {
  display: inline-flex;
  gap: 6px;
  justify-self: center;
}

.view-tab-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-panel);
  color: var(--text-muted);
  border-radius: var(--radius);
  padding: 8px 28px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.view-tab-btn:hover {
  color: var(--text);
  border-color: var(--border-strong);
}

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

.view-tab-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.tab-panel.is-hidden,
.tab-panel[hidden] {
  display: none !important;
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: var(--radius);
  padding: 6px 12px;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover {
  border-color: var(--border-strong);
  background: #222b3a;
}

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

.btn-accent {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-accent:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

.btn-ghost {
  background: transparent;
}

.btn-sm {
  padding: 4px 10px;
  font-size: 12px;
}

.btn-icon {
  min-width: 28px;
  padding: 2px 6px;
}

.btn-danger {
  color: var(--danger);
  border-color: rgba(239, 68, 68, 0.4);
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.15);
}

.workspace {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  gap: 24px;
}

.workspace-inner {
  width: 100%;
  max-width: var(--content-max);
  margin-left: auto;
  margin-right: auto;
}

.editor-panel {
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}

.editor-scroll {
  width: 100%;
  margin-left: auto;
  margin-right: auto;
}

.preview-panel {
  width: 100%;
  display: flex;
  justify-content: center;
}

.preview-scroll {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 8px 0 16px;
}

.preview-container {
  width: 100%;
  max-width: 210mm;
}

.editor-section {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.editor-section h2 {
  margin: 0 0 14px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.section-head h2 {
  margin: 0;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

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

.field label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.field input,
.field select,
.field textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font: inherit;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.25);
}

.field-error input,
.field-error textarea,
.field-error select {
  border-color: var(--danger);
}

.field-msg {
  display: block;
  font-size: 11px;
  color: var(--danger);
  margin-top: 4px;
}

.logo-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-preview {
  width: 80px;
  height: 80px;
  border: 1px dashed var(--border);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  background: var(--bg-input);
}

.logo-preview img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.logo-placeholder {
  font-size: 11px;
  color: var(--text-muted);
}

.profile-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.profile-bar select {
  flex: 1;
  min-width: 160px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
}

.line-items-wrap {
  overflow-x: auto;
}

.line-items-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.line-items-table th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 0 6px 8px;
  border-bottom: 1px solid var(--border);
}

.line-items-table td {
  padding: 6px 4px;
  vertical-align: middle;
}

.line-items-table input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text);
  padding: 6px 8px;
  font: inherit;
  font-size: 13px;
  height: 34px;
  min-height: 34px;
  max-height: 34px;
  line-height: 20px;
  box-sizing: border-box;
  margin: 0;
  vertical-align: middle;
}

.line-items-table .line-desc-input {
  min-width: 140px;
  height: 34px;
  min-height: 34px;
  max-height: 34px;
  resize: none;
}

.line-items-footer {
  display: flex;
  justify-content: center;
  margin-top: 12px;
}

.line-items-table .col-desc,
.line-items-table th:first-child {
  min-width: 140px;
}

.line-items-table td:nth-child(2),
.line-items-table td:nth-child(3),
.line-items-table td:nth-child(4),
.line-items-table td:nth-child(5) {
  width: 72px;
}

.line-actions {
  white-space: nowrap;
  width: 90px;
}

.totals-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.totals-display {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.totals-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 13px;
  color: var(--text-muted);
}

.totals-row span:last-child {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.totals-bold {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  border-top: 1px solid var(--border);
  margin-top: 6px;
  padding-top: 10px;
}

.totals-accent {
  font-weight: 700;
  color: var(--accent);
}

.totals-accent span:last-child {
  color: var(--accent);
}

.history-section {
  width: 100%;
  max-width: var(--content-max);
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}

.history-title {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  text-align: center;
}

.history-hint {
  margin: 0 0 12px;
  font-size: 12px;
  text-align: center;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.history-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px;
  cursor: pointer;
  text-align: left;
}

.history-item:hover {
  border-color: var(--accent);
  background: rgba(59, 130, 246, 0.08);
}

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

.history-item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-item-name {
  font-weight: 600;
  font-size: 13px;
}

.history-item-meta {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.history-item-badge {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 3px 8px;
  border-radius: 4px;
  background: rgba(59, 130, 246, 0.15);
  color: var(--accent);
}

.history-item-date {
  font-size: 11px;
  color: var(--text-muted);
  white-space: nowrap;
}

.history-delete {
  flex-shrink: 0;
}

.empty-history {
  margin: 0;
  font-size: 13px;
  text-align: center;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
  max-width: 90vw;
}

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

.toast-error {
  border-color: var(--danger);
  color: #fecaca;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}

.modal-dialog {
  position: relative;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: min(440px, 92vw);
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.modal-dialog-sm {
  width: min(360px, 92vw);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 15px;
}

.modal-body {
  padding: 16px;
  overflow-y: auto;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.muted {
  color: var(--text-muted);
}

.hidden {
  display: none !important;
}

@media (max-width: 640px) {
  .app-header {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .header-left,
  .header-right {
    justify-self: center;
    justify-content: center;
  }

  .header-right {
    width: 100%;
  }

  .field-grid,
  .totals-grid {
    grid-template-columns: 1fr;
  }

  .header-actions {
    width: 100%;
    justify-content: center;
  }

  .history-item {
    flex-direction: column;
    align-items: stretch;
  }

  .history-item-right {
    justify-content: space-between;
  }

  .view-tab span {
    padding: 8px 20px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .toast,
  .view-tab-btn {
    transition: none;
  }
}
