:root {
  color-scheme: dark;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  --page: #141311;
  --surface: #1d1b19;
  --surface-raised: #26231f;
  --surface-hover: #302c27;
  --text: #f5f3ef;
  --muted: #a09d99;
  --subtle: #777570;
  --border: #39352f;
  --accent: #ff825c;
  --accent-hover: #ff9a79;
  --accent-ink: #24120e;
  --focus: #ff9a79;
  --shadow: 0 22px 60px #0005;
  --radius: 18px;
}

:root[data-theme="light"] {
  color-scheme: light;
  --page: #f3f0eb;
  --surface: #fcfaf6;
  --surface-raised: #eeebe5;
  --surface-hover: #e7e2da;
  --text: #211f1c;
  --muted: #69655f;
  --subtle: #827d76;
  --border: #e1dbd1;
  --accent: #df5736;
  --accent-hover: #c94729;
  --accent-ink: #fff;
  --focus: #c94729;
  --shadow: 0 22px 60px #40321c12;
}

* { box-sizing: border-box; }
html { min-width: 320px; background: var(--page); }
body { min-height: 100vh; margin: 0; background: var(--page); color: var(--text); }
button, input, select { font: inherit; }
button, a, input, select { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
a { color: inherit; }
::selection { background: color-mix(in srgb, var(--accent) 35%, transparent); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 3px; }

.site-header {
  position: sticky;
  z-index: 5;
  top: 0;
  display: flex;
  min-height: 72px;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0 5vw;
  border-bottom: 1px solid color-mix(in srgb, var(--border) 78%, transparent);
  background: color-mix(in srgb, var(--page) 84%, transparent);
  backdrop-filter: blur(18px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: .65rem;
  color: var(--text);
  font-size: 1rem;
  font-weight: 760;
  letter-spacing: -.035em;
  text-decoration: none;
}

.brand-mark {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 1px solid color-mix(in srgb, var(--accent) 45%, var(--border));
  border-radius: 12px;
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
  font-size: 1rem;
}

.header-actions, .page-actions { display: flex; align-items: center; gap: .65rem; }
.button {
  display: inline-flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  padding: .65rem 1rem;
  border: 1px solid transparent;
  border-radius: 12px;
  background: var(--surface-raised);
  color: var(--text);
  font-size: .9rem;
  font-weight: 650;
  text-decoration: none;
  cursor: pointer;
  transition: background .18s ease, border-color .18s ease, transform .18s ease;
}

.button:hover:not(:disabled) { transform: translateY(-1px); background: var(--surface-hover); }
.button:disabled { cursor: not-allowed; opacity: .48; }
.button-primary { background: var(--accent); color: var(--accent-ink); }
.button-primary:hover:not(:disabled) { background: var(--accent-hover); }
.button-quiet { border-color: var(--border); background: transparent; }
.button-quiet:hover:not(:disabled) { border-color: var(--muted); background: var(--surface-raised); }
.button-small { min-height: 34px; padding: .4rem .7rem; border-radius: 10px; font-size: .8rem; }
.settings { position: relative; }
.settings-toggle { min-height: 40px; min-width: 40px; padding: .5rem; }
.settings-panel {
  position: absolute; right: 0; top: calc(100% + .5rem); z-index: 50;
  display: grid; gap: .25rem; width: max-content; min-width: 240px; max-width: calc(100vw - 1.5rem); padding: .5rem;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: 0 16px 40px #00000033;
}
.settings-panel[hidden] { display: none; }
.settings-row { padding: .6rem .65rem; border-radius: 10px; color: var(--text); }
.settings-row:hover { background: var(--surface-raised); }
.settings-text { display: grid; gap: .1rem; font-size: .88rem; }
.settings-text small { color: var(--muted); font-size: .72rem; font-weight: 500; }
.theme-icon { font-size: 1rem; }

.home-hero {
  position: relative;
  display: grid;
  min-height: 390px;
  overflow: hidden;
  align-content: center;
  justify-items: center;
  padding: 5rem 1.5rem 4.5rem;
  text-align: center;
  isolation: isolate;
}

.home-hero::before {
  position: absolute;
  z-index: -1;
  top: -200px;
  left: 50%;
  width: min(820px, 100vw);
  height: 560px;
  border-radius: 50%;
  background: radial-gradient(ellipse, color-mix(in srgb, var(--accent) 16%, transparent), transparent 68%);
  content: "";
  transform: translateX(-50%);
}

.eyebrow {
  margin: 0 0 .9rem;
  color: var(--accent);
  font-size: .74rem;
  font-weight: 760;
  letter-spacing: .14em;
  text-transform: uppercase;
}

.home-hero h1 {
  max-width: 780px;
  margin: 0;
  font-size: clamp(2.7rem, 6.5vw, 5.4rem);
  font-weight: 760;
  letter-spacing: -.075em;
  line-height: .99;
}

.accent-text { color: var(--accent); }
.home-hero p {
  max-width: 520px;
  margin: 1.25rem 0 1.7rem;
  color: var(--muted);
  font-size: clamp(1rem, 1.6vw, 1.12rem);
  line-height: 1.65;
}

.gallery { width: min(1320px, 100%); margin: 0 auto; padding: 0 5vw 5rem; }
.section-heading { display: flex; align-items: end; justify-content: space-between; gap: 1rem; margin: 0 0 1.25rem; }
.section-heading h2 { margin: 0; font-size: 1.2rem; letter-spacing: -.035em; }
.section-heading p { margin: .35rem 0 0; color: var(--muted); font-size: .88rem; }

#grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 245px), 1fr));
  gap: 1rem;
}

.card {
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  text-decoration: none;
  transition: transform .2s ease, border-color .2s ease, box-shadow .2s ease;
}

.card:hover { transform: translateY(-3px); border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); box-shadow: var(--shadow); }
.card img { display: block; width: 100%; max-height: 360px; min-height: 130px; background: var(--surface-raised); object-fit: cover; }
.card .meta { padding: 1rem 1.05rem 1.1rem; }
.card .caption { display: -webkit-box; overflow: hidden; font-size: .92rem; line-height: 1.5; white-space: pre-line; -webkit-box-orient: vertical; -webkit-line-clamp: 4; }
.card .time { margin-top: .7rem; color: var(--subtle); font-size: .75rem; }

