/* Hallmark — macrostructure: Workbench — tone: utilitarian — anchor: warm oat / ink blue
 * pre-emit critique: P5 H5 E4 S5 R5 V4
 * genre: editorial — theme: Almanac
 * audience: elementary-school teachers — use: build kanji worksheets — tone: utilitarian
 * nav: edge-aligned app header — footer: none (application shell) — enrichment: none
 * protected: A4 result rendering and print rules
 * ==========================================================================
   Import Google Fonts
   ========================================================================== */
@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@400;500;700&family=Noto+Serif+JP:wght@400;600;700&family=Shippori+Mincho:wght@600;700&display=swap');
@import url('./tokens.css');

/* ==========================================================================
   CSS Variables & Reset
   ========================================================================== */
:root {
  --bg-primary: var(--color-paper-2);
  --bg-secondary: var(--color-paper-3);
  --bg-card: var(--color-surface);
  --border-color: var(--color-rule);
  --text-primary: var(--color-ink);
  --text-secondary: var(--color-muted);
  --accent-color: var(--color-accent);
  --accent-hover: var(--color-ink);
  --success-color: var(--color-success);
  --font-ui: var(--font-body);
  --font-print: 'Noto Serif JP', 'Yu Mincho', 'MS Mincho', serif;
  
  --preview-scale: 1;
}

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

body {
  background-color: var(--bg-primary);
  color: var(--text-primary);
  font-family: var(--font-ui);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  overflow-x: clip;
}

/* ==========================================================================
   App Header (UI)
   ========================================================================== */
.app-header {
  background: rgba(15, 23, 42, 0.8);
  backdrop-filter: none;
  border-bottom: 1px solid var(--border-color);
  padding: 1.25rem 2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
}

.logo-container {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.server-status {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.78rem;
  font-weight: 500;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid transparent;
  transition: opacity var(--dur-short) var(--ease-out);
}

.server-status__dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.server-status--checking {
  background: rgba(148, 163, 184, 0.1);
  border-color: rgba(148, 163, 184, 0.2);
  color: var(--text-secondary);
}
.server-status--checking .server-status__dot {
  background: var(--text-secondary);
  opacity: 0.6;
}

.server-status--ok {
  background: rgba(16, 185, 129, 0.1);
  border-color: rgba(16, 185, 129, 0.3);
  color: #34d399;
}
.server-status--ok .server-status__dot {
  background: #10b981;
  box-shadow: 0 0 6px #10b981;
  animation: pulse-dot 2s infinite;
}

.server-status--error {
  background: rgba(239, 68, 68, 0.1);
  border-color: rgba(239, 68, 68, 0.3);
  color: #f87171;
}
.server-status--error .server-status__dot {
  background: #ef4444;
}

@keyframes pulse-dot {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.4; }
}

.logo-icon {
  background: var(--color-surface);
  color: var(--color-ink);
  border: var(--rule-hair) solid var(--color-ink);
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 1.25rem;
  box-shadow: none;
}

.app-title {
  font-size: 1.25rem;
  font-weight: 600;
  letter-spacing: -0.025em;
  color: var(--color-ink);
  background: none;
  -webkit-background-clip: initial;
  -webkit-text-fill-color: currentColor;
}

