:root {
  color-scheme: light;
  --paper: #f4f1ea;
  --card: #fffefa;
  --surface: #ffffff;
  --ink: #1c1b19;
  --muted: #6b665d;
  --line: #e4dfd4;
  --line-strong: #cec7b8;
  --vermilion: #b83b2a;
  --vermilion-soft: #fdf2ef;
  --cta: #1c1b19;
  --cta-hover: #35322e;
  --danger: #b42318;
  --danger-bg: #fef3f2;
  font: 16px/1.5 system-ui, -apple-system, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--paper);
  background-image:
    radial-gradient(900px 420px at 15% 0%, rgba(184, 59, 42, 0.04), transparent 65%),
    radial-gradient(800px 480px at 88% 10%, rgba(28, 27, 25, 0.035), transparent 65%);
}

.shell {
  width: min(100% - 2.5rem, 66rem);
  margin: 0 auto;
  padding: clamp(1.25rem, 3vw, 2.25rem) 0 2rem;
}

/* Header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.header-main {
  display: flex;
  align-items: center;
  gap: .85rem;
}

.site-mark {
  position: relative;
  width: 2.5rem;
  height: 2.5rem;
  flex-shrink: 0;
  border-radius: .65rem;
  background: var(--vermilion);
  box-shadow: 0 2px 6px rgba(184, 59, 42, 0.22);
}

.site-mark::before {
  content: "";
  position: absolute;
  left: .52rem;
  top: .65rem;
  width: 1.45rem;
  height: .72rem;
  border-left: 2.5px solid #fffefa;
  border-bottom: 2.5px solid #fffefa;
  border-radius: 0 0 0 .35rem;
  transform: rotate(-36deg);
}

h1 {
  margin: 0;
  font-size: clamp(1.35rem, 3vw, 1.75rem);
  line-height: 1.2;
  letter-spacing: -0.01em;
}

.subtitle {
  margin: .2rem 0 0;
  font-size: .875rem;
  color: var(--muted);
}

.privacy-badge {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  padding: .35rem .75rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 254, 250, 0.85);
  color: var(--muted);
  font-size: .8125rem;
  font-weight: 500;
}

.privacy-dot {
  width: .45rem;
  height: .45rem;
  border-radius: 50%;
  background: #2f7d4d;
}

/* Workspace grid */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(17rem, 20.5rem);
  gap: 1.125rem;
  align-items: start;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 1.05rem;
  padding: clamp(.9rem, 2vw, 1.35rem);
  min-width: 0;
  box-shadow: 0 1px 2px rgba(28, 27, 25, 0.04);
}

.panel-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: .75rem;
  margin-bottom: .75rem;
}

.panel-heading.compact {
  margin-bottom: .4rem;
}

h2 {
  margin: 0;
  font-size: 1.0625rem;
  line-height: 1.35;
}

.panel-hint,
.privacy,
.status,
.mobile-tip {
  font-size: .8125rem;
  color: var(--muted);
}

/* Brush Toolbar */
.brush-toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: .5rem .65rem;
  margin-bottom: .85rem;
  padding: .45rem .6rem;
  border: 1px solid var(--line);
  border-radius: .75rem;
  background: #faf8f2;
}

.toolbar-group {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
}

.toolbar-divider {
  width: 1px;
  height: 1.25rem;
  background: var(--line);
}

.swatch {
  position: relative;
  width: 1.65rem;
  height: 1.65rem;
  padding: 0;
  border: 2px solid transparent;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.swatch::before {
  content: "";
  width: 1.05rem;
  height: 1.05rem;
  border-radius: 50%;
  background: var(--swatch);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}

.swatch[aria-pressed="true"] {
  border-color: var(--vermilion);
  background: var(--surface);
}

.segmented {
  display: inline-flex;
  padding: .15rem;
  border: 1px solid var(--line);
  border-radius: .55rem;
  background: var(--surface);
  gap: .15rem;
}

.seg-btn,
.chip-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 1.9rem;
  padding: .2rem .6rem;
  border: 1px solid transparent;
  border-radius: .42rem;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: .8125rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background-color .15s ease, color .15s ease, border-color .15s ease;
}

.chip-btn {
  border-color: var(--line);
  background: var(--surface);
}

.seg-btn:hover,
.chip-btn:hover {
  color: var(--ink);
  background: #f5f2eb;
}

.seg-btn[aria-pressed="true"] {
  background: var(--ink);
  color: #fffefa;
}

.chip-btn[aria-pressed="true"] {
  border-color: rgba(184, 59, 42, 0.45);
  background: var(--vermilion-soft);
  color: var(--vermilion);
}