#empty {
  display: none;
  padding: 3.5rem 1rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  text-align: center;
}

.editor-layout {
  display: grid;
  width: min(1380px, 100%);
  min-height: 100vh;
  grid-template-columns: minmax(0, 1fr) 260px;
  gap: clamp(1.5rem, 3.5vw, 3.5rem);
  margin: 0 auto;
  padding: 1.8rem clamp(1.25rem, 4vw, 3.5rem) 5rem;
  background:
    radial-gradient(ellipse at 42% 0, color-mix(in srgb, var(--accent) 8%, transparent), transparent 48%);
}

#historySidebar {
  position: sticky;
  top: 6.5rem;
  grid-column: 2;
  grid-row: 1;
  align-self: start;
  max-height: calc(100vh - 8rem);
  overflow: auto;
  padding: 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.historyHeader { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .9rem; padding-bottom: .8rem; border-bottom: 1px solid var(--border); }
.historyHeader h2 { margin: 0; font-size: .95rem; letter-spacing: -.02em; }
#historyList { display: flex; flex-direction: column; gap: .35rem; }
.historyEmpty { padding: 1rem .25rem; color: var(--muted); font-size: .84rem; }
.historyItem { display: flex; min-width: 0; align-items: center; gap: .7rem; padding: .55rem; border: 1px solid transparent; border-radius: 12px; cursor: pointer; transition: background .16s ease, border-color .16s ease; }
.historyItem:hover { border-color: var(--border); background: var(--surface-raised); }
.historyItem img { width: 54px; height: 54px; flex: 0 0 auto; border: 1px solid var(--border); border-radius: 9px; background: var(--surface-raised); object-fit: cover; }
.historyMeta { min-width: 0; }
.historyCaption { display: -webkit-box; overflow: hidden; font-size: .78rem; line-height: 1.4; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.historyTime { margin-top: .25rem; color: var(--subtle); font-size: .68rem; }

.editor-main { width: min(100%, 940px); min-width: 0; grid-column: 1; grid-row: 1; margin: 0 auto; }
.editor-title { position: relative; isolation: isolate; margin: 0 0 1.6rem; padding: 1.4rem 1rem 1.2rem; text-align: center; }
.editor-title::before { position: absolute; z-index: -1; top: -7rem; left: 50%; width: min(600px, 90%); height: 260px; border-radius: 50%; background: radial-gradient(ellipse, color-mix(in srgb, var(--accent) 12%, transparent), transparent 70%); content: ""; transform: translateX(-50%); }
.editor-title .eyebrow { margin-bottom: .65rem; }
.editor-title h1 { margin: 0; font-size: clamp(2.15rem, 4vw, 3.25rem); font-weight: 760; letter-spacing: -.075em; line-height: 1; }
.editor-title p:last-child { margin: .8rem 0 0; color: var(--muted); font-size: .95rem; line-height: 1.55; }

.panel { margin-bottom: 1rem; padding: clamp(1rem, 2.4vw, 1.4rem); border: 1px solid var(--border); border-radius: var(--radius); background: color-mix(in srgb, var(--surface) 90%, transparent); box-shadow: 0 12px 36px #0000000a; }
.panel-heading { display: flex; align-items: center; gap: .65rem; margin: 0 0 1rem; font-size: .88rem; font-weight: 700; letter-spacing: -.01em; }
.step-number { display: grid; width: 25px; height: 25px; place-items: center; border: 1px solid color-mix(in srgb, var(--accent) 36%, var(--border)); border-radius: 8px; background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--accent); font-size: .72rem; }
.controls { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: .8rem; align-items: end; }
.controls .field:first-child { grid-column: 1 / -1; }
.controls .button { grid-column: 1 / -1; }
.field { display: grid; min-width: 0; gap: .45rem; color: var(--muted); font-size: .77rem; font-weight: 650; }
.field input:not([type="color"]), .field select, .field textarea, .control-select, .control-number {
  width: 100%;
  min-height: 44px;
  padding: .65rem .75rem;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--page);
  color: var(--text);
  font-size: .87rem;
}
.field textarea { resize: vertical; font-family: inherit; line-height: 1.4; }
.field[hidden], .button[hidden] { display: none; }
.field input::file-selector-button { margin: -.35rem .55rem -.35rem -.4rem; padding: .45rem .6rem; border: 0; border-radius: 7px; background: var(--surface-raised); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; }
.field input[type="file"] { padding: .4rem; }
.field input[type="file"]::file-selector-button { min-height: 31px; }
.controls .button { min-height: 44px; white-space: nowrap; }
.row { display: flex; flex-wrap: wrap; align-items: end; gap: .9rem; }
.row .field { width: auto; min-width: 120px; }
.row .field-position { flex: 1 1 190px; }
.control-number { width: 86px !important; }
.color-control { display: flex; height: 44px; align-items: center; gap: .55rem; padding: .35rem .65rem; border: 1px solid var(--border); border-radius: 11px; background: var(--page); }
.color-control input { width: 29px; height: 29px; padding: 0; border: 0; border-radius: 8px; background: transparent; cursor: pointer; }
.color-control input::-webkit-color-swatch-wrapper { padding: 0; }
.color-control input::-webkit-color-swatch { border: 1px solid var(--border); border-radius: 6px; }
.tool-divider { width: 1px; height: 38px; align-self: end; background: var(--border); }
.export-actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: end; }

