:root {
  --bar: #1e1f22;
  --text: #e6e7ea;
  --muted: #9a9ea6;
  --accent: #f5b301;
  --canvas: #2b2d31;
  --line: #34363c;
  --hover: #2c2e33;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--canvas);
  color: var(--text);
  font: 14px/1.3 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
  height: 100dvh;
  display: grid;
  grid-template-columns: 56px 1fr;
  grid-template-rows: 48px 1fr;
  grid-template-areas: "top top" "rail stage";
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; pointer-events: none; }
button {
  font: inherit; color: inherit; background: transparent; border: 0; border-radius: 8px;
  min-width: 44px; min-height: 44px; cursor: pointer; touch-action: manipulation;
}
button:hover { background: var(--hover); }
button:disabled { opacity: 0.35; cursor: default; background: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
button.on { color: var(--accent); background: #3a3320; }
.icon { display: inline-grid; place-items: center; padding: 0; width: 44px; height: 44px; }

/* ---------- top bar ---------- */
#topbar {
  grid-area: top; background: var(--bar); display: flex; align-items: center; gap: 2px;
  padding: 2px 6px; border-bottom: 1px solid var(--line); overflow-x: auto; overflow-y: hidden;
  scrollbar-width: none; white-space: nowrap; z-index: 5;
}
#topbar::-webkit-scrollbar { display: none; }
.brand { display: inline-flex; align-items: center; gap: 6px; padding: 0 8px 0 2px; flex: none; }
.brand-star { width: 26px; height: 26px; stroke: none; overflow: visible; }
.brand-star path { stroke: none; transform-box: fill-box; transform-origin: center; }
.brand-name {
  font-family: ui-rounded, "SF Pro Rounded", "Hiragino Maru Gothic ProN", "Arial Rounded MT Bold", "Nunito", system-ui, sans-serif;
  font-weight: 800; letter-spacing: 0.2px; color: #ffd76a;
}
.brand-sub { color: var(--muted); font-size: 12px; margin-left: 2px; }
@media (max-width: 900px) { .brand-sub { display: none; } }
/* one-time sparkle on load (transform / opacity only, no fill mode: nothing keeps running) */
.brand-star .bs-main { animation: bs-pop 0.9s ease-out 1; }
.brand-star .bs-sp1 { animation: bs-twinkle 0.8s ease-out 0.35s 1; }
.brand-star .bs-sp2 { animation: bs-twinkle 0.8s ease-out 0.45s 1; }
.brand-star.twinkle .bs-main { animation: bs-pop 0.6s ease-out 1; }
.brand-star.twinkle .bs-sp1, .brand-star.twinkle .bs-sp2 { animation: bs-twinkle 0.6s ease-out 1; }
@keyframes bs-pop { 0% { transform: scale(0.4) rotate(-40deg); opacity: 0; } 60% { transform: scale(1.15) rotate(8deg); opacity: 1; } 100% { transform: scale(1) rotate(0); opacity: 1; } }
@keyframes bs-twinkle { 0% { transform: scale(0); opacity: 0; } 50% { transform: scale(1.4); opacity: 1; } 100% { transform: scale(1); opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .brand-star path, .brand-star.twinkle path { animation: none !important; } }
.help-opts { display: flex; flex-wrap: wrap; gap: 6px 14px; padding: 2px 0 6px; }
.help-opts select { font: inherit; color: var(--text); background: #15161a; border: 1px solid var(--line); border-radius: 6px; height: 32px; margin-left: 4px; }
.keys kbd { font: 11px ui-monospace, Consolas, monospace; background: #15161a; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }
#title {
  flex: 0 1 220px; min-width: 90px; height: 36px; padding: 0 8px; font: inherit; color: var(--text);
  background: transparent; border: 1px solid transparent; border-radius: 6px; user-select: text;
}
#title:hover { border-color: var(--line); }
#title:focus { border-color: var(--accent); outline: none; background: #15161a; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 0 10px; flex: none; }
.chip svg { width: 20px; height: 20px; }
.spacer { flex: 1 1 auto; min-width: 8px; }
.save { color: var(--muted); font-size: 13px; }
.save.err { color: #e5484d; }
#topbar > button { flex: none; }

/* ---------- rail ---------- */
#rail {
  grid-area: rail; background: var(--bar); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 6px 0;
  overflow-y: auto; overflow-x: hidden; scrollbar-width: none; z-index: 4;
}
#rail::-webkit-scrollbar { display: none; }
#rail .group { display: flex; flex-direction: column; align-items: center; gap: 2px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
#rail .group:last-child { border-bottom: 0; }
#rail .group[hidden] { display: none; }
.selmode { display: flex; flex-direction: column; gap: 0; background: #15161a; border-radius: 8px; }
.selmode[hidden] { display: none; }
.selmode svg { width: 20px; height: 20px; }
.swatch { width: 44px; height: 36px; min-height: 36px; display: grid; place-items: center; padding: 0; }
.swatch::before { content: ""; width: 22px; height: 22px; border-radius: 50%; background: var(--c); box-shadow: 0 0 0 1px #ffffff33; }
.swatch.on::before { box-shadow: 0 0 0 2px var(--bar), 0 0 0 4px var(--accent); }
.swatch.on { background: transparent; }
.custom { width: 44px; height: 44px; display: grid; place-items: center; cursor: pointer; }
.custom input { width: 28px; height: 28px; padding: 0; border: 0; background: none; cursor: pointer; }
.size { width: 44px; height: 36px; min-height: 36px; display: grid; place-items: center; padding: 0; }
.size::before { content: ""; width: var(--d); height: var(--d); border-radius: 50%; background: var(--text); }
.size.on::before { background: var(--accent); }
.slider-wrap { padding: 6px 0; }
#size-slider { writing-mode: vertical-lr; direction: rtl; width: 32px; height: 140px; accent-color: var(--accent); }

/* ---------- stage ---------- */
#stage { grid-area: stage; position: relative; overflow: hidden; background: var(--canvas); min-width: 0; min-height: 0; }
#stage canvas { position: absolute; left: 0; top: 0; display: block; touch-action: none; }
#live { cursor: crosshair; }
#stage[data-tool="hand"] #live { cursor: grab; }
#stage[data-tool="select"] #live { cursor: default; }
#stage[data-tool="text"] #live, #stage[data-tool="note"] #live { cursor: text; }
#stage[data-tool="eraser"] #live { cursor: none; }
#editor {
  position: absolute; margin: 0; border: 1px dashed var(--accent); border-radius: 2px; resize: none; overflow: hidden;
  outline: none; z-index: 3; user-select: text; white-space: pre; min-width: 24px; min-height: 24px;
}
#editor[hidden] { display: none; }
#selbar {
  position: absolute; left: 0; top: 0; z-index: 4; display: flex; gap: 2px; padding: 2px;
  background: var(--bar); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 4px 16px #0006;
}
#selbar[hidden], #selbar button[hidden] { display: none; }
.size-stepper { display: inline-flex; align-items: center; gap: 0; background: var(--bar); border: 1px solid var(--line); border-radius: 10px; height: 44px; }
.size-stepper[hidden], #selbar .size-stepper[hidden] { display: none; }
.size-stepper button { width: 40px; min-width: 40px; height: 42px; min-height: 42px; font-size: 20px; font-weight: 600; padding: 0; }
.size-stepper .sz-label { min-width: 52px; text-align: center; font-size: 13px; font-variant-numeric: tabular-nums; }
#editor-size { position: absolute; z-index: 4; }
.sb-count { align-self: center; padding: 0 8px; font-size: 13px; color: var(--muted); white-space: nowrap; }
#hint {
  position: absolute; left: 50%; top: 16px; transform: translateX(-50%); z-index: 2; pointer-events: none;
  background: #1e1f22e6; padding: 8px 14px; border-radius: 8px; color: var(--text); max-width: calc(100% - 32px); text-align: center;
}
#hint[hidden] { display: none; }

/* ---------- menus ---------- */
.menu {
  position: fixed; z-index: 20; min-width: 220px; background: var(--bar); border: 1px solid var(--line);
  border-radius: 10px; padding: 6px; box-shadow: 0 8px 28px #0008; display: flex; flex-direction: column;
}
.menu[hidden] { display: none; }
.menu button { text-align: left; padding: 0 12px; width: 100%; }
.menu-title { color: var(--muted); padding: 6px 12px; font-size: 13px; }
.settings { min-width: 280px; gap: 2px; }
.settings .row { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 10px; border-radius: 8px; cursor: pointer; }
.settings .row:hover { background: var(--hover); }
.settings .row.col { justify-content: space-between; }
.settings input[type="checkbox"] { width: 20px; height: 20px; accent-color: var(--accent); }
.settings select { font: inherit; color: var(--text); background: #15161a; border: 1px solid var(--line); border-radius: 6px; height: 36px; max-width: 190px; }

/* ---------- toasts ---------- */
#toasts {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 30;
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; max-width: calc(100% - 32px);
}
.toast {
  pointer-events: auto; display: flex; align-items: center; gap: 8px; background: #111214; color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 0 6px 0 14px; min-height: 48px; box-shadow: 0 6px 22px #0008;
}
.toast .sep { color: var(--muted); }
.toast button { color: var(--accent); font-weight: 600; padding: 0 10px; }
#command-bar[hidden] { display: none; }

/* ---------- narrow screens: rail becomes a bottom bar ---------- */
@media (max-width: 700px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: 48px 1fr auto;
    grid-template-areas: "top" "stage" "rail";
  }
  .brand-name, .brand-sub { display: none; }
  #rail {
    flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-top: 1px solid var(--line);
    padding: 0 6px; height: 56px; align-items: center; justify-content: flex-start;
  }
  #rail .group { flex-direction: row; border-bottom: 0; border-right: 1px solid var(--line); padding: 0 6px 0 0; flex: none; }
  .selmode { flex-direction: row; }
  .swatch, .size { width: 36px; height: 44px; }
  #size-slider { writing-mode: horizontal-tb; direction: ltr; width: 120px; height: 32px; }
  #toasts { bottom: 72px; }
}

