:root {
  color-scheme: light;
  --theme-bg: #f1f5f9;
  --theme-surface: #ffffff;
  --theme-surface-muted: #f8fafc;
  --theme-text: #172033;
  --theme-text-muted: #526078;
  --theme-border: #cbd5e1;
  --theme-focus: #f59e0b;
  --theme-shadow: 0 8px 24px rgba(15, 23, 42, 0.1);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --theme-bg: #0f172a;
  --theme-surface: #1e293b;
  --theme-surface-muted: #273449;
  --theme-text: #f1f5f9;
  --theme-text-muted: #cbd5e1;
  --theme-border: #64748b;
  --theme-focus: #fbbf24;
  --theme-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);

  /* 既存画面で使われている主要なカスタムプロパティを共通テーマへ接続する。 */
  --bg: var(--theme-bg);
  --background: var(--theme-bg);
  --bg-main: var(--theme-bg);
  --bg-color: var(--theme-bg);
  --surface: var(--theme-surface);
  --card-bg: var(--theme-surface);
  --panel-bg: var(--theme-surface);
  --text: var(--theme-text);
  --text-main: var(--theme-text);
  --text-color: var(--theme-text);
  --muted: var(--theme-text-muted);
  --text-muted: var(--theme-text-muted);
  --border: var(--theme-border);
  --border-color: var(--theme-border);
}

html, body { min-height: 100%; }
body { transition: background-color 0.18s ease, color 0.18s ease; }
body > :is(header, .page-header):first-child { padding-right: 150px !important; }

.theme-toggle {
  position: fixed !important;
  z-index: 2147483646 !important;
  top: 12px !important;
  right: 14px !important;
  display: inline-flex !important;
  align-items: center !important;
  justify-content: center !important;
  gap: 7px !important;
  width: auto !important;
  min-width: 116px !important;
  min-height: 44px !important;
  margin: 0 !important;
  padding: 8px 13px !important;
  color: #172033 !important;
  background: rgba(255, 255, 255, 0.96) !important;
  border: 1px solid #94a3b8 !important;
  border-radius: 999px !important;
  box-shadow: 0 4px 16px rgba(15, 23, 42, 0.18) !important;
  font: 700 13px/1.2 'Noto Sans JP', system-ui, sans-serif !important;
  letter-spacing: 0 !important;
  text-transform: none !important;
  white-space: nowrap !important;
  cursor: pointer !important;
  opacity: 1 !important;
  transform: none !important;
}
.theme-toggle:hover { background: #f1f5f9 !important; border-color: #475569 !important; }
.theme-toggle:focus-visible { outline: 3px solid var(--theme-focus) !important; outline-offset: 3px !important; }
.theme-toggle__icon { font-size: 17px !important; line-height: 1 !important; }
.theme-toggle--offset { top: 82px !important; }

html[data-theme="dark"] .theme-toggle {
  color: #f8fafc !important;
  background: rgba(30, 41, 59, 0.97) !important;
  border-color: #94a3b8 !important;
  box-shadow: 0 5px 18px rgba(0, 0, 0, 0.45) !important;
}
html[data-theme="dark"] .theme-toggle:hover { background: #334155 !important; border-color: #cbd5e1 !important; }

html[data-theme="dark"] body {
  color: var(--theme-text) !important;
  background-color: var(--theme-bg) !important;
  background-image: none !important;
}
html[data-theme="dark"] :is(h1, h2, h3, h4, h5, h6, label, legend, .title, .card-title, .chart-title, .app-name) {
  color: var(--theme-text) !important;
}
html[data-theme="dark"] :is(p, .hint, .description, .subtitle, .field-note, .field-help, .empty, .muted, .app-desc) {
  color: var(--theme-text-muted);
}
html[data-theme="dark"] :is(
  .page-header, header, nav, .toolbar, .panel, .card, .app-card, .chart-card, .kpi-card,
  .summary-card, .api-card, .result-box, .drop-zone, .modal-content,
  .dialog, .settings-panel, .input-section, .output-section, .section,
  .tab-content, .table-container, .workspace, .controls, .control-panel,
  .phase-section, .sg-section, .sum-item
) {
  color: var(--theme-text) !important;
  background-color: var(--theme-surface) !important;
  border-color: var(--theme-border) !important;
  box-shadow: var(--theme-shadow);
}
html[data-theme="dark"] :is(input, textarea, select) {
  color: var(--theme-text) !important;
  background-color: #111827 !important;
  border-color: var(--theme-border) !important;
}
html[data-theme="dark"] :is(input, textarea)::placeholder { color: #94a3b8 !important; opacity: 1; }
html[data-theme="dark"] :is(table, th, td) { color: var(--theme-text); border-color: var(--theme-border) !important; }
html[data-theme="dark"] th { background-color: #273449 !important; }
html[data-theme="dark"] td { background-color: var(--theme-surface) !important; }
html[data-theme="dark"] :is(pre, code, .code, .preview, .output, .log) {
  color: #e2e8f0;
  background-color: #111827;
  border-color: var(--theme-border);
}
html[data-theme="dark"] hr { border-color: var(--theme-border); }
html[data-theme="dark"] a:not(.btn):not(.btn-back):not([class*="button"]) { color: #93c5fd; }
html[data-theme="dark"] img { color-scheme: light; }

@media (max-width: 640px) {
  body > :is(header, .page-header):first-child { padding-right: inherit !important; }
  .theme-toggle--offset { top: auto !important; }
  .theme-toggle {
    top: auto !important;
    right: 10px !important;
    bottom: 12px !important;
    min-width: 108px !important;
    min-height: 42px !important;
    padding: 7px 11px !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  body { transition: none; }
}

@media print {
  .theme-toggle { display: none !important; }
}