.preview-heading { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin: 1.5rem .15rem .75rem; }
.preview-heading h2 { margin: 0; font-size: 1rem; letter-spacing: -.025em; }
.preview-heading p { margin: .3rem 0 0; color: var(--muted); font-size: .8rem; }
.preview-indicator { display: inline-flex; align-items: center; gap: .45rem; padding: .4rem .65rem; border: 1px solid var(--border); border-radius: 999px; color: var(--muted); font-size: .7rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; }
.preview-indicator span { color: #69c58a; font-size: .6rem; }

#status, #gifProgress {
  margin: .5rem 0;
  color: var(--muted);
  font-size: .84rem;
  line-height: 1.5;
}
#status:empty, #gifProgress:empty { display: none; }

#canvasWrap {
  position: relative;
  display: grid;
  min-height: 430px;
  overflow: auto;
  place-items: center;
  padding: clamp(1rem, 3vw, 2.25rem);
  border: 1px solid var(--border);
  border-radius: 22px;
  background:
    radial-gradient(ellipse at 50% 0, color-mix(in srgb, var(--accent) 9%, transparent), transparent 60%),
    var(--surface);
  box-shadow: inset 0 1px #ffffff08, var(--shadow);
}

canvas { display: block; max-width: 100%; height: auto; border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 18px 48px #0004; }
#canvasWrap.is-empty canvas { display: none; }
.canvas-empty { display: none; max-width: 360px; padding: 2rem; text-align: center; }
#canvasWrap.is-empty .canvas-empty { display: block; }
.canvas-empty-icon { display: grid; width: 58px; height: 58px; margin: 0 auto 1.15rem; place-items: center; border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border)); border-radius: 18px; background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--accent); font-size: 1.5rem; }
.canvas-empty h2 { margin: 0; font-size: 1rem; letter-spacing: -.025em; }
.canvas-empty p { margin: .5rem 0 0; color: var(--muted); font-size: .84rem; line-height: 1.55; }