/* ---------- text direction toggle ---------- */
.dir-toggle { font: 600 18px/1 "Yu Gothic", "Hiragino Sans", "Noto Sans JP", sans-serif; }
#editor-dir { position: absolute; z-index: 4; background: var(--bar); border: 1px solid var(--line); color: var(--accent); }
#editor-dir[hidden] { display: none; }
#selbar .text-btn { padding: 0 12px; white-space: nowrap; }

/* ---------- layers panel ---------- */
.layers { grid-area: layers; background: var(--bar); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; z-index: 5; }
.layers-head { display: flex; align-items: center; gap: 8px; width: 100%; padding: 0 12px; justify-content: flex-start; font-weight: 600; border-radius: 0; border-bottom: 1px solid var(--line); }
.layers-head svg { width: 20px; height: 20px; }
.layers-body { display: flex; flex-direction: column; min-height: 0; flex: 1 1 auto; }
.layers-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 0 1 auto; }
.layer-row { display: flex; align-items: center; height: 44px; border-left: 3px solid transparent; cursor: pointer; }
.layer-row:hover { background: var(--hover); }
.layer-row.active { border-left-color: var(--accent); background: #2a2a24; }
.layer-row .icon { width: 44px; min-width: 44px; height: 44px; flex: none; color: var(--muted); }
.layer-row .icon[aria-pressed="true"] { color: var(--text); }
.layer-row .lk[aria-pressed="true"] { color: var(--accent); }
.layer-row .icon svg { width: 18px; height: 18px; }
.layer-name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 4px; }
.layer-rename { flex: 1 1 auto; min-width: 0; height: 32px; font: inherit; color: var(--text); background: #15161a; border: 1px solid var(--accent); border-radius: 6px; padding: 0 6px; user-select: text; }
.layers-foot { border-top: 1px solid var(--line); padding: 6px 8px; display: flex; flex-direction: column; gap: 4px; }
.layers-opacity { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); min-height: 44px; }
.layers-opacity input { flex: 1 1 auto; min-width: 0; accent-color: var(--accent); }
.layers-opacity span { width: 38px; text-align: right; color: var(--text); }
.layers-btns { display: flex; justify-content: space-between; }
body.layers-open { grid-template-columns: 56px 1fr 208px; grid-template-areas: "top top top" "rail stage layers"; }
body:not(.layers-open) .layers { display: none; } /* hidden: takes no space, the canvas gets the column back */
.menu.recent { max-width: min(560px, calc(100vw - 16px)); }
.menu.recent button { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- tool options popover ---------- */
#rail .has-opts { position: relative; }
#rail .has-opts::after { content: ""; position: absolute; right: 4px; bottom: 4px; border: 4px solid transparent; border-right-color: var(--muted); border-bottom-color: var(--muted); }
.tool-pop {
  position: fixed; z-index: 25; width: 280px; background: var(--bar); border: 1px solid var(--line); border-radius: 10px;
  padding: 4px 8px 8px; box-shadow: 0 8px 28px #0008; display: flex; flex-direction: column; gap: 4px;
}
.tool-pop[hidden] { display: none; }
.pop-label { font-size: 13px; color: var(--muted); padding: 2px 2px 0; }
.seg { display: flex; background: #15161a; border-radius: 8px; padding: 2px; }
.seg button { flex: 1 1 0; min-width: 0; padding: 0 6px; font-size: 13px; border-radius: 6px; }
.seg button.on { background: var(--accent); color: #1e1f22; font-weight: 600; }
.pop-row { display: grid; grid-template-columns: 1fr; gap: 2px; font-size: 13px; color: var(--muted); }
.pop-row input[type="range"] { width: 100%; accent-color: var(--accent); }
.pop-row:not(.check) { grid-template-columns: 1fr 64px; align-items: center; }
.pop-row:not(.check) span { grid-column: 1 / -1; }
.pop-row input[type="number"] { width: 64px; height: 32px; font: inherit; color: var(--text); background: #15161a; border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; }
.pop-row.check { display: flex; align-items: center; gap: 8px; min-height: 44px; color: var(--text); }
.pop-row.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.pop-btn { align-self: flex-start; border: 1px solid var(--line); }

/* ---------- page buttons ---------- */
.page-tools {
  position: absolute; left: 0; top: 0; z-index: 5; display: flex; align-items: center; gap: 2px; height: 36px; padding: 0 4px 0 12px;
  background: #1e1f22f0; border: 1px solid var(--line); border-radius: 18px; box-shadow: 0 4px 16px #0006; white-space: nowrap;
}
.page-tools[hidden] { display: none; }
.pt-label { color: var(--muted); font-size: 13px; padding-right: 6px; }
.page-tools button { display: inline-flex; align-items: center; gap: 6px; height: 34px; min-height: 34px; min-width: 0; padding: 0 10px; border-radius: 17px; font-size: 13px; }
.page-tools button[hidden] { display: none; }
.page-tools svg { width: 18px; height: 18px; }

/* ---------- round 2: AI command bar ---------- */
#command-bar {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%); z-index: 6;
  width: min(640px, calc(100% - 32px)); display: flex; flex-direction: column; gap: 6px; pointer-events: none;
}
#command-bar > * { pointer-events: auto; }
#command-bar [hidden] { display: none !important; }
.ai-bar {
  height: 56px; display: flex; align-items: center; gap: 2px; padding: 0 6px;
  background: var(--bar); border: 1.5px solid var(--accent); border-radius: 12px; box-shadow: 0 6px 24px #0007;
}
.ai-tag { display: inline-flex; align-items: center; gap: 4px; color: var(--accent); font-weight: 700; padding: 0 6px 0 4px; flex: none; }
.ai-tag svg { width: 20px; height: 20px; fill: var(--accent); stroke: var(--accent); }
#ai-input {
  flex: 1 1 auto; min-width: 0; height: 40px; padding: 0 6px; font: 15px/1.2 system-ui, sans-serif; color: var(--text);
  background: transparent; border: 0; outline: none; user-select: text;
}
#ai-input::placeholder { color: var(--muted); }
#ai-input:disabled { opacity: 0.5; }
.ai-thinking { flex: 1 1 auto; color: var(--muted); padding: 0 6px; }
.ai-bar .icon { flex: none; font-size: 18px; font-weight: 700; }
.ai-bar .cancel, .ai-preview .cancel { padding: 0 14px; flex: none; }
.mic.rec { color: #e5484d; background: #3a2020; }
.ai-row { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.ai-row::-webkit-scrollbar { display: none; }
.chip-btn {
  flex: none; white-space: nowrap; font-size: 13px; padding: 0 14px; border-radius: 22px;
  background: var(--bar); border: 1px solid var(--line); box-shadow: 0 2px 10px #0005;
}
.chip-btn:hover { border-color: var(--accent); background: var(--bar); }
.ai-preview { flex-direction: column; gap: 0; background: var(--bar); border: 1px solid var(--line); border-radius: 12px; padding: 2px 6px; box-shadow: 0 4px 18px #0006; overflow: visible; }
.pv-line { display: flex; align-items: center; gap: 2px; }
.pv-label { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding: 0 6px; }
.ai-preview .keep { background: var(--accent); color: #1e1f22; font-weight: 700; padding: 0 18px; flex: none; }
.ai-preview .keep:hover { background: #ffc933; }
.pv-warn { color: #f59e0b; font-size: 13px; padding: 0 6px 6px; }
.ai-preview-ai { max-height: 40vh; overflow-y: auto; }
.pv-text { padding: 6px 6px 2px; }
.pv-title { font-weight: 600; white-space: normal; overflow-wrap: anywhere; }
.pv-note { color: var(--muted); font-size: 13px; margin-top: 2px; white-space: normal; overflow-wrap: anywhere; }
.pv-buttons { justify-content: flex-end; flex-wrap: nowrap; }
.text-btn { padding: 0 10px; white-space: nowrap; flex: none; }
.pv-tokens { color: var(--muted); font-size: 12px; margin-top: 2px; }
.link-btn { min-width: 0; min-height: 0; padding: 0 2px; color: var(--accent); text-decoration: underline; font-size: 12px; }
.pv-hint { color: var(--muted); font-size: 13px; padding: 0 6px 6px; }
.pv-rejected { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; padding: 0 6px; }
.ai-card, .ai-details {
  background: var(--bar); border: 1px solid var(--accent); border-radius: 12px; padding: 2px 6px 8px;
  box-shadow: 0 6px 24px #0008; overflow: auto; user-select: text;
}
.ai-card { max-height: 40vh; }
.ai-details { max-height: 50vh; border-color: var(--line); }
.card-head { display: flex; align-items: center; gap: 2px; position: sticky; top: 0; background: var(--bar); }
.card-title { flex: 1 1 auto; font-weight: 700; color: var(--accent); padding: 0 6px; }
.card-body { padding: 0 6px; white-space: pre-wrap; overflow-wrap: anywhere; }
.card-body div + div { margin-top: 6px; }
.ai-details pre { margin: 0; padding: 0 6px; font: 12px/1.4 ui-monospace, Consolas, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.ai-help {
  background: var(--bar); border: 1px solid var(--line); border-radius: 12px; padding: 4px 10px 8px; font-size: 13px;
  max-height: 50vh; overflow: auto; box-shadow: 0 8px 28px #0008; user-select: text;
}
.ai-help ul { margin: 0; padding-left: 18px; }
.ai-help li { margin: 3px 0; }
.ai-help code { background: #15161a; padding: 1px 4px; border-radius: 4px; }
#editor-mic { position: absolute; z-index: 4; background: var(--bar); border: 1px solid var(--line); }
#editor-mic[hidden] { display: none; }
.settings { max-height: calc(100vh - 72px); overflow-y: auto; }
.ai-settings { border-top: 1px solid var(--line); margin-top: 4px; padding-top: 2px; display: flex; flex-direction: column; gap: 2px; }
.ai-note { color: var(--muted); font-size: 12px; padding: 0 10px 4px; }
.ai-note a { color: var(--accent); }
.settings input[type="number"] { width: 80px; }
.settings input[type="password"], .settings input[type="text"], .settings input[type="number"] {
  font: inherit; color: var(--text); background: #15161a; border: 1px solid var(--line); border-radius: 6px;
  height: 36px; width: 170px; padding: 0 8px; user-select: text;
}
#toasts { bottom: 150px; }
@media (max-width: 700px) {
  body.layers-open { grid-template-columns: 1fr; grid-template-areas: "top" "stage" "rail"; }
  body.layers-open .layers { position: fixed; top: 48px; right: 0; bottom: 56px; width: 208px; border-left: 1px solid var(--line); }
  #command-bar { left: 0; right: 0; bottom: 4px; transform: none; width: auto; padding: 0 4px; }
  #toasts { bottom: 200px; }
}

/* ---------- polish round: bottom area fits the canvas ---------- */
#toasts { position: absolute; z-index: 30; }
/* the column above the AI bar never leaves the canvas; the AI bar itself never shrinks */
#command-bar { gap: 8px; max-height: calc(100% - 24px); }
#command-bar > * { flex-shrink: 0; min-height: 0; }
#command-bar > .ai-bar { flex: none; }
/* preview: the text scrolls, the buttons always stay visible above the AI bar */
#command-bar > .ai-preview-ai { flex-shrink: 1; display: flex; flex-direction: column; max-height: 40vh; overflow: hidden; }
.ai-preview-ai .pv-text { flex: 1 1 auto; min-height: 0; overflow-y: auto; }
.ai-preview-ai .pv-buttons, .ai-preview-ai .pv-rejected, .ai-preview-ai .pv-warn { flex: none; }
#command-bar > .ai-card, #command-bar > .ai-details, #command-bar > .ai-help { flex-shrink: 1; }
/* quick chips: wrap onto at most 2 lines; beyond that, 2 rows that scroll sideways with a fade */
#ai-chips { flex-wrap: wrap; overflow: hidden; }
#ai-chips.scroll {
  display: grid; grid-auto-flow: column; grid-template-rows: repeat(2, auto); grid-auto-columns: max-content;
  overflow-x: auto; overflow-y: hidden; mask-image: linear-gradient(to right, #000 85%, transparent);
  -webkit-mask-image: linear-gradient(to right, #000 85%, transparent);
}

/* MangaCoin link at the far left of the header (the app is hosted on the MangaCoin site) */
.mc-link { flex: none; display: inline-grid; place-items: center; min-width: 44px; height: 44px; border-radius: 8px; }
.mc-link:hover { background: var(--hover); }
.mc-link img { width: auto; height: 32px; display: block; }
@media (max-width: 700px) { #brand { display: none; } }