/* Writing Canvas */
.drawing-surface {
  position: relative;
  width: min(100%, 27.5rem);
  aspect-ratio: 1;
  margin-inline: auto;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line-strong);
  border-radius: .85rem;
  box-shadow: inset 0 0 0 .45rem #faf8f3;
}

.grid-lines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background-image:
    linear-gradient(to bottom right, transparent calc(50% - .5px), #efeae0 calc(50% - .5px), #efeae0 calc(50% + .5px), transparent calc(50% + .5px)),
    linear-gradient(to top right, transparent calc(50% - .5px), #efeae0 calc(50% - .5px), #efeae0 calc(50% + .5px), transparent calc(50% + .5px));
  background-size: 86% 86%;
  background-position: center;
  background-repeat: no-repeat;
}

.grid-frame {
  position: absolute;
  inset: 7%;
  border: 1px dashed #e6e0d3;
  border-radius: .35rem;
}

.grid-axis-h {
  position: absolute;
  inset: 50% 7% auto;
  border-top: 1px dashed #ded7c7;
}

.grid-axis-v {
  position: absolute;
  inset: 7% auto 7% 50%;
  border-left: 1px dashed #ded7c7;
}

.drawing-surface.no-grid .grid-lines {
  display: none;
}

.drawing-prompt {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  padding: .28rem .75rem;
  border-radius: .45rem;
  background: rgba(255, 255, 255, 0.92);
  color: var(--muted);
  font-size: .875rem;
  white-space: nowrap;
  pointer-events: none;
}

#drawing {
  position: relative;
  z-index: 3;
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

/* Buttons & Actions */
.editor-actions,
.action-group,
.character-actions {
  display: flex;
  align-items: center;
  gap: .4rem;
}

.editor-actions {
  margin-top: .95rem;
  justify-content: space-between;
  flex-wrap: wrap;
}

.button,
.text-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 2.55rem;
  padding: .42rem .9rem;
  border: 1px solid var(--line-strong);
  border-radius: .6rem;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: .9rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color .15s ease, border-color .15s ease, transform .1s ease;
}

.text-button {
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  padding-inline: .5rem;
  font-weight: 500;
}

.button:disabled,
.text-button:disabled {
  cursor: default;
  opacity: .42;
}

.button:hover:not(:disabled),
.text-button:hover:not(:disabled) {
  background: #f4f1ea;
}

.button:active:not(:disabled),
.text-button:active:not(:disabled) {
  transform: scale(0.985);
}

.primary {
  border-color: var(--cta);
  background: var(--cta);
  color: #fffefa;
}

.primary:hover:not(:disabled) {
  background: var(--cta-hover);
  border-color: var(--cta-hover);
}

.text-button.danger {
  color: var(--danger);
  background: var(--danger-bg);
  border-color: rgba(180, 35, 24, 0.25);
}

:focus-visible {
  outline: 2px solid var(--vermilion);
  outline-offset: 2px;
}

/* Preview & Export Panel */
.preview-surface {
  min-height: clamp(11rem, 26vw, 17rem);
  display: grid;
  place-items: center;
  padding: 1.25rem;
  border: 1px solid var(--line);
  border-radius: .85rem;
  overflow: auto;
}

.preview-surface[data-surface="checker"] {
  background: repeating-conic-gradient(#eeebe2 0% 25%, #ffffff 0% 50%) 0 0 / 20px 20px;
}

.preview-surface[data-surface="paper"] {
  background: #ffffff;
  box-shadow: inset 0 0 0 1px rgba(28, 27, 25, 0.04);
}

#result-img {
  display: block;
  max-width: 100%;
  max-height: 15rem;
  width: auto;
  height: auto;
  object-fit: contain;
}

.export-controls {
  margin-top: .9rem;
  padding: .75rem .85rem;
  border: 1px solid var(--line);
  border-radius: .75rem;
  background: #faf8f2;
  display: flex;
  flex-direction: column;
  gap: .65rem;
}

.control-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .65rem;
  flex-wrap: wrap;
}

.control-label {
  font-size: .8125rem;
  color: var(--muted);
  font-weight: 500;
}

.range-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--vermilion);
  cursor: pointer;
}

.range-value {
  font-size: .8125rem;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  min-width: 3.1rem;
  text-align: right;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(12.5rem, 1fr));
  gap: .55rem .9rem;
}

.mobile-tip {
  margin: .5rem 0 0;
  text-align: right;
}

/* Sidebar: Live Strip + Character Sequence */
.live-strip-wrap {
  margin-bottom: 1rem;
  padding-bottom: .9rem;
  border-bottom: 1px solid var(--line);
}

