:root {
  --app-bg: #f3f1eb;
  --panel-bg: #ffffff;
  --text: #111111;
  --muted: #5c5c5c;
  --line: #d7d2c8;
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.08);
  --page-width: 210mm;
  --page-height: 297mm;
  --page-padding-x: 18mm;
  --page-padding-y: 18mm;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Avenir Next", "Helvetica Neue", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.95), transparent 36%),
    linear-gradient(180deg, #efebe2 0%, #f7f5ef 100%);
}

.app-shell {
  max-width: 100%;
  margin: 0;
  padding: 0;
}

.app-header {
  margin-bottom: 24px;
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
}

.app-header .eyebrow {
  margin: 0 0 8px;
  font-size: 0.82rem;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.app-shell h1,
.app-shell h2,
.app-shell p {
  margin-top: 0;
}

.app-shell h1 {
  margin-bottom: 8px;
  font-size: clamp(2rem, 3vw, 3.3rem);
  line-height: 1;
}

.app-shell .subtitle,
.app-shell .help-text {
  color: var(--muted);
}

.app-header .language-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  min-width: 260px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #ffffff;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.08);
}

.app-header .language-button {
  padding: 14px 18px;
  background: transparent;
  color: var(--text);
  box-shadow: none;
}

.app-header .language-button.active {
  background: #111111;
  color: #ffffff;
}

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 440px) minmax(420px, 1fr);
  gap: 24px;
  align-items: start;
}

.panel {
  background: var(--panel-bg);
  border: 1px solid rgba(17, 17, 17, 0.08);
  border-radius: 22px;
  box-shadow: var(--shadow);
}

.controls-panel {
  padding: 0;
  height: min(950px, calc(100vh - 148px));
  overflow: hidden;
}

.preview-panel {
  padding: 24px;
  max-height: min(950px, calc(100vh - 148px));
  overflow: auto;
}

.controls-scroll {
  height: 100%;
  overflow: auto;
  padding: 24px;
}

textarea {
  width: 100%;
  min-height: 320px;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 18px;
  resize: vertical;
  font: 500 1rem/1.6 "SF Mono", "Menlo", monospace;
  background: #fffdfa;
}

.prompt-box {
  min-height: 280px;
}

.button-row {
  display: flex;
  gap: 12px;
  margin: 18px 0 28px;
}

.app-shell button {
  appearance: none;
  border: none;
  border-radius: 999px;
  padding: 13px 18px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: transform 120ms ease, opacity 120ms ease, box-shadow 120ms ease;
}

.app-shell button:hover {
  transform: translateY(-1px);
}

.app-shell button:active {
  transform: translateY(0);
}

#previewButton {
  background: #111111;
  color: #ffffff;
  box-shadow: 0 10px 24px rgba(17, 17, 17, 0.18);
}

#exportButton {
  background: #ece7dc;
  color: #111111;
}

.secondary-button {
  background: #f2efe7;
  color: #111111;
}

.controls-grid {
  display: grid;
  gap: 16px;
}

.control-group {
  display: grid;
  gap: 14px;
  padding: 18px 0 8px;
}

.control-group + .control-group {
  border-top: 1px dashed var(--line);
}

.group-heading-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.group-heading-row h3 {
  margin: 0;
  font-size: 1.03rem;
}

.group-dots {
  flex: 1;
  height: 1px;
  background-image: radial-gradient(circle, rgba(17, 17, 17, 0.28) 1px, transparent 1.5px);
  background-size: 10px 1px;
  background-repeat: repeat-x;
}

label {
  display: grid;
  gap: 8px;
}

label span {
  font-weight: 700;
}

select,
input[type="range"] {
  width: 100%;
}

output {
  color: var(--muted);
  font-size: 0.95rem;
}

.instructions {
  margin-top: 28px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.format-guide {
  margin: 12px 0 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #fcfbf7;
}

.format-guide summary {
  cursor: pointer;
  padding: 14px 16px;
  font-weight: 700;
}

.format-guide-body {
  padding: 0 16px 16px;
}

.format-guide pre {
  margin: 12px 0;
  padding: 14px;
  border-radius: 12px;
  background: #fff;
  border: 1px solid var(--line);
  overflow: auto;
  font: 500 0.95rem/1.6 "SF Mono", "Menlo", monospace;
}

.format-guide ul {
  margin: 0;
  padding-left: 18px;
  line-height: 1.7;
}

.instructions ol {
  margin: 0;
  padding-left: 20px;
  line-height: 1.7;
}

.override-panel {
  display: grid;
  gap: 16px;
}

.override-header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 16px;
}