.app-subtitle {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ==========================================================================
   Main Layout Container
   ========================================================================== */
.main-container {
  display: flex;
  flex: 1;
  padding: 2rem;
  gap: 2rem;
  max-width: 1600px;
  margin: 0 auto;
  width: 100%;
}

/* ==========================================================================
   Control Panel (Left Side)
   ========================================================================== */
.control-panel {
  flex: 1;
  min-width: 380px;
  max-width: 460px;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.card {
  background: var(--bg-card);
  backdrop-filter: none;
  border: 1px solid var(--border-color);
  border-radius: 1rem;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.card-title {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 1.25rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #f1f5f9;
  border-left: 3px solid var(--accent-color);
  padding-left: 0.5rem;
}

/* Tab Selector Styles */
.tab-container {
  display: flex;
  background: rgba(15, 23, 42, 0.4);
  padding: 0.25rem;
  border-radius: 0.5rem;
  margin-bottom: 1.25rem;
  border: 1px solid rgba(255, 255, 255, 0.05);
}

.tab-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-secondary);
  padding: 0.5rem 0.25rem;
  font-size: 0.8rem;
  font-weight: 500;
  border-radius: 0.375rem;
  cursor: pointer;
  transition: color var(--dur-short) var(--ease-out), transform var(--dur-micro) var(--ease-out);
}

.tab-btn.active {
  background: var(--accent-color);
  color: #fff;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.2);
}

/* Form Groups */
.form-group {
  margin-bottom: 1.25rem;
}

.form-group:last-child {
  margin-bottom: 0;
}

label {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 0.5rem;
}

select, input[type="text"], input[type="number"], textarea {
  width: 100%;
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid var(--border-color);
  border-radius: 0.5rem;
  color: var(--text-primary);
  padding: 0.75rem;
  font-size: 0.95rem;
  font-family: var(--font-ui);
  transition: background-color var(--dur-short) var(--ease-out), color var(--dur-short) var(--ease-out);
}

select:focus, input:focus-visible, textarea:focus {
  outline: none;
  border-color: var(--accent-color);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; align-items: end; }
.option-grid { display: grid; gap: .55rem; }
.check-row { display: flex; align-items: center; gap: .55rem; margin: 0; cursor: pointer; }
.check-row input { accent-color: var(--accent-color); }
.palette-toolbar, .inline-actions { display: flex; flex-wrap: wrap; gap: .5rem; margin: .75rem 0; }
.btn-small, .variant-nav button { border: 1px solid var(--border-color); background: rgba(255,255,255,.07); color: var(--text-primary); border-radius: .45rem; padding: .5rem .7rem; cursor: pointer; }
.btn-small:hover, .variant-nav button:hover { background: rgba(255,255,255,.14); }
.btn-danger { border: 1px solid #ef4444; background: transparent; color: #fca5a5; border-radius: .45rem; padding: .6rem .8rem; cursor: pointer; }
.kanji-palette { display: grid; grid-template-columns: repeat(auto-fill,minmax(52px,1fr)); gap: .35rem; max-height: 230px; overflow: auto; }
.kanji-choice { display: flex; flex-direction: column; align-items: center; padding: .4rem .2rem; border: 1px solid var(--border-color); border-radius: .4rem; color: var(--text-primary); cursor: pointer; }
.kanji-choice:has(input:checked) { border-color: var(--accent-color); background: rgba(99,102,241,.16); }
.kanji-choice input { position: absolute; opacity: 0; }
.kanji-choice small { color: var(--text-secondary); font-size: .65rem; }
.stats-strip { display: flex; justify-content: space-between; color: var(--text-secondary); margin-top: .75rem; }
.badge { display: inline-flex; min-width: 1.6rem; height: 1.6rem; align-items: center; justify-content: center; border-radius: 99px; background: var(--accent-color); color: #fff; font-size: .75rem; }
.preview-header { gap: .75rem; }
.variant-nav { margin-left: auto; display: flex; gap: .3rem; }
.app-alert { margin: 0 0 1rem; padding: .8rem 1rem; border-radius: .6rem; background: #fff7d6; border: 1px solid #e8c24e; color: #573f00; }
.grading-panel { width: min(100%, 1000px); background: var(--bg-card); border: 1px solid var(--border-color); border-radius: .8rem; padding: 1rem; margin-top: 1rem; color: var(--text-primary); }
.grading-list { display: flex; flex-wrap: wrap; gap: .45rem; margin: .75rem 0; }
.grading-chip { display: flex; align-items: center; gap: .35rem; border: 1px solid var(--border-color); border-radius: 99px; padding: .35rem .55rem; cursor: pointer; }
.grading-chip:has(input:checked) { border-color: #ef4444; background: rgba(239,68,68,.13); }
.target-kanji { border-right: 1.5px solid #0055ff; font-weight: 700; }
.reading-line { min-width: 36px; min-height: 72px; border: 1.2px solid #000; display: inline-flex; align-items: center; justify-content: center; }
.practice-steps { display: flex; flex-direction: column; gap: 3px; }
.practice-steps .answer-boxes:first-child .answer-key { display: block; color: #d1d5db; }
.layout-font-small .question-item { font-size: .88em; }
.layout-font-large .question-item { font-size: 1.12em; }
#print-batch { display: none; }
.print-page-label { position: absolute; top: 6mm; right: 9mm; color: #555; font: 9pt sans-serif; }
.print-document { position: relative; }

.app-dialog { width: min(720px, 92vw); max-height: 86vh; overflow: auto; border: 1px solid var(--border-color); border-radius: .8rem; background: var(--color-surface); color: var(--text-primary); padding: 1.25rem; }
.app-dialog::backdrop { background: var(--color-overlay); }
.app-dialog-wide { width: min(1100px, 94vw); }
.dialog-close { float: right; background: none; border: 0; color: var(--color-ink); font-size: 1.6rem; cursor: pointer; }
.admin-toolbar { display: grid; grid-template-columns: 120px 1fr auto auto; gap: .5rem; margin: 1rem 0; }
.admin-layout { display: grid; grid-template-columns: minmax(280px,.8fr) minmax(360px,1.2fr); gap: 1rem; }
.admin-list { max-height: 520px; overflow: auto; border: 1px solid var(--border-color); border-radius: .5rem; }
.admin-row { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border-color); background: transparent; color: var(--text-primary); padding: .65rem; cursor: pointer; }
.admin-row:hover { background: rgba(255,255,255,.06); }
.admin-row small { display: block; color: var(--text-secondary); }
.validation-box, .quality-summary { padding: .75rem; border-radius: .5rem; background: rgba(255,255,255,.06); margin: .75rem 0; }
.validation-box.valid { border: 1px solid #22c55e; color: #86efac; }
.validation-box.invalid { border: 1px solid #ef4444; color: #fca5a5; }

textarea {
  resize: vertical;
  min-height: 160px;
  font-family: monospace;
  font-size: 0.85rem;
  line-height: 1.5;
}

/* Button Styles */
.btn {
  width: 100%;
  background: var(--accent-color);
  color: #fff;
  border: none;
  border-radius: 0.5rem;
  padding: 0.85rem;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  transition: background-color var(--dur-short) var(--ease-out), color var(--dur-short) var(--ease-out), transform var(--dur-micro) var(--ease-out);
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.25);
}

.btn:hover {
  background: var(--accent-hover);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.05);
  color: #e2e8f0;
  border: 1px solid var(--border-color);
  box-shadow: none;
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

/* Toggle Switches */
.toggle-group {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 0;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #334155;
  transition: .3s;
  border-radius: 24px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  transition: .3s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--accent-color);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

/* Mode Container Switches */
.mode-panel {
  display: none;
}

.mode-panel.active {
  display: block;
}

/* Help text */
.help-text {
  font-size: 0.8rem;
  color: var(--text-secondary);
  line-height: 1.4;
  margin-top: 0.5rem;
}

.help-text code {
  background: rgba(255, 255, 255, 0.08);
  padding: 0.1rem 0.3rem;
  border-radius: 0.25rem;
  color: #e2e8f0;
}

/* ==========================================================================
   Preview Container (Right Side)
   ========================================================================== */
.preview-container {
  flex: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.preview-header {
  width: 100%;
  max-width: 800px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem;
}

.preview-title {
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-secondary);
}

/* A4 Sheet Preview Wrapper */
.a4-wrapper {
  border: 1px solid rgba(255, 255, 255, 0.15);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.4);
  background-color: #fff;
  border-radius: 4px;
  transform-origin: top center;
  overflow: hidden;
  margin-bottom: 2rem;
  width: 1123px; /* A4 landscape: 297mm @ 96DPI = ~1123px */
  height: 794px;  /* A4 landscape: 210mm @ 96DPI = ~794px */
  position: relative;
  color: #000; /* Force black text in preview */
}

/* ==========================================================================
   A4 Print Document Style (Shared with @media print)
   ========================================================================== */
.print-document {
  width: 100%;
  height: 100%;
  padding: 15mm;
  background-color: #fff;
  font-family: var(--font-print);
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

/* Header Area (Horizontal) */
.test-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 2px solid #000;
  padding-bottom: 8px;
  margin-bottom: 12px;
  height: 40px; /* Exact height for header */
}

.test-title {
  font-size: 18pt;
  font-weight: 700;
  color: #000;
  font-family: var(--font-print);
}

/* Custom score box or metadata placeholder if needed, keep it simple */
.test-meta {
  font-size: 10pt;
  color: #000;
  display: flex;
  align-items: center;
  gap: 15px;
}

/* Question Area */
.test-questions-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: calc(100% - 52px);
  box-sizing: border-box;
}

/* Rows (Upper and Lower) */
.test-row {
  display: flex;
  height: 48%; /* Each row takes around half height, leaving space for margins */
  justify-content: space-between;
  align-items: flex-start; /* Align questions to the top line of the row */
  box-sizing: border-box;
  padding: 8px 0;
}

/* Dotted horizontal separator line between upper and lower row */
.test-row.row-upper {
  border-bottom: 1px dotted #888;
}

/* Row flow direction based on config */
.test-questions-container.flow-ltr .test-row {
  flex-direction: row;
}

.test-questions-container.flow-rtl .test-row {
  flex-direction: row-reverse;
}

/* Individual Question Block */
.question-item {
  display: block;
  writing-mode: vertical-rl;
  -webkit-writing-mode: vertical-rl;
  height: auto;
  box-sizing: border-box;
}

/* Question Sentence Column (Right side under vertical-rl) */
.sentence-col {
  padding-top: 5px;
  margin-left: 8px;
  color: #000;
  font-family: var(--font-print);
  letter-spacing: 0.05em;
  white-space: nowrap;
  line-height: 1.1; /* Tighter vertical character packing */
}

.kanji-ruby {
  ruby-position: over;
}

.kanji-ruby rt {
  font-size: 0.5em;                  /* ルビをやや大きめにして判読性を確保 */
  letter-spacing: 0.08em;            /* 文字同士がくっつかないよう余白を確保 */
  line-height: 1.1;
  font-variant-ligatures: none;      /* 意図しない合字を無効化 */
  /* 縦書きで文字詰め(vpal/palt)が効くと「とお」の「お」が小書きのように
     見えることがあるため、プロポーショナルメトリクスを明示的に無効化する */
  font-feature-settings: "vpal" 0, "palt" 0;
}

/* Underline for target hiragana */
.target-hiragana {
  border-right: 1.5px solid #0055ff; /* Use blue for a premium look in preview, print will handle monochrome */
  padding-right: 1.5px;
  margin-right: -2px;
}

@media print {
  .target-hiragana {
    border-right-color: #000 !important; /* Force black in print */
  }
}

/* Answer Writing Column (Left side under vertical-rl) */
.answer-col {
  display: block;
  text-align: center;
}

/* Question Number (e.g. ①) */
.q-number {
  font-size: 10pt;
  font-weight: bold;
  color: #000;
  display: inline;
  line-height: 1;
  margin-inline-end: 2px;
}

/* Answer Boxes Container */
.answer-boxes {
  display: block;
}

/* Single Kanji Square (e.g. 1 square for 1 kanji character) */
.kanji-square {
  width: 36px;
  height: 36px;
  border: 1.2px solid #000;
  position: relative;
  background-color: #fff;
  box-sizing: border-box;
  margin-bottom: -1.2px; /* Collapse adjacent borders */
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Guide cross lines inside square */
.kanji-square::before {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  border-top: 1px dotted #bbb;
  pointer-events: none;
}

.kanji-square::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  border-left: 1px dotted #bbb;
  pointer-events: none;
}

/* Elongated bracket for Okurigana */
.okurigana-bracket {
  width: 36px;
  height: 70px; /* Dynamic or default height */
  border: 1.2px solid #000;
  position: relative;
  background-color: #fff;
  box-sizing: border-box;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Answer key text inside the boxes (thin light gray or red, shown when toggled) */
.answer-key {
  font-size: 14pt;
  color: #d1d5db; /* Very light gray for writing guides, or red for grading key */
  font-family: var(--font-print);
  writing-mode: vertical-rl;
  display: none;
  z-index: 5;
}

.show-answers .answer-key {
  display: block;
}

/* Red grading key style */
.answer-key-red {
  color: #dc2626 !important;
  font-weight: bold;
}

/* ==========================================================================
   Print Media Query
   ========================================================================== */
@media print {
  /* Hide all UI elements */
  body, html {
    background-color: #fff !important;
    color: #000 !important;
    height: auto;
    overflow: visible;
  }
  
  .app-header, 
  .control-panel, 
  .preview-header,
  .preview-container,
  .app-alert,
  .grading-panel,
  dialog,
  footer {
    display: none !important;
  }

  #print-batch { display: block !important; }
  #print-batch .print-document { page-break-after: always; break-after: page; }
  #print-batch .print-document:last-child { page-break-after: auto; break-after: auto; }

  .main-container {
    padding: 0 !important;
    margin: 0 !important;
    max-width: none !important;
    width: 100% !important;
    display: block !important;
  }

  .a4-wrapper {
    border: none !important;
    box-shadow: none !important;
    width: 297mm !important;
    height: 210mm !important;
    padding: 0 !important;
    margin: 0 !important;
    transform: none !important;
    page-break-after: avoid;
    page-break-before: avoid;
  }

  .print-document {
    padding: 15mm 18mm !important; /* Slightly tighter margins for printing safety */
    width: 297mm !important;
    height: 210mm !important;
  }

  /* Force background colors and borders for printing */
  * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
  
  /* Page size setup */
  @page {
    size: A4 landscape;
    margin: 0;
  }
}

/* ==========================================================================
   Density-based Dynamic Scaling for Questions
   ========================================================================== */

/* Low Density (<= 10 questions) */
.density-low .question-item {
  font-size: 18pt;
  margin-bottom: 12px;
}
.density-low .kanji-square {
  width: 78px;
  height: 78px;
  margin-bottom: -1.2px;
}
.density-low .okurigana-bracket {
  width: 78px;
  height: 156px;
}
.density-low .answer-key {
  font-size: 24pt;
}

/* Medium Density (11 - 15 questions, e.g. 12 questions) */
.density-medium .question-item {
  font-size: 16pt;
  margin-bottom: 8px;
}
.density-medium .kanji-square {
  width: 72px;
  height: 72px;
  margin-bottom: -1.2px;
}
.density-medium .okurigana-bracket {
  width: 72px;
  height: 144px;
}
.density-medium .answer-key {
  font-size: 22pt;
}

/* Normal Density (16 - 20 questions) */
.density-normal .question-item {
  font-size: 14pt;
  margin-bottom: 6px;
}
.density-normal .kanji-square {
  width: 56px;
  height: 56px;
  margin-bottom: -1.2px;
}
.density-normal .okurigana-bracket {
  width: 56px;
  height: 112px;
}
.density-normal .answer-key {
  font-size: 18pt;
}

/* High Density (21 - 25 questions) */
.density-high .question-item {
  font-size: 12.5pt;
  margin-bottom: 4px;
}
.density-high .kanji-square {
  width: 45px;
  height: 45px;
  margin-bottom: -1.2px;
}
.density-high .okurigana-bracket {
  width: 45px;
  height: 90px;
}
.density-high .answer-key {
  font-size: 16pt;
}

/* ==========================================================================
   Responsive UI styling for screens
   ========================================================================== */
@media (max-width: 1200px) {
  .main-container {
    flex-direction: column;
    align-items: center;
  }
  .control-panel {
    max-width: 600px;
    width: 100%;
  }
}

@media (max-width: 768px) {
  .app-header {
    padding: 1rem;
  }
  .main-container {
    padding: 1rem;
    gap: 1rem;
  }
  .a4-wrapper {
    /* Scale down preview on mobile screens */
    width: 100%;
    height: auto;
    aspect-ratio: 297/210;
  }
  .form-row, .admin-layout, .admin-toolbar { grid-template-columns: 1fr; }
}

/* ==========================================================================
   Hallmark screen UI — light Workbench
   This block is screen-only so protected A4/print output remains unchanged.
   ========================================================================== */
@media screen {
  html,
  body {
    overflow-x: clip;
  }

  body {
    background: var(--color-paper-2);
    color: var(--color-ink);
    font-family: var(--font-body);
    font-size: var(--text-base);
    line-height: 1.6;
  }

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

  button,
  .tab-btn,
  .variant-nav button,
  .dialog-close {
    white-space: nowrap;
  }

  .btn,
  .btn-small,
  .btn-danger {
    overflow-wrap: anywhere;
    white-space: normal;
    text-wrap: balance;
  }

  .app-header {
    min-height: 4.75rem;
    padding: var(--space-sm) clamp(var(--space-md), 3vw, var(--space-xl));
    background: var(--color-surface);
    color: var(--color-ink);
    border-bottom: var(--rule-hair) solid var(--color-rule-2);
    box-shadow: none;
    backdrop-filter: none;
    z-index: var(--z-sticky);
  }

  .logo-container {
    gap: var(--space-sm);
    min-width: 0;
  }

  .logo-icon {
    width: 2.5rem;
    height: 2.5rem;
    flex: 0 0 2.5rem;
    background: var(--color-paper);
    color: var(--color-ink);
    border: var(--rule-hair) solid var(--color-ink);
    border-radius: var(--radius-control);
    box-shadow: none;
    font-family: var(--font-display);
    font-size: var(--text-lg);
    font-weight: 700;
  }

  .app-title {
    min-width: 0;
    overflow-wrap: anywhere;
    color: var(--color-ink);
    background: none;
    -webkit-background-clip: initial;
    -webkit-text-fill-color: currentColor;
    font-family: var(--font-display);
    font-size: var(--text-lg);
    font-style: normal;
    font-weight: 700;
    letter-spacing: -0.025em;
    line-height: 1.3;
  }

  .app-subtitle {
    margin-top: var(--space-3xs);
    color: var(--color-muted);
    font-size: var(--text-sm);
    line-height: 1.45;
  }

  .server-status {
    min-height: 2rem;
    padding: var(--space-2xs) var(--space-xs);
    gap: var(--space-xs);
    color: var(--color-muted);
    background: transparent;
    border: 0;
    border-radius: 0;
    font-size: var(--text-sm);
    font-weight: 500;
    transition: opacity var(--dur-short) var(--ease-out);
  }

  .server-status__dot {
    width: 0.5rem;
    height: 0.5rem;
    border-radius: var(--radius-control);
    box-shadow: none;
    animation: none;
  }

  .server-status--checking,
  .server-status--ok,
  .server-status--error {
    background: transparent;
    border-color: transparent;
  }

  .server-status--checking {
    color: var(--color-muted);
  }

  .server-status--checking .server-status__dot {
    background: var(--color-neutral);
  }

  .server-status--ok {
    color: var(--color-success);
  }

  .server-status--ok .server-status__dot {
    background: var(--color-success);
    box-shadow: none;
    animation: none;
  }

  .server-status--error {
    color: var(--color-error);
  }

  .server-status--error .server-status__dot {
    background: var(--color-error);
  }

  .main-container {
    display: grid;
    grid-template-columns: minmax(22rem, 27rem) minmax(0, 1fr);
    align-items: start;
    gap: clamp(var(--space-lg), 3vw, var(--space-2xl));
    width: 100%;
    max-width: 106rem;
    margin-inline: auto;
    padding: clamp(var(--space-md), 3vw, var(--space-xl));
  }

  .control-panel {
    position: static;
    display: flex;
    flex-direction: column;
    gap: 0;
    width: 100%;
    min-width: 0;
    max-width: none;
    max-height: none;
    overflow: visible;
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-card);
    box-shadow: var(--shadow-whisper);
    scrollbar-color: var(--color-rule-2) var(--color-paper-2);
  }

  .card {
    padding: var(--space-lg);
    background: transparent;
    color: var(--color-ink);
    border: 0;
    border-bottom: var(--rule-hair) solid var(--color-rule);
    border-radius: 0;
    box-shadow: none;
    backdrop-filter: none;
  }

  .card-title {
    display: block;
    margin: 0 0 var(--space-lg);
    padding: 0;
    color: var(--color-ink);
    border: 0;
    font-family: var(--font-display);
    font-size: var(--text-md);
    font-style: normal;
    font-weight: 700;
    letter-spacing: -0.015em;
    line-height: 1.35;
  }

  .card-title::before {
    content: none;
  }

  .card-collapse-body {
    min-width: 0;
  }

  .card.is-collapsed .card-title {
    margin-bottom: 0;
  }

  .section-toggle {
    position: relative;
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    width: calc(100% + (var(--space-xs) * 2));
    min-height: 2.75rem;
    margin: calc(var(--space-xs) * -1);
    padding: var(--space-xs);
    color: var(--color-ink);
    background: transparent;
    border: 0;
    border-radius: var(--radius-control);
    font: inherit;
    text-align: left;
    cursor: pointer;
  }

  .section-toggle::before {
    content: '';
    width: 0.5rem;
    height: 0.5rem;
    flex: 0 0 0.5rem;
    background: var(--color-accent);
  }

  .section-toggle-label {
    min-width: 0;
  }

  .section-toggle-icon {
    width: 0.55rem;
    height: 0.55rem;
    flex: 0 0 0.55rem;
    margin-left: auto;
    margin-right: 0.7rem;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: translateY(-20%) rotate(45deg);
    transition: transform var(--dur-short) var(--ease-out);
  }

  .card.is-collapsed .section-toggle-icon {
    transform: translateX(-20%) rotate(-45deg);
  }

  .control-panel > div:last-child {
    display: grid !important;
    grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
    gap: var(--space-sm) !important;
    padding: var(--space-lg);
    background: var(--color-paper);
  }

  .tab-container {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0;
    margin-bottom: var(--space-lg);
    padding: 0;
    background: transparent;
    border: 0;
    border-bottom: var(--rule-hair) solid var(--color-rule-2);
    border-radius: 0;
  }

  .tab-btn {
    position: relative;
    min-height: 2.75rem;
    padding: var(--space-xs) var(--space-2xs);
    color: var(--color-muted);
    background: transparent;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    font-size: var(--text-sm);
    font-weight: 500;
    transition: color var(--dur-short) var(--ease-out), transform var(--dur-micro) var(--ease-out);
  }

  .tab-btn::after {
    content: '';
    position: absolute;
    inset-inline: var(--space-xs);
    bottom: -1px;
    height: 2px;
    background: var(--color-accent);
    opacity: 0;
    transform: scaleX(0.5);
    transform-origin: center;
    transition: opacity var(--dur-short) var(--ease-out), transform var(--dur-short) var(--ease-out);
  }

  .tab-btn.active {
    color: var(--color-ink);
    background: transparent;
    box-shadow: none;
    font-weight: 700;
  }

  .tab-btn.active::after {
    opacity: 1;
    transform: scaleX(1);
  }

  .form-group {
    margin-bottom: var(--space-lg);
  }

  .form-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-sm);
  }

  /* Keep paired controls on one baseline. The global :last-child rule used to
     remove only the second column's bottom margin and push it down by 24px. */
  .form-row > .form-group,
  .form-row > .form-group:last-child {
    margin-bottom: 0;
  }

  .form-row:has(> .form-group) {
    margin-bottom: var(--space-lg);
  }

  .variants-row:has(> .form-group) {
    margin-bottom: 0;
  }

  #variants-help {
    margin-bottom: var(--space-lg);
  }

  label {
    margin-bottom: var(--space-xs);
    color: var(--color-ink-2);
    font-size: var(--text-sm);
    font-weight: 500;
    line-height: 1.45;
  }

  select,
  input[type='text'],
  input[type='number'] {
    height: 3rem;
  }

  select,
  input[type='text'],
  input[type='number'],
  textarea {
    min-height: 2.75rem;
    padding: var(--space-sm);
    padding-inline-end: var(--space-xl);
    color: var(--color-ink);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-input);
    outline: 2px solid transparent;
    outline-offset: 1px;
    box-shadow: none;
    font-family: var(--font-body);
    font-size: var(--text-base);
    line-height: 1.45;
    transition: background-color var(--dur-short) var(--ease-out), color var(--dur-short) var(--ease-out);
  }

  select,
  input[type='text'],
  input[type='number'] {
    padding-block: var(--space-xs);
    line-height: 1.5;
  }

  textarea {
    min-height: 9rem;
    resize: vertical;
  }

  input::placeholder,
  textarea::placeholder {
    color: var(--color-muted);
    opacity: 1;
  }

  select:focus,
  select:focus-visible,
  input:focus,
  input:focus-visible,
  textarea:focus,
  textarea:focus-visible {
    border-color: var(--color-ink-2);
    outline: 2px solid var(--color-focus);
    outline-offset: 1px;
    box-shadow: none;
  }

  input:disabled,
  select:disabled,
  textarea:disabled,
  button:disabled,
  [aria-disabled='true'] {
    opacity: 0.55;
    cursor: not-allowed;
  }

  [aria-invalid='true'] {
    border-color: var(--color-error);
  }

  [aria-busy='true'],
  [data-state='loading'] {
    cursor: progress;
  }

  button[aria-busy='true'],
  .btn[data-state='loading'] {
    color: var(--color-muted);
    background: var(--color-paper-3);
    border-color: var(--color-rule-2);
  }

  [data-state='success'] {
    color: var(--color-success);
    background: var(--color-success-soft);
    border-color: var(--color-success);
  }

  [data-state='error'] {
    color: var(--color-error);
    background: var(--color-error-soft);
    border-color: var(--color-error);
  }

  .option-grid {
    gap: var(--space-sm);
    margin-bottom: var(--space-lg);
  }

  .check-row {
    min-height: 2rem;
    gap: var(--space-xs);
    color: var(--color-ink-2);
  }

  .check-row input {
    width: 1.125rem;
    height: 1.125rem;
    flex: 0 0 1.125rem;
    accent-color: var(--color-accent);
  }

  .help-text {
    min-height: 1lh;
    margin-top: var(--space-xs);
    color: var(--color-muted);
    font-size: var(--text-sm);
    line-height: 1.55;
  }

  .help-text code {
    padding: var(--space-3xs) var(--space-2xs);
    color: var(--color-ink);
    background: var(--color-paper-3);
    border-radius: var(--radius-control);
    font-family: var(--font-body);
  }

  .palette-toolbar,
  .inline-actions {
    gap: var(--space-xs);
    margin: var(--space-sm) 0;
  }

  .btn,
  .btn-small,
  .variant-nav button,
  .btn-danger {
    min-height: 2.75rem;
    border-radius: var(--radius-control);
    cursor: pointer;
    font-weight: 700;
    transition: background-color var(--dur-short) var(--ease-out), color var(--dur-short) var(--ease-out), transform var(--dur-micro) var(--ease-out);
  }

  .btn {
    padding: var(--space-sm) var(--space-md);
    color: var(--color-paper);
    background: var(--color-ink);
    border: var(--rule-hair) solid var(--color-ink);
    box-shadow: none;
    font-size: var(--text-sm);
  }

  .btn-secondary {
    color: var(--color-ink);
    background: var(--color-surface);
    border-color: var(--color-rule-2);
    box-shadow: none;
  }

  .btn-small,
  .variant-nav button {
    padding: var(--space-xs) var(--space-sm);
    color: var(--color-ink);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule-2);
    box-shadow: none;
    font-size: var(--text-sm);
  }

  .btn-danger {
    padding: var(--space-xs) var(--space-sm);
    color: var(--color-error);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-error);
  }

  .btn:focus-visible,
  .btn-small:focus-visible,
  .btn-danger:focus-visible,
  .tab-btn:focus-visible,
  .variant-nav button:focus-visible,
  .kanji-choice:focus-within,
  .switch:focus-within,
  .dialog-close:focus-visible,
  .section-toggle:focus-visible,
  .admin-row:focus-visible {
    outline: 2px solid var(--color-focus);
    outline-offset: 2px;
  }

  .btn:active,
  .btn-small:active,
  .btn-danger:active,
  .tab-btn:active,
  .variant-nav button:active {
    transform: translateY(1px);
  }

  .toggle-group {
    min-height: 2.75rem;
    gap: var(--space-md);
    padding: 0;
  }

  .toggle-group > label:first-child {
    margin: 0;
  }

  .switch {
    width: 2.75rem;
    height: 1.5rem;
    flex: 0 0 2.75rem;
    margin: 0;
    border-radius: 1rem;
  }

  .slider {
    background: var(--color-rule-2);
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: 1rem;
    transition: background-color var(--dur-short) var(--ease-in-out);
  }

  .slider::before {
    width: 1rem;
    height: 1rem;
    background: var(--color-surface);
    border-radius: 50%;
    box-shadow: var(--shadow-whisper);
    transition: transform var(--dur-short) var(--ease-in-out);
  }

  input:checked + .slider {
    background: var(--color-accent);
    border-color: var(--color-accent);
  }

  .kanji-palette {
    grid-template-columns: repeat(auto-fill, minmax(3.25rem, 1fr));
    gap: var(--space-2xs);
    height: 15rem;
    max-height: 15rem;
    padding: var(--space-2xs);
    background: var(--color-paper);
    border: var(--rule-hair) solid var(--color-rule);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    overflow-anchor: none;
    align-content: start;
  }

  .kanji-palette:not(.is-loaded) {
    display: none;
  }

  .kanji-choice {
    position: relative;
    isolation: isolate;
    min-height: 3.25rem;
    padding: var(--space-xs) var(--space-2xs);
    color: var(--color-ink);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule);
    border-radius: var(--radius-control);
  }

  .kanji-choice input {
    position: absolute;
    inset: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    margin: 0;
    opacity: 0;
    cursor: pointer;
  }

  .kanji-choice span,
  .kanji-choice small {
    pointer-events: none;
  }

  .kanji-choice:has(input:checked) {
    color: var(--color-accent);
    background: var(--color-accent-soft);
    border-color: var(--color-accent);
  }

  .kanji-choice small {
    color: var(--color-muted);
    font-size: var(--text-xs);
  }

  .stats-strip {
    gap: var(--space-md);
    margin-top: var(--space-md);
    padding-block: var(--space-sm);
    color: var(--color-muted);
    border-block: var(--rule-hair) solid var(--color-rule);
    font-size: var(--text-sm);
    font-variant-numeric: tabular-nums;
  }

  .stats-strip strong {
    color: var(--color-ink);
  }

  .control-panel > .card:nth-of-type(3) .form-row,
  .control-panel > .card:nth-of-type(3) .inline-actions {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: var(--space-xs);
  }

  .control-panel > .card:nth-of-type(3) .form-row > *,
  .control-panel > .card:nth-of-type(3) .inline-actions > * {
    width: 100%;
    min-width: 0;
  }

  .control-panel > .card:nth-of-type(3) .form-row + .form-row {
    margin-top: var(--space-xs);
  }

  .preview-container {
    min-width: 0;
    align-items: center;
    gap: var(--space-md);
    overflow-x: auto;
    scrollbar-color: var(--color-rule-2) var(--color-paper-2);
  }

  .preview-header {
    max-width: 70.1875rem;
    min-height: 3rem;
    gap: var(--space-sm);
    padding: var(--space-xs) 0;
    color: var(--color-ink);
    border-bottom: var(--rule-hair) solid var(--color-rule-2);
  }

  .preview-title {
    color: var(--color-ink-2);
    font-size: var(--text-sm);
    font-weight: 700;
  }

  .badge {
    min-width: 1.75rem;
    height: 1.75rem;
    margin-inline-start: var(--space-2xs);
    color: var(--color-accent);
    background: var(--color-accent-soft);
    border: var(--rule-hair) solid var(--color-accent);
    border-radius: var(--radius-control);
    font-size: var(--text-xs);
    font-weight: 700;
  }

  .variant-nav {
    gap: var(--space-2xs);
  }

  .variant-nav button {
    width: 2.75rem;
    padding: 0;
    font-size: var(--text-lg);
  }

  .app-alert {
    width: min(100%, 70.1875rem);
    margin: 0;
    padding: var(--space-sm) var(--space-md);
    color: var(--color-warning);
    background: var(--color-warning-soft);
    border: var(--rule-hair) solid var(--color-warning);
    border-radius: var(--radius-control);
  }

  .grading-panel {
    width: min(100%, 70.1875rem);
    margin-top: var(--space-lg);
    padding: var(--space-lg);
    color: var(--color-ink);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-card);
    box-shadow: none;
  }

  .grading-list {
    gap: var(--space-xs);
    margin: var(--space-sm) 0;
  }

  .grading-chip {
    min-height: 2.75rem;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-sm);
    color: var(--color-ink-2);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-control);
  }

  .grading-chip:has(input:checked) {
    color: var(--color-error);
    background: var(--color-error-soft);
    border-color: var(--color-error);
  }

  .app-dialog {
    position: fixed;
    inset: 0;
    width: min(45rem, calc(100% - 2rem));
    max-height: min(86dvh, 48rem);
    height: fit-content;
    margin: auto;
    padding: var(--space-lg);
    color: var(--color-ink);
    background: var(--color-surface);
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-card);
    box-shadow: 0 var(--space-xs) var(--space-xl) var(--color-shadow);
  }

  .app-dialog-wide {
    width: min(68.75rem, calc(100% - 2rem));
  }

  .app-dialog::backdrop {
    background: var(--color-overlay);
  }

  .app-dialog[open] {
    animation: dialog-in var(--dur-long) var(--ease-out) both;
  }

  @keyframes dialog-in {
    from {
      opacity: 0;
      transform: scale(0.98);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }

  .app-dialog h2 {
    margin-bottom: var(--space-lg);
    font-family: var(--font-display);
    font-size: var(--text-xl);
    font-style: normal;
    line-height: 1.3;
  }

  .dialog-close {
    min-width: 2.75rem;
    min-height: 2.75rem;
    color: var(--color-ink);
    background: transparent;
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-control);
  }

  .admin-toolbar {
    grid-template-columns: 7.5rem minmax(0, 1fr) auto auto;
    gap: var(--space-xs);
    margin: var(--space-md) 0;
  }

  .admin-layout {
    grid-template-columns: minmax(17.5rem, 0.8fr) minmax(22.5rem, 1.2fr);
    gap: var(--space-lg);
  }

  .admin-list {
    border: var(--rule-hair) solid var(--color-rule-2);
    border-radius: var(--radius-control);
  }

  .admin-row {
    min-height: 2.75rem;
    padding: var(--space-sm);
    color: var(--color-ink);
    background: var(--color-surface);
    border-bottom: var(--rule-hair) solid var(--color-rule);
  }

  .admin-row small {
    color: var(--color-muted);
  }

  .validation-box,
  .quality-summary {
    margin: var(--space-sm) 0;
    padding: var(--space-sm);
    color: var(--color-ink-2);
    background: var(--color-paper-2);
    border: var(--rule-hair) solid var(--color-rule);
    border-radius: var(--radius-control);
  }

  .validation-box.valid {
    color: var(--color-success);
    background: var(--color-success-soft);
    border-color: var(--color-success);
  }

  .validation-box.invalid {
    color: var(--color-error);
    background: var(--color-error-soft);
    border-color: var(--color-error);
  }

  @media (hover: hover) and (pointer: fine) {
    select:hover,
    input[type='text']:hover,
    input[type='number']:hover,
    textarea:hover,
    .kanji-choice:hover {
      background: var(--color-paper);
    }

    .tab-btn:hover {
      color: var(--color-ink);
    }

    .btn:hover {
      color: var(--color-paper);
      background: var(--color-ink-2);
      transform: translateY(-1px);
    }

    .btn-secondary:hover,
    .btn-small:hover,
    .variant-nav button:hover,
    .dialog-close:hover,
    .section-toggle:hover,
    .admin-row:hover {
      color: var(--color-ink);
      background: var(--color-paper-3);
    }

    .btn-danger:hover {
      color: var(--color-surface);
      background: var(--color-error);
    }
  }
}