@media (max-width: 980px) {
  .editor-layout { grid-template-columns: minmax(0, 1fr) 220px; gap: 1.25rem; }
  #canvasWrap { min-height: 360px; }
  .tool-divider { display: none; }
}

@media (max-width: 700px) {
  .site-header { min-height: 64px; padding: 0 1rem; }
  .brand { font-size: .92rem; }
  .brand-mark { width: 31px; height: 31px; }
  .header-actions { gap: .45rem; }
  .header-actions .button { min-height: 38px; padding: .5rem .7rem; font-size: .8rem; }
  .home-hero { min-height: 340px; padding: 4rem 1.25rem 3.5rem; }
  .gallery { padding: 0 1rem 3rem; }
  .editor-layout { display: flex; flex-direction: column; padding: 1.25rem 1rem 3rem; }
  #historySidebar { position: static; width: 100%; max-height: none; order: 2; }
  #historyList { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
  .editor-main { width: 100%; order: 1; }
  #canvasWrap { min-height: 300px; }
}

@media (max-width: 480px) {
  .theme-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; }
  .header-actions .button-primary { min-width: 42px; padding: .5rem .7rem; font-size: .8rem; }
  .home-hero h1 { font-size: 2.65rem; }
  .section-heading { align-items: start; flex-direction: column; }
  .controls { grid-template-columns: 1fr; }
  .controls .field:first-child, .controls .button { grid-column: auto; }
  .controls .button { width: 100%; }
  .row { align-items: stretch; }
  .row .field { flex: 1 1 125px; }
  .export-actions { width: 100%; }
  .export-actions .button { flex: 1; }
}

@media (max-width: 380px) {
  .page-actions .button-small { padding: .4rem .55rem; font-size: .72rem; }
  .site-header { padding: 0 .65rem; }
  .brand { gap: .4rem; font-size: .85rem; }
  .header-actions { gap: .35rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}

/* Shareable view page (/v/<id>) */
.view-page { display: flex; justify-content: center; padding: clamp(1.25rem, 4vw, 3.5rem) 1rem 4rem; }
.view-card {
  width: min(100%, 760px);
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 6px);
  background: var(--surface);
  box-shadow: var(--shadow);
}
.view-figure { margin: 0; background: var(--surface-raised); }
.view-figure img { display: block; width: 100%; max-height: 72vh; object-fit: contain; }
.view-body { padding: clamp(1.25rem, 4vw, 2rem); text-align: center; }
.view-caption { margin: 0 0 .6rem; font-size: clamp(1.2rem, 3.2vw, 1.7rem); font-weight: 700; line-height: 1.3; text-wrap: balance; }
.view-date { margin: 0 0 1.5rem; color: var(--subtle); font-size: .85rem; }
.view-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: .65rem; }
.view-missing { max-width: 460px; margin-top: 12vh; text-align: center; }
.view-missing h1 { margin: 0 0 .6rem; font-size: 1.8rem; }
.view-missing p { color: var(--muted); }

.view-figure { display: flex; justify-content: center; }
.view-frame { position: relative; display: inline-block; max-width: 100%; line-height: 0; }
.view-frame img { width: auto; max-width: 100%; }
.watermark {
  position: absolute;
  /* Size/offset are set by view.js to match the editor canvas watermark. */
  right: .9rem;
  bottom: .8rem;
  color: #fff;
  font-family: 'Montserrat', sans-serif;
  font-size: .85rem;
  font-weight: 700;
  line-height: 1;
  opacity: .85;
  text-shadow: 0 0 6px #000b;
  pointer-events: none;
  user-select: none;
}

.chips { display: flex; flex-wrap: wrap; justify-content: center; gap: .4rem; margin: 0 0 1rem; padding: 0; list-style: none; }
.chip {
  padding: .25rem .65rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-raised);
  color: var(--muted);
  font-size: .78rem;
  font-weight: 600;
}
.chip-warn { border-color: var(--accent); color: var(--accent); }

.card { position: relative; }
.nsfw-badge {
  position: absolute;
  top: .6rem;
  left: .6rem;
  padding: .2rem .5rem;
  border-radius: 999px;
  background: #000a;
  color: #fff;
  font-size: .68rem;
  font-weight: 760;
  letter-spacing: .08em;
}
.switch { display: inline-flex; align-items: center; gap: .6rem; color: var(--muted); font-size: .88rem; font-weight: 600; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch-track {
  position: relative;
  width: 40px;
  height: 22px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-raised);
  transition: background .2s, border-color .2s;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform .2s, background .2s;
}
.switch input:checked + .switch-track { border-color: var(--accent); background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); background: var(--accent-ink); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--focus); outline-offset: 2px; }