.override-target {
  padding: 8px 12px;
  border-radius: 999px;
  background: #f5f3ec;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.override-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 14px;
}

.compact-row {
  margin: 0;
}

.override-list {
  display: grid;
  gap: 10px;
}

.override-chip {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fbfaf6;
}

.override-chip button {
  padding: 8px 12px;
  border-radius: 999px;
}

.override-empty {
  color: var(--muted);
  font-size: 0.95rem;
}

.preview-panel.flash-preview {
  animation: previewPulse 500ms ease;
}

@keyframes previewPulse {
  0% { box-shadow: var(--shadow); }
  40% { box-shadow: 0 0 0 3px rgba(17, 17, 17, 0.12), 0 20px 50px rgba(0, 0, 0, 0.12); }
  100% { box-shadow: var(--shadow); }
}

.status-message {
  min-height: 24px;
  margin-bottom: 14px;
  color: var(--muted);
}

.status-message.error {
  color: #a12626;
}

.status-message.fresh {
  color: #1f5a2e;
}

.preview-container {
  display: grid;
  gap: 26px;
  justify-items: center;
}

.flashcard-page {
  width: var(--page-width);
  height: var(--page-height);
  background: #ffffff;
  color: #000000;
  border: 1px solid rgba(17, 17, 17, 0.12);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
}

.flashcard-page.selected-page {
  outline: 3px solid rgba(17, 17, 17, 0.26);
  outline-offset: 5px;
}

.page-inner {
  height: 100%;
  padding: var(--page-padding-y) var(--page-padding-x);
  display: grid;
  grid-template-rows: 1fr 1fr;
}

.card-half {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 10mm 6mm;
}

.top-half {
  border-bottom: 1.5px dashed #777777;
}

.character {
  margin: 0;
  font-weight: 800;
  line-height: 1;
  cursor: pointer;
}

.bottom-stack {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  max-width: 100%;
}

.pinyin,
.sentence {
  margin: 0;
  color: currentColor;
}

.pinyin {
  font-weight: 600;
  line-height: 1.2;
  cursor: pointer;
}

.sentence {
  line-height: var(--sentence-line-height, 1.45);
  max-width: var(--sentence-width, 88%);
  word-break: normal;
  overflow-wrap: break-word;
  line-break: loose;
  text-wrap: pretty;
  cursor: pointer;
}

.sentence strong {
  font-weight: 800;
}

.selectable-field.selected-field {
  background: rgba(17, 17, 17, 0.06);
  border-radius: 8px;
  box-shadow: 0 0 0 2px rgba(17, 17, 17, 0.18);
}

@media (max-width: 1180px) {
  .app-header {
    align-items: start;
    flex-direction: column;
  }

  .app-header .language-toggle {
    width: 100%;
    max-width: 420px;
  }

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

  .override-header,
  .override-grid {
    display: grid;
    grid-template-columns: 1fr;
  }

  .controls-panel {
    height: auto;
    overflow: visible;
  }

  .controls-scroll,
  .preview-panel {
    max-height: none;
    overflow: visible;
  }
}

@media (max-width: 900px) {
  .button-row {
    flex-direction: column;
  }

  .override-target {
    white-space: normal;
  }

  .flashcard-page {
    width: 100%;
    height: auto;
    aspect-ratio: 210 / 297;
  }

  .page-inner {
    height: 100%;
    padding: 7vw 6vw;
  }
}

@page {
  size: A4 portrait;
  margin: 0;
}

@media print {
  body {
    background: #ffffff;
  }

  .no-print {
    display: none !important;
  }

  .app-shell,
  .preview-panel {
    max-width: none;
    margin: 0;
    padding: 0;
    box-shadow: none;
    border: none;
  }

  .layout {
    display: block;
  }

  .panel {
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
  }

  .controls-panel,
  .controls-scroll,
  .preview-panel {
    height: auto;
    max-height: none;
    overflow: visible;
  }

  .preview-container {
    display: block;
  }

  .flashcard-page {
    width: 185mm;
    height: 260mm;
    border: none;
    box-shadow: none;
    margin: 10mm auto 0;
    page-break-inside: avoid;
  }

  .flashcard-page + .flashcard-page {
    page-break-before: always;
  }
}
