:root {
  --ui-bg: #151413;
  --ui-panel: #211f1d;
  --ui-panel-2: #2b2824;
  --ui-text: #f2eee7;
  --ui-muted: #afa69a;
  --ui-line: rgba(255, 255, 255, .12);
  --ui-accent: #b25f36;
  --preview-zoom: .75;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  background: var(--ui-bg);
  color: var(--ui-text);
  font: 14px/1.4 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input, textarea, select { font: inherit; }
button {
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: #302c27;
  color: var(--ui-text);
  padding: 8px 10px;
  cursor: pointer;
}
button:hover { background: #3a352f; }
button.primary {
  border-color: transparent;
  background: #9f4d2f;
}
button.danger { color: #ffd7d0; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--ui-line);
  background: rgba(21, 20, 19, .96);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 6px;
  background: linear-gradient(135deg, var(--ui-accent), #3b2118);
  font-weight: 800;
}
.brand strong, .brand small { display: block; }
.brand small, .muted, .brew-row small { color: var(--ui-muted); }
.actions { display: flex; flex-wrap: wrap; gap: 8px; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(360px, 43vw) minmax(0, 1fr);
  min-height: calc(100vh - 56px);
}
.editor-panel {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - 56px);
  border-right: 1px solid var(--ui-line);
  background: var(--ui-panel);
}
.meta-row {
  display: grid;
  grid-template-columns: 56px 1fr;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-bottom: 1px solid var(--ui-line);
}
.meta-row input {
  width: 100%;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: #151413;
  color: var(--ui-text);
  padding: 8px 10px;
}
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--ui-line);
}
.toolbar button { padding: 6px 8px; }
textarea {
  width: 100%;
  border: 0;
  outline: 0;
  resize: none;
  background: #11100f;
  color: #f6ead8;
  font: 13px/1.55 "Cascadia Code", "Fira Code", Consolas, monospace;
}
#markdownInput {
  flex: 1;
  min-height: 46vh;
  padding: 14px;
}
.style-panel {
  border-top: 1px solid var(--ui-line);
  background: var(--ui-panel-2);
}
.style-panel label {
  display: block;
  padding: 8px 12px;
  color: var(--ui-muted);
}
#customCss {
  min-height: 120px;
  max-height: 220px;
  padding: 10px 14px;
  border-top: 1px solid var(--ui-line);
}
.hidden-file { display: none; }

.preview-panel {
  min-width: 0;
  max-height: calc(100vh - 56px);
  overflow: auto;
  background: #2d2d2d;
}
.preview-tools {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: rgba(45, 45, 45, .92);
  border-bottom: 1px solid rgba(255, 255, 255, .1);
}
.preview-tools input { width: 140px; }
.pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 24px;
}
.pages .phb {
  color: #1f1812;
  flex: 0 0 auto;
  transform: scale(var(--preview-zoom));
  transform-origin: top center;
  margin-bottom: calc((1056px * var(--preview-zoom)) - 1056px);
}

.phb p,
.phb li,
.phb td,
.phb th,
.phb dd,
.phb dt,
.phb blockquote {
  color: inherit;
}
.phb img { max-width: 100%; }
.phb .note {
  color: #1f1812;
  margin-bottom: 1em;
  padding: 12px;
  background: #e0e5c1;
  border: 1px solid #b5bd89;
}
.phb .monster {
  color: #1f1812;
  margin-bottom: 1em;
  padding: 10px;
  background: #f2e5b5;
  border-top: 4px solid #7a200d;
  border-bottom: 4px solid #7a200d;
}
.phb .monster h3 { margin-top: 0; }
.phb .artist {
  position: absolute;
  color: #1f1812;
  font-size: .8em;
  text-align: center;
}
.phb .blank {
  height: 1em;
}
.phb dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  column-gap: 6px;
  margin: 0 0 .35em;
}
.phb dt {
  font-weight: 700;
}
.phb dd {
  margin: 0;
}
.phb table { width: 100%; margin-bottom: 1em; }
.phb th, .phb td { padding: 3px 6px; }

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 40;
  opacity: 0;
  transform: translateY(8px);
  padding: 10px 12px;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: #11100f;
  transition: .18s ease;
}
.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

dialog {
  width: min(720px, calc(100vw - 32px));
  border: 1px solid var(--ui-line);
  border-radius: 8px;
  background: var(--ui-panel);
  color: var(--ui-text);
}
dialog::backdrop { background: rgba(0, 0, 0, .55); }
dialog header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}
.brew-list { display: grid; gap: 8px; }
.brew-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: rgba(255, 255, 255, .04);
}
.row-actions { display: flex; gap: 6px; }
.asset-dialog {
  width: min(920px, calc(100vw - 32px));
}
.asset-upload-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
.asset-upload-row code {
  color: #f8dfb8;
}
.asset-drop-zone {
  display: grid;
  place-items: center;
  min-height: 74px;
  margin-bottom: 14px;
  border: 1px dashed rgba(255, 255, 255, .28);
  border-radius: 6px;
  background: rgba(255, 255, 255, .035);
  color: var(--ui-muted);
}
.asset-drop-zone.dragging {
  border-color: #f4d7a9;
  background: rgba(178, 95, 54, .18);
  color: var(--ui-text);
}
.asset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 10px;
  max-height: 62vh;
  overflow: auto;
}
.asset-card {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: rgba(255, 255, 255, .04);
}
.asset-card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  border-radius: 4px;
  background: #11100f;
}
.asset-card strong,
.asset-card small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.asset-card .row-actions {
  flex-wrap: wrap;
}
.template-dialog {
  width: min(980px, calc(100vw - 32px));
}
.template-dialog h2 {
  margin: 0 0 10px;
  color: #f4d7a9;
  font-size: 16px;
}
.tips-panel {
  margin-bottom: 16px;
  padding: 12px;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: rgba(255, 255, 255, .035);
}
.tip-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 8px;
}
.tip-item {
  display: grid;
  grid-template-columns: minmax(120px, auto) 1fr;
  gap: 10px;
  align-items: start;
  min-width: 0;
}
.tip-item code {
  overflow-wrap: anywhere;
  color: #f8dfb8;
}
.tip-item span {
  color: var(--ui-muted);
}
.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 10px;
  max-height: 48vh;
  overflow: auto;
}
.template-card {
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 8px;
  min-height: 150px;
  padding: 12px;
  border: 1px solid var(--ui-line);
  border-radius: 6px;
  background: rgba(255, 255, 255, .045);
}
.template-card h3 {
  margin: 0;
  font-size: 15px;
}
.template-card p {
  margin: 0;
  color: var(--ui-muted);
}
.template-card button {
  justify-self: start;
}

@media (max-width: 980px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .app-shell { grid-template-columns: 1fr; }
  .editor-panel { min-height: auto; border-right: 0; border-bottom: 1px solid var(--ui-line); }
  .preview-panel { max-height: none; }
}

@page { size: letter; margin: 0; }
@media print {
  body { background: white; }
  .no-print { display: none !important; }
  .app-shell, .preview-panel, .pages {
    display: block;
    margin: 0;
    padding: 0;
    background: white;
    overflow: visible;
  }
  .pages .phb {
    transform: none;
    margin: 0;
    box-shadow: none;
    break-after: page;
    page-break-after: always;
  }
  .pages .phb:last-child {
    break-after: auto;
    page-break-after: auto;
  }
}
