:root {
  --bg: #fbf1c7;
  --bg-2: #f2e5bc;
  --text: #3c3836;
  --muted: #7c6f64;
  --accent: #b57614;
  --border: #d5c4a1;
  --surface: #f2e5bc;
  --surface-hover: #ebdbb2;
  --card-a: rgba(213, 196, 161, 0.55);
  --card-b: rgba(189, 174, 147, 0.45);
  --danger: #cc241d;
  --radius: 12px;
}

:root[data-theme='dark'] {
  --bg: #1d2021;
  --bg-2: #282828;
  --text: #ebdbb2;
  --muted: #bdae93;
  --accent: #d79921;
  --border: #3c3836;
  --surface: #282828;
  --surface-hover: #3c3836;
  --card-a: rgba(60, 56, 54, 0.6);
  --card-b: rgba(80, 73, 69, 0.5);
  --danger: #fb4934;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  background-color: var(--bg);
}

body {
  min-height: 100vh;
  background: linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
  color: var(--text);
  font-family: 'Times New Roman', 'Times', 'Liberation Serif', serif;
  line-height: 1.6;
  text-rendering: optimizeLegibility;
}

button,
input,
textarea,
select {
  font-family: inherit;
}

.container {
  max-width: 860px;
  margin: 0 auto;
  padding: 2rem 1rem 4rem;
}

header {
  text-align: center;
  margin-bottom: 2rem;
  position: relative;
}

header h1 {
  font-size: clamp(1.9rem, 5vw, 2.6rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-bottom: 0.25rem;
}

header p {
  color: var(--muted);
  font-size: clamp(1rem, 2vw, 1.15rem);
}

.theme-toggle {
  position: absolute;
  top: 0;
  right: 0;
  background: color-mix(in srgb, var(--border) 45%, transparent);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.25rem 0.65rem;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: color 150ms ease, background-color 150ms ease, border-color 150ms ease;
}

.theme-toggle:hover,
.theme-toggle:focus-visible {
  color: var(--accent);
  border-color: var(--accent);
  background: color-mix(in srgb, var(--border) 60%, transparent);
}

section {
  margin-bottom: 1.75rem;
}

section h2 {
  font-size: 1.05rem;
  font-weight: 700;
  margin-bottom: 0.6rem;
  margin-top: 1.25rem;
  color: var(--text);
}

section h2:first-child {
  margin-top: 0;
}

label {
  display: block;
  font-weight: 700;
  margin-bottom: 0.4rem;
}

.hint {
  font-size: 0.9rem;
  color: var(--muted);
  margin-top: 0.4rem;
}

.input-area,
.output-area {
  width: 100%;
  min-height: 120px;
  padding: 0.85rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 1.05rem;
  line-height: 1.55;
  resize: vertical;
  outline: none;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

.input-area:focus,
.output-area:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 20%, transparent);
}

.output-area {
  min-height: 90px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.95rem;
  background: color-mix(in srgb, var(--surface) 70%, transparent);
}

.output-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.4rem;
}

.char-count {
  font-size: 0.9rem;
  color: var(--muted);
}

.char-count.over-limit {
  color: var(--danger);
  font-weight: 700;
}

.preview-box {
  min-height: 80px;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 60%, transparent);
  font-size: 1.4rem;
  line-height: 1.6;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.preview-line {
  display: inline;
}

.button-row {
  display: flex;
  gap: 0.75rem;
  margin-top: 0.75rem;
  flex-wrap: wrap;
}

button,
select,
input[type='number'],
input[type='text'] {
  background: color-mix(in srgb, var(--border) 45%, transparent);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.45rem 0.75rem;
  font-size: 0.95rem;
  cursor: pointer;
  transition: color 150ms ease, background-color 150ms ease, border-color 150ms ease;
}

button:hover,
button:focus-visible,
select:hover,
select:focus-visible,
input[type='number']:focus-visible,
input[type='text']:focus-visible {
  color: var(--accent);
  border-color: var(--accent);
  background: color-mix(in srgb, var(--border) 60%, transparent);
}

button.primary {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}

button.primary:hover,
button.primary:focus-visible {
  background: color-mix(in srgb, var(--accent) 85%, var(--text));
  border-color: color-mix(in srgb, var(--accent) 85%, var(--text));
  color: var(--bg);
}

.control-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 0.5rem;
}

/* Color picker */
.color-picker {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  background: color-mix(in srgb, var(--surface) 50%, transparent);
  display: grid;
  gap: 0.75rem;
  max-width: 312px;
}

.sv-wrap,
.hue-wrap,
.gradient-bar-wrap {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  cursor: crosshair;
}

.sv-wrap canvas,
.hue-wrap canvas,
.gradient-bar-wrap canvas {
  display: block;
  width: 100%;
  height: auto;
}

.sv-cursor,
.hue-cursor {
  position: absolute;
  width: 12px;
  height: 12px;
  border: 2px solid white;
  border-radius: 50%;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
  pointer-events: none;
  transform: translate(-50%, -50%);
}

.hue-cursor {
  top: 50%;
  left: 0;
}

.color-info {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.color-preview {
  width: 40px;
  height: 40px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: #ff0055;
  flex-shrink: 0;
}

#hex-input {
  flex: 1;
  text-transform: uppercase;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

/* Gradient editor */
.gradient-builder {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  background: color-mix(in srgb, var(--surface) 50%, transparent);
}

.gradient-bar-wrap {
  height: 28px;
  margin-bottom: 0.75rem;
}

.gradient-handles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.gradient-handle {
  position: absolute;
  top: 50%;
  width: 14px;
  height: 22px;
  border: 2px solid white;
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4);
  transform: translate(-50%, -50%);
  pointer-events: auto;
  cursor: grab;
}

.gradient-handle.selected {
  box-shadow: 0 0 0 2px var(--accent), 0 0 0 3px rgba(0, 0, 0, 0.4);
  z-index: 1;
}

.gradient-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.gradient-actions .toggle {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  font-weight: 400;
  cursor: pointer;
}

.gradient-actions .toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.motion-row {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 0.75rem;
}

.motion-control {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.motion-control label {
  font-size: 0.85rem;
  margin-bottom: 0;
}

.motion-control input {
  width: 100%;
  border-radius: var(--radius);
}

.motion-control button {
  width: 100%;
}

.reference {
  border-top: 1px solid var(--border);
  padding-top: 1.5rem;
}

.reference summary {
  cursor: pointer;
  color: var(--muted);
  font-weight: 700;
}

.reference dl {
  display: grid;
  gap: 0.7rem;
  margin-top: 0.75rem;
}

.reference dt {
  font-weight: 700;
}

.reference dd {
  color: var(--muted);
  margin-left: 0;
}

.reference code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  background: color-mix(in srgb, var(--border) 40%, transparent);
  border-radius: 4px;
  padding: 0.1rem 0.3rem;
  font-size: 0.9em;
}

@media (max-width: 520px) {
  .theme-toggle {
    position: static;
    margin-top: 0.75rem;
  }

  header {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .motion-row {
    grid-template-columns: 1fr;
  }
}