.strip-label {
  font-size: .8125rem;
  font-weight: 600;
  color: var(--ink);
}

.live-strip {
  width: 100%;
  height: 3.75rem;
  padding: .45rem .65rem;
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: .65rem;
  background: repeating-conic-gradient(#f3f0e8 0% 25%, #ffffff 0% 50%) 0 0 / 14px 14px;
  cursor: pointer;
  transition: border-color .15s ease, transform .1s ease;
}

.live-strip:hover {
  border-color: var(--vermilion);
}

.live-strip:active {
  transform: scale(0.99);
}

#live-strip-canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
}

.characters {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(4.2rem, 1fr));
  gap: .5rem;
  margin: 0;
  padding: 0;
  list-style: none;
}

.character {
  display: grid;
  justify-items: center;
  align-content: center;
  gap: .22rem;
  width: 100%;
  min-height: 4.15rem;
  padding: .35rem .2rem;
  border: 1px solid var(--line);
  border-radius: .6rem;
  background: var(--surface);
  color: var(--muted);
  font: inherit;
  font-size: .78rem;
  cursor: pointer;
  transition: border-color .15s ease, background-color .15s ease;
}

.character:hover {
  background: #f6f3ec;
  border-color: var(--line-strong);
}

.character[aria-current="true"] {
  border-color: var(--vermilion);
  background: var(--vermilion-soft);
  color: var(--vermilion);
  font-weight: 600;
}

.character.add-slot {
  border-style: dashed;
  color: var(--muted);
  gap: .1rem;
}

.character.add-slot:hover {
  color: var(--ink);
  border-color: var(--ink);
}

.add-plus {
  font-size: 1.2rem;
  line-height: 1;
}

.character canvas {
  width: 2.6rem;
  height: 1.75rem;
}

.character-actions {
  justify-content: space-between;
  margin-top: .65rem;
  flex-wrap: wrap;
}

.privacy {
  margin: .65rem 0 0;
  border-top: 1px solid var(--line);
  padding-top: .75rem;
  line-height: 1.5;
}

.status {
  margin: 0;
}

.status:not(:empty) {
  margin-top: .6rem;
  color: var(--vermilion);
  font-weight: 500;
}

/* Guide & Footer */
.guide {
  margin-top: 1.5rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
}

.guide-heading {
  font-size: .9375rem;
  color: var(--muted);
  font-weight: 600;
  margin-bottom: .75rem;
}

.guide-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: .85rem;
}

.guide-card {
  padding: .9rem 1rem;
  border: 1px solid var(--line);
  border-radius: .85rem;
  background: rgba(255, 254, 250, 0.72);
}

.guide-card h3 {
  margin: 0 0 .3rem;
  font-size: .9rem;
  color: var(--ink);
}

.guide-card p {
  margin: 0;
  font-size: .8125rem;
  color: var(--muted);
  line-height: 1.55;
}

.page-footer {
  margin-top: 1.25rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .75rem;
  font-size: .8125rem;
  color: var(--muted);
}

.footer-links {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.footer-links a {
  color: var(--muted);
  text-decoration: none;
}

.footer-links a:hover {
  color: var(--ink);
  text-decoration: underline;
}

/* Responsive */
@media (max-width: 860px) {
  .shell {
    width: min(100% - 1.5rem, 36rem);
    padding-top: 1rem;
  }

  .workspace {
    grid-template-columns: 1fr;
    gap: .85rem;
  }

  .guide-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 480px) {
  .toolbar-divider {
    display: none;
  }

  .brush-toolbar {
    justify-content: space-between;
    padding: .4rem .5rem;
  }

  .button {
    padding-inline: .72rem;
  }

  .privacy-badge {
    font-size: .75rem;
    padding: .25rem .6rem;
  }
}

@media (orientation: landscape) and (max-height: 560px) and (min-width: 480px) {
  .shell {
    padding-top: .65rem;
  }

  .page-header {
    margin-bottom: .65rem;
  }

  .subtitle {
    display: none;
  }

  .writing-panel {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 8.5rem;
    column-gap: .85rem;
  }

  .writing-panel .panel-heading,
  .writing-panel .brush-toolbar,
  .writing-panel .status {
    grid-column: 1 / -1;
  }

  .drawing-surface {
    width: min(100%, 24rem, 56svh);
  }

  .writing-panel .editor-actions {
    align-self: center;
    align-items: stretch;
    flex-direction: column;
    margin-top: 0;
  }

  .writing-panel .action-group {
    justify-content: space-between;
  }

  .writing-panel .action-group:last-child {
    flex-direction: column;
    align-items: stretch;
  }
}