@media screen and (max-width: 100rem) {
  .main-container {
    display: flex;
    flex-direction: column;
    align-items: stretch;
  }

  .control-panel {
    position: static;
    width: min(100%, 47rem);
    max-width: 47rem;
    max-height: none;
    margin-inline: auto;
    overflow: visible;
  }

  .preview-container {
    width: 100%;
  }
}

@media screen and (max-width: 48rem) {
  .app-header {
    align-items: flex-start;
    gap: var(--space-sm);
    padding: var(--space-sm) var(--space-md);
  }

  .server-status__label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
    border: 0;
  }

  .main-container {
    gap: var(--space-xl);
    padding: var(--space-md);
  }

  .main-container .control-panel,
  .control-panel .card,
  .tab-container,
  .form-group,
  .form-row {
    width: 100%;
    min-width: 0;
    max-width: 100%;
  }

  select,
  input[type='text'],
  input[type='number'],
  textarea {
    min-width: 0;
    max-width: 100%;
  }

  .card,
  .control-panel > div:last-child,
  .grading-panel {
    padding: var(--space-md);
  }

  .control-panel > div:last-child,
  .form-row,
  .admin-layout,
  .admin-toolbar {
    grid-template-columns: minmax(0, 1fr);
  }

  .tab-btn {
    font-size: var(--text-xs);
  }

  .preview-header {
    flex-wrap: wrap;
  }

  .variant-nav {
    order: 3;
    width: 100%;
    margin-inline-start: 0;
  }

  .variant-nav button {
    flex: 1;
  }

  .app-dialog,
  .app-dialog-wide {
    width: calc(100% - 1rem);
    padding: var(--space-md);
  }
}

@media screen and (pointer: coarse) {
  .btn,
  .btn-small,
  .btn-danger,
  .tab-btn,
  .variant-nav button,
  .dialog-close,
  select,
  input[type='text'],
  input[type='number'] {
    min-height: 2.75rem;
  }
}

@media screen and (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 150ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 150ms !important;
  }

  .app-dialog[open] {
    animation-name: dialog-fade;
  }

  @keyframes dialog-fade {
    from { opacity: 0; }
    to { opacity: 1; }
  }
}
