:root {
  --bg: #0f1419;
  --panel: #1a2029;
  --panel-2: #222b36;
  --line: #2e3846;
  --text: #e7edf3;
  --muted: #8a97a6;
  --brand: #4c9eff;
  --brand-2: #7c5cff;
  --ok: #34d399;
  --danger: #f87171;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  /* Layered brand glow outside the cards; cards keep their solid --panel. */
  background:
    radial-gradient(820px 480px at 10% -6%, rgba(76, 158, 255, 0.20), transparent 62%),
    radial-gradient(900px 560px at 92% 4%, rgba(124, 92, 255, 0.18), transparent 60%),
    radial-gradient(700px 520px at 50% 116%, rgba(52, 211, 153, 0.10), transparent 60%),
    linear-gradient(180deg, #10151c 0%, var(--bg) 55%, #0b0f14 100%);
  background-attachment: fixed;
  color: var(--text);
  font-family: "Noto Sans JP", system-ui, -apple-system, "Segoe UI", sans-serif;
  padding-bottom: 184px; /* clear the fixed footer (bottom:88 + 76) + launcher */
}

.topbar {
  display: flex; align-items: baseline; gap: 16px;
  padding: 18px 28px; border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 9px; font-size: 26px; font-weight: 800; letter-spacing: -0.5px; }
.brand .wordmark span { color: var(--brand); }
.brand-icon { width: 30px; height: 30px; display: block; filter: drop-shadow(0 2px 6px rgba(76,158,255,0.35)); }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.topbar-right .stat-badge { margin-left: 0; }
.lang-select { width: auto; padding: 7px 10px; font-size: 13px; }
button.big { width: 100%; padding: 16px 24px; font-size: 17px; margin-top: 4px; }
.howto { max-width: 760px; }
.howto h3 { font-size: 16px; margin: 20px 0 6px; color: var(--text); }
.howto p { margin: 0; color: var(--muted); line-height: 1.8; }
.howto .row { justify-content: flex-start; }
.howto-top { position: sticky; top: 0; z-index: 1; padding-bottom: 12px; margin-bottom: 4px; background: var(--panel); }
.tag { color: var(--muted); font-size: 13px; }
.stat-badge {
  margin-left: auto; font-size: 13px; font-weight: 700; color: var(--ok);
  background: rgba(52, 211, 153, 0.12); border: 1px solid rgba(52, 211, 153, 0.35);
  padding: 6px 12px; border-radius: 999px; white-space: nowrap;
}

.view { display: none; padding: 32px; }
.view.active { display: block; }

.card {
  max-width: 640px; margin: 6vh auto; background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 28px;
}
.card h1 { margin: 0 0 6px; font-size: 22px; text-align: center; }
.card > .muted { text-align: center; }

.field { display: block; margin: 14px 0; }
.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
.field.inline { display: flex; align-items: center; gap: 12px; }
.field.inline > span { margin: 0; }
.field.checkbox { display: flex; align-items: center; gap: 8px; }
.field.checkbox > span { margin: 0; color: var(--text); }

input[type="text"], input[type="url"], textarea, select {
  width: 100%; padding: 11px 12px; background: var(--panel-2);
  color: var(--text); border: 1px solid var(--line); border-radius: 8px;
  font: inherit;
}
input[type="range"] { width: 100%; accent-color: var(--brand); }
input[type="color"] { width: 46px; height: 32px; border: none; background: none; }
textarea { resize: vertical; }

.row { display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap; }
button {
  cursor: pointer; font: inherit; font-weight: 600; border-radius: 8px;
  padding: 11px 18px; border: 1px solid transparent;
}
button.primary { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; }
button.ghost { background: transparent; border-color: var(--line); color: var(--text); }
button.accent {
  background: rgba(124, 92, 255, 0.14); border-color: rgba(124, 92, 255, 0.5);
  color: #cbb9ff;
}
button.accent:hover { background: rgba(124, 92, 255, 0.24); }
button.small { padding: 7px 12px; font-size: 13px; }
button:disabled { opacity: .5; cursor: not-allowed; }

.muted { color: var(--muted); font-size: 13px; }
.center { text-align: center; }
.status { min-height: 20px; margin-top: 12px; font-size: 13px; }
.status.err { color: var(--danger); }
.status.ok { color: var(--ok); }
hr { border: none; border-top: 1px solid var(--line); margin: 18px 0; }

/* book editor (markdown / preview) */
.book-head { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.book-head input[type="text"] { flex: 1; min-width: 160px; }
.book-head .chk { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; white-space: nowrap; }
.book-head .sel { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; white-space: nowrap; }
.book-head .sel select { width: auto; padding: 8px 10px; }
/* tabs in row 1 (col 2); cover + panes share row 2 → cover top aligns with the
   manuscript pane, not the tabs. */
.book-layout {
  display: grid; grid-template-columns: 200px 1fr; grid-template-rows: auto 1fr;
  column-gap: 18px; align-items: start;
}
.book-tabs { grid-column: 2; grid-row: 1; }
.cover-panel { grid-column: 1; grid-row: 2; }
.book-panes { grid-column: 2; grid-row: 2; min-width: 0; }
.cover-panel { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
.cover-thumb {
  width: 100%; aspect-ratio: 1000 / 1600; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: 10px; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
}
.cover-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover-thumb img[hidden] { display: none; } /* let the hidden attribute win */
#cover-placeholder { color: var(--muted); font-size: 13px; text-align: center; line-height: 1.6; white-space: pre-line; }
.cover-panel #btn-cover { width: 100%; }
.cover-panel #cover-state { text-align: center; font-size: 12px; }
@media (max-width: 720px) {
  .book-layout { grid-template-columns: 1fr; }
  .cover-thumb { width: 160px; margin: 0 auto; }
}
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
.tab {
  background: transparent; border: 1px solid transparent; border-bottom: none;
  color: var(--muted); padding: 10px 18px; border-radius: 8px 8px 0 0;
}
.tab.active { color: var(--text); background: var(--panel); border-color: var(--line); }
.pane {
  background: var(--panel); border: 1px solid var(--line); border-top: none;
  border-radius: 0 0 10px 10px; height: 60vh; overflow: auto;
}
/* The markdown pane must NOT scroll itself — the textarea and tag list each
   scroll internally, otherwise you get a double scrollbar. */
#pane-md { overflow: hidden; }
.md-split { display: flex; height: 100%; }
.md-split #md-text { flex: 1; min-width: 0; }
#md-text {
  width: 100%; height: 100%; border: none; background: transparent; resize: none;
  padding: 18px; color: var(--text); font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 14px; line-height: 1.7;
}
.md-tags {
  width: 200px; flex: none; border-left: 1px solid var(--line);
  padding: 12px; overflow: auto; background: rgba(0,0,0,0.12);
}
.md-tags h3 { font-size: 12px; color: var(--muted); margin: 0 0 10px; font-weight: 700; }
.tag-btn {
  display: block; width: 100%; text-align: left; cursor: pointer;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px;
  padding: 8px 10px; margin-bottom: 7px; color: var(--text); font-size: 12.5px; font-weight: 600;
}
.tag-btn:hover { border-color: var(--brand); }
.tag-btn code {
  display: inline-block; min-width: 42px; margin-right: 8px; color: var(--brand);
  font-family: ui-monospace, Consolas, monospace; font-weight: 700;
}
.md-note { font-size: 11px; color: var(--muted); line-height: 1.6; margin: 10px 0 0; }
.field-note { font-size: 11px; color: var(--muted); line-height: 1.6; margin: -2px 0 10px; }
@media (max-width: 640px) { .md-split { flex-direction: column; } .md-tags { width: auto; border-left: none; border-top: 1px solid var(--line); } }
.preview {
  padding: 22px 28px; line-height: 1.9;
  font-family: "Shippori Mincho", "Yu Mincho", serif;
}
.preview h1 { font-size: 1.7em; margin: 0.2em 0 0.8em; }
.preview h2 { font-size: 1.3em; margin: 1.4em 0 0.6em; }
.preview p { margin: 0 0 0.9em; }
.preview img { max-width: 100%; height: auto; display: block; margin: 1em auto; border-radius: 6px; }
.preview a { color: var(--brand); }
.preview strong { font-weight: 700; }
.book-actions { display: flex; align-items: center; gap: 10px; margin-top: 14px; flex-wrap: wrap; }
.book-actions .grow { flex: 1; }

/* cover-source choice */
.choose-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin: 18px 0; }
.choice {
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  padding: 26px 16px; text-align: center; background: var(--panel-2);
  border: 1px solid var(--line); border-radius: var(--radius); color: var(--text);
  transition: border-color .15s, transform .15s;
}
.choice:hover { border-color: var(--brand); transform: translateY(-2px); }
.choice-emoji { font-size: 34px; }
.choice-title { font-size: 16px; font-weight: 700; }
.choice-desc { font-size: 12px; color: var(--muted); font-weight: 400; line-height: 1.5; }
@media (max-width: 560px) { .choose-grid { grid-template-columns: 1fr; } }

/* editor */
.editor-grid { display: grid; grid-template-columns: 64px 1fr 360px; gap: 18px; align-items: start; }
/* left overlay toolbar to add elements */
.editor-toolbar { display: flex; flex-direction: column; gap: 8px; position: sticky; top: 16px; }
.editor-toolbar button {
  width: 56px; height: 56px; padding: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; color: var(--text);
}
.editor-toolbar button:hover { border-color: var(--brand); }
.editor-toolbar button.danger:hover { border-color: var(--danger); }
.editor-toolbar .ti { font-size: 20px; line-height: 1; }
.editor-toolbar .lbl { font-size: 10px; color: var(--muted); }
.preview-pane { display: flex; flex-direction: column; align-items: center; }
.canvas-frame {
  /* Height-driven so the tall 1.6:1 canvas fits the available vertical space
     (above the footer + launcher) instead of overflowing off the bottom. */
  height: min(672px, calc(100vh - 330px)); aspect-ratio: 1600 / 2560;
  width: auto; max-width: 92%;
  border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
  background: #000;
}
/* Fabric wraps the canvas in a .canvas-container div sized to the backing store
   (1600×2560). Scale the WRAPPER down to the frame, not just the <canvas>. */
.canvas-frame .canvas-container { width: 100% !important; height: 100% !important; }
.canvas-frame canvas { width: 100% !important; height: 100% !important; }
/* Right column: scrollable settings card on top + a separate, always-visible
   action card pinned at the bottom. */
.editor-right {
  display: flex; flex-direction: column; gap: 12px;
  position: sticky; top: 16px; max-height: calc(100vh - 300px);
}
.control-pane {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 20px;
  flex: 1 1 auto; min-height: 0; overflow-y: auto;
}
.editor-actions {
  flex: none;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
}
.editor-actions button { flex: 1 1 0; }
.editor-actions .status { flex: 1 1 100%; margin: 2px 0 0; text-align: center; }

/* layer panel (stacking order, drag to reorder) */
#layer-list { list-style: none; margin: 6px 0 0; padding: 0; max-height: 190px; overflow-y: auto; }
.layer-item {
  display: flex; align-items: center; gap: 8px; padding: 7px 9px; margin-bottom: 5px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px;
  font-size: 12.5px; cursor: grab; user-select: none;
}
.layer-item.active { border-color: var(--brand); background: rgba(76, 158, 255, 0.14); }
.layer-item.dragging { opacity: 0.45; cursor: grabbing; }
.layer-item .layer-icon { width: 18px; text-align: center; color: var(--brand); flex: none; }
.layer-item .layer-grip { margin-left: auto; color: var(--muted); flex: none; }
.layer-item .layer-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.control-pane h2 { margin: 0 0 12px; font-size: 18px; }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; }

/* footer: persistent ad (left) + book actions (right) */
#app-footer {
  /* sits ABOVE the free-geares launcher dock, which fixes itself to the very
     bottom (~78px tall incl. offset). */
  position: fixed; left: 0; right: 0; bottom: 88px; z-index: 50;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  height: 76px; padding: 0 16px;
  background: var(--panel); border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
#app-footer.center-ad { justify-content: center; }
#sidebar-video-ad {
  position: relative; width: 200px; height: 60px; flex: none;
  background: #000; border: 1px solid var(--line); border-radius: 8px; overflow: hidden;
}
#sidebar-video-ad video { width: 100%; height: 100%; object-fit: cover; }
#sidebar-video-ad .ad-label {
  position: absolute; top: 5px; left: 5px; font-size: 10px; font-weight: 700;
  background: rgba(0,0,0,.6); padding: 2px 6px; border-radius: 4px; color: #cbd5e1;
}
#footer-actions { display: flex; align-items: stretch; gap: 10px; height: 60px; }
#footer-actions[hidden] { display: none; } /* let the hidden attribute win over display:flex */
#footer-actions button { height: 60px; padding: 0 22px; }
@media (max-width: 560px) {
  #app-footer { height: 64px; }
  #sidebar-video-ad { width: 120px; height: 48px; }
  #footer-actions { height: 48px; }
  #footer-actions button { height: 48px; padding: 0 12px; font-size: 13px; }
}

/* modal */
.modal {
  display: none; position: fixed; inset: 0; z-index: 60;
  background: rgba(6,9,13,.92); align-items: center; justify-content: center;
}
.modal.open { display: flex; }
.modal-inner { max-width: 560px; text-align: center; padding: 32px; }
.modal-inner h2 { margin: 12px 0 8px; }
.ad-slot {
  margin: 18px auto; width: 480px; max-width: 90vw; aspect-ratio: 16/9;
  background: #000; border: 1px solid var(--line); border-radius: 10px;
  display: flex; align-items: center; justify-content: center; color: var(--muted);
}
.progress { list-style: none; display: flex; gap: 20px; justify-content: center; padding: 0; }
.progress li { color: var(--muted); font-size: 13px; }
.progress li.done { color: var(--ok); }
.progress li.done::before { content: "✓ "; }
.spinner {
  width: 42px; height: 42px; margin: 0 auto; border-radius: 50%;
  border: 4px solid var(--line); border-top-color: var(--brand);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 860px) {
  .editor-grid { grid-template-columns: 56px 1fr; }
  .editor-grid .editor-right { grid-column: 1 / -1; }
  .canvas-frame { max-width: 84vw; }
  #sidebar-video-ad { width: 130px; height: 98px; }
}
