/* Material Design 3 visual layer. Existing layout and behavior stay intact. */
body.material-theme {
  --md-primary: #006a60;
  --md-on-primary: #ffffff;
  --md-primary-container: #d8f3ee;
  --md-on-primary-container: #00201c;
  --md-secondary: #4a635f;
  --md-secondary-container: #e2f1ee;
  --md-on-secondary-container: #06201c;
  --md-tertiary: #8b4a5c;
  --md-tertiary-container: #ffd9e1;
  --md-on-tertiary-container: #3a0719;
  --md-error: #ba1a1a;
  --md-error-container: #ffdad6;
  --md-surface: #f8faf8;
  --md-surface-dim: #d8dbd9;
  --md-surface-container-lowest: #ffffff;
  --md-surface-container-low: #f2f5f3;
  --md-surface-container: #ecefed;
  --md-surface-container-high: #e6e9e7;
  --md-surface-container-highest: #e0e3e1;
  --md-on-surface: #191c1b;
  --md-on-surface-variant: #3f4946;
  --md-outline: #6f7976;
  --md-outline-variant: #bec9c5;
  --md-warning: #8a4f00;
  --md-warning-container: #ffddb8;
  --md-scrim: rgba(0, 0, 0, 0.42);
  --md-elevation-1: 0 1px 2px rgba(25, 28, 27, 0.22), 0 1px 3px 1px rgba(25, 28, 27, 0.1);
  --md-elevation-2: 0 2px 6px 2px rgba(25, 28, 27, 0.14), 0 1px 2px rgba(25, 28, 27, 0.18);
  --md-elevation-3: 0 4px 8px 3px rgba(25, 28, 27, 0.13), 0 1px 3px rgba(25, 28, 27, 0.2);
  --bg: var(--md-surface-container-low);
  --panel: var(--md-surface-container-lowest);
  --panel-soft: var(--md-secondary-container);
  --line: var(--md-outline-variant);
  --text: var(--md-on-surface);
  --muted: var(--md-on-surface-variant);
  --accent: var(--md-primary);
  --accent-strong: #005048;
  --warn: var(--md-warning);
  --danger: var(--md-error);
  --shadow: var(--md-elevation-1);
  margin: 0;
  background: var(--md-surface-container-low);
  color: var(--md-on-surface);
  font-family: Roboto, "Yu Gothic UI", "Meiryo", system-ui, sans-serif;
  font-size: 14px;
}

body.material-theme * {
  scrollbar-color: var(--md-outline-variant) transparent;
}

body.material-theme button,
body.material-theme input,
body.material-theme select,
body.material-theme textarea {
  letter-spacing: 0;
}

body.material-theme :where(button, a, input, select, textarea, summary):focus-visible {
  outline: 3px solid color-mix(in srgb, var(--md-primary) 35%, transparent);
  outline-offset: 2px;
}

/* Top app bar and navigation */
body.material-theme .topbar {
  min-height: 76px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--md-outline-variant);
  background: var(--md-surface-container-lowest);
  color: var(--md-on-surface);
  box-shadow: var(--md-elevation-2);
}

body.material-theme .topbar h1 {
  color: var(--md-on-surface);
  font-size: 22px;
  font-weight: 700;
}

body.material-theme .topbar p {
  color: var(--md-on-surface-variant);
}

body.material-theme .top-nav {
  gap: 4px;
  padding: 4px;
  border-radius: 8px;
  background: var(--md-surface-container);
}

body.material-theme .top-link {
  min-width: 88px;
  height: 38px;
  padding: 0 14px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--md-on-surface-variant);
  box-shadow: none;
  font-size: 13px;
  font-weight: 600;
}

body.material-theme .top-link:hover {
  background: color-mix(in srgb, var(--md-on-surface) 8%, transparent);
  color: var(--md-on-surface);
}

body.material-theme .top-link.disabled,
body.material-theme .top-link.disabled:hover {
  background: transparent;
  color: var(--md-on-surface-variant);
  box-shadow: none;
  opacity: 0.42;
}

body.material-theme .top-link.active {
  position: relative;
  background: var(--md-primary-container);
  color: var(--md-on-primary-container);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--md-primary) 52%, transparent);
  font-weight: 700;
}

body.material-theme .top-link.active::after {
  content: "";
  position: absolute;
  right: 10px;
  bottom: 2px;
  left: 10px;
  height: 3px;
  border-radius: 2px;
  background: var(--md-primary);
}

/* Material buttons */
body.material-theme .primary-button,
body.material-theme .run-actions .primary-button {
  min-height: 40px;
  border: 0;
  border-radius: 20px;
  padding: 0 20px;
  background: var(--md-primary);
  color: var(--md-on-primary);
  box-shadow: var(--md-elevation-1);
  font-weight: 600;
}

body.material-theme .primary-button:hover,
body.material-theme .run-actions .primary-button:hover {
  background: color-mix(in srgb, var(--md-primary) 92%, black);
  box-shadow: var(--md-elevation-2);
}

body.material-theme .secondary-button {
  min-height: 40px;
  border: 1px solid var(--md-outline);
  border-radius: 20px;
  padding: 0 18px;
  background: transparent;
  color: var(--md-primary);
}

body.material-theme :where(.ghost-button, .formation-tab, .library-tab, .filter-button, .catalog-filter-icon, .rotation-clear) {
  border: 0;
  border-radius: 18px;
  background: var(--md-surface-container-high);
  color: var(--md-on-surface-variant);
  box-shadow: none;
  font-weight: 600;
}

body.material-theme :where(.ghost-button, .formation-tab, .library-tab, .filter-button, .catalog-filter-icon, .rotation-clear):hover {
  background: color-mix(in srgb, var(--md-on-surface) 12%, var(--md-surface-container-high));
  color: var(--md-on-surface);
}

body.material-theme :where(.ghost-button, .formation-tab, .library-tab, .filter-button, .catalog-filter-icon).active {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
  box-shadow: inset 0 0 0 1px var(--md-primary);
}

body.material-theme :where(.danger-button, .slot-remove, button.danger) {
  border: 1px solid var(--md-error);
  border-radius: 18px;
  background: var(--md-surface-container-lowest);
  color: var(--md-error);
  box-shadow: none;
}

body.material-theme :where(.danger-button, .slot-remove, button.danger):hover {
  background: var(--md-error-container);
  color: var(--md-error);
}

body.material-theme .icon-button {
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--md-on-surface-variant);
}

body.material-theme .icon-button:hover {
  background: color-mix(in srgb, var(--md-on-surface) 10%, transparent);
}

body.material-theme button:disabled {
  background: color-mix(in srgb, var(--md-on-surface) 8%, var(--md-surface));
  color: color-mix(in srgb, var(--md-on-surface) 38%, transparent);
  box-shadow: none;
}

/* Outlined fields */
body.material-theme :where(input:not([type="checkbox"]):not([type="radio"]):not([type="color"]), select, textarea) {
  border: 1px solid var(--md-outline);
  border-radius: 4px;
  background: var(--md-surface-container-lowest);
  color: var(--md-on-surface);
  box-shadow: none;
}

body.material-theme :where(input:not([type="checkbox"]):not([type="radio"]):not([type="color"]), select, textarea):hover {
  border-color: var(--md-on-surface);
}

body.material-theme :where(input:not([type="checkbox"]):not([type="radio"]):not([type="color"]), select, textarea):focus {
  border-color: var(--md-primary);
  box-shadow: inset 0 0 0 1px var(--md-primary);
  outline: 0;
}

body.material-theme :where(input[type="checkbox"], input[type="radio"]) {
  accent-color: var(--md-primary);
}

body.material-theme :where(label, .slot-meta, .b3-candidate-meta span, .b3-formation-title span) {
  color: var(--md-on-surface-variant);
}

/* Main surfaces */
body.material-theme :where(.app-shell, .b3-shell, .training-profile-shell) {
  gap: 12px;
  padding: 12px;
  background: var(--md-surface-container-low);
}

body.material-theme :where(
  .library-pane,
  .setup-pane,
  .results-pane,
  .b3-library-pane,
  .b3-formation-pane,
  .b3-results-pane,
  .training-common-pane,
  .training-character-pane,
  .training-editor-pane
) {
  border: 1px solid var(--md-outline-variant);
  border-radius: 8px;
  background: var(--md-surface-container-lowest);
  box-shadow: var(--md-elevation-1);
}

body.material-theme .pane-head {
  min-height: 56px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--md-outline-variant);
  background: var(--md-surface-container-lowest);
}

body.material-theme .pane-head > h2 {
  flex: 0 0 auto;
  white-space: nowrap;
}

body.material-theme .setup-pane > .pane-head {
  align-items: flex-start;
  flex-wrap: wrap;
}

body.material-theme .setup-pane > .pane-head .formation-actions {
  flex: 1 1 300px;
  justify-content: flex-end;
  flex-wrap: wrap;
}

body.material-theme .setup-pane > .formation-pane-head .formation-actions {
  justify-content: flex-start;
}

body.material-theme .setup-pane > .pane-head .formation-actions > :where(.ghost-button, .primary-button, .profile-toggle) {
  min-height: 34px;
  padding-inline: 10px;
  font-size: 12px;
}

body.material-theme .training-character-pane > .pane-head {
  align-items: center;
  flex-wrap: wrap;
}

body.material-theme .training-character-pane > .pane-head #profileSearchInput {
  flex: 1 1 170px;
  width: auto;
  min-width: 140px;
}

body.material-theme :where(.pane-head h2, .options-band h2, .b3-formation-detail h3) {
  color: var(--md-on-surface);
  font-weight: 600;
}

body.material-theme .library-tabs {
  gap: 0;
  padding: 0 12px;
  border-bottom: 1px solid var(--md-outline-variant);
  background: var(--md-surface-container-lowest);
}

body.material-theme .library-tab {
  min-height: 48px;
  border: 0;
  border-bottom: 3px solid transparent;
  border-radius: 0;
  background: transparent;
}

body.material-theme .library-tab.active {
  border-bottom-color: var(--md-primary);
  background: transparent;
  color: var(--md-primary);
  box-shadow: none;
}

body.material-theme .filter-panel-head,
body.material-theme .catalog-option-row {
  border-color: var(--md-outline-variant);
  background: var(--md-surface-container-low);
}

body.material-theme .filter-panel-toggle {
  color: var(--md-primary);
}

/* Formation, character, and result cards */
body.material-theme :where(
  .slot,
  .saved-folder-card,
  .saved-formation-card,
  .b3-candidate,
  .b3-formation-card,
  .b3-result-row,
  .training-character-row
) {
  border: 1px solid var(--md-outline-variant);
  border-radius: 8px;
  background: var(--md-surface-container-lowest);
  box-shadow: none;
}

body.material-theme :where(.slot, .b3-candidate, .training-character-row) {
  transition: background-color 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}

body.material-theme :where(.slot, .b3-candidate, .training-character-row):hover {
  border-color: var(--md-outline);
  background: var(--md-surface-container-low);
  box-shadow: var(--md-elevation-1);
}

body.material-theme :where(.slot.active, .b3-formation-card.expanded, .training-character-row.active, .saved-folder-card.active) {
  border-color: var(--md-primary);
  background: var(--md-primary-container);
  color: var(--md-on-primary-container);
  box-shadow: 0 0 0 1px var(--md-primary);
}

body.material-theme .slot-index {
  border-radius: 6px;
  background: var(--md-primary);
  color: var(--md-on-primary);
}

body.material-theme :where(.slot-icon, .saved-formation-icon, .b3-result-thumb) {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}

body.material-theme .formation-rotation-panel,
body.material-theme .status-section,
body.material-theme .options-band,
body.material-theme .slot-status-panel {
  border: 1px solid var(--md-outline-variant);
  border-radius: 8px;
  background: var(--md-surface-container-lowest);
  box-shadow: none;
}

body.material-theme .status-section {
  gap: 10px;
  padding: 0 12px 12px;
  overflow: hidden;
}

body.material-theme .status-section-head {
  min-height: 44px;
  margin: 0 -12px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--md-outline-variant);
}

body.material-theme .status-section > h3:first-child {
  margin: 0;
  padding-top: 12px;
}

body.material-theme .rotation-heading,
body.material-theme .status-section-head {
  background: var(--md-surface-container-low);
}

body.material-theme .rotation-stage-head {
  border-color: var(--md-outline-variant);
  background: var(--md-secondary-container);
}

body.material-theme .rotation-sequence {
  border-color: var(--md-outline-variant);
  background: var(--md-surface-container-lowest);
}

body.material-theme .rotation-candidates {
  border-color: color-mix(in srgb, var(--md-primary) 45%, var(--md-outline-variant));
  background: color-mix(in srgb, var(--md-primary-container) 35%, white);
}

body.material-theme :where(.rotation-chip, .rotation-add) {
  border: 1px solid var(--md-outline-variant);
  border-radius: 6px;
  background: var(--md-surface-container-lowest);
}

body.material-theme .rotation-chip:hover {
  border-color: var(--md-error);
  background: var(--md-error-container);
}

body.material-theme .rotation-add:hover {
  border-color: var(--md-primary);
  background: var(--md-primary-container);
}

/* Material expansion panels */
body.material-theme .setup-accordion {
  border: 1px solid var(--md-outline-variant);
  border-radius: 8px;
  background: var(--md-surface-container-lowest);
  box-shadow: none;
}

body.material-theme .setup-accordion > summary {
  min-height: 48px;
  padding: 0 16px;
  color: var(--md-on-surface);
  font-weight: 600;
}

body.material-theme .setup-accordion > summary:hover {
  background: color-mix(in srgb, var(--md-on-surface) 6%, transparent);
}

body.material-theme .setup-accordion[open] > summary {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}

body.material-theme .unified-accordion {
  border: 1px solid var(--md-outline-variant);
  border-left: 6px solid var(--md-primary);
  box-shadow: var(--md-elevation-1);
}

body.material-theme .unified-accordion > summary,
body.material-theme .unified-accordion[open] > summary {
  min-height: 56px;
  background: var(--md-surface-container-lowest);
  color: var(--md-on-surface);
  font-size: 15px;
  font-weight: 800;
}

body.material-theme .unified-accordion[open] > summary {
  background: var(--md-surface-container-high);
}

body.material-theme .unified-accordion > summary:hover {
  background: var(--md-surface-container);
}

body.material-theme .setup-accordion > summary::after {
  content: ">";
  color: currentColor;
  transform: rotate(90deg);
  transition: transform 120ms ease;
}

body.material-theme .setup-accordion[open] > summary::after {
  content: ">";
  transform: rotate(-90deg);
}

body.material-theme .unified-accordion > summary::after {
  content: ">";
  color: currentColor;
  transform: rotate(90deg);
  transition: transform 120ms ease;
}

body.material-theme .unified-accordion[open] > summary::after {
  content: ">";
  transform: rotate(-90deg);
}

body.material-theme .unified-filter-accordion {
  border-color: var(--md-outline-variant);
  border-left-color: var(--md-primary);
  background: var(--md-surface-container-lowest);
  box-shadow: var(--md-elevation-1);
}

body.material-theme .unified-filter-accordion .filter-panel-toggle,
body.material-theme .unified-filter-accordion .filter-panel-toggle.active {
  background: var(--md-surface-container-lowest);
  color: var(--md-on-surface);
}

body.material-theme .unified-filter-accordion .filter-panel-toggle:hover {
  background: var(--md-surface-container);
}

body.material-theme .unified-filter-accordion .filter-panel-toggle[aria-expanded="true"] {
  background: var(--md-surface-container-high);
}

body.material-theme .setup-accordion-body {
  border-top-color: var(--md-outline-variant);
  background: var(--md-surface-container-lowest);
}

body.material-theme .option-checkbox-control,
body.material-theme .range-bonus-options .check-label,
body.material-theme .research-grid > div {
  border-color: var(--md-outline-variant);
  background: var(--md-surface-container-low);
}

/* Chips, badges, switches, and status */
body.material-theme :where(.result-status, .rotation-title strong) {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}

body.material-theme .profile-toggle {
  border-color: var(--md-outline);
  background: var(--md-surface-container-lowest);
}

body.material-theme .profile-toggle-track {
  background: var(--md-surface-container-highest);
}

body.material-theme .profile-toggle.active {
  border-color: var(--md-primary);
  background: var(--md-primary-container);
  color: var(--md-on-primary-container);
}

body.material-theme .profile-toggle.active .profile-toggle-track {
  background: var(--md-primary);
}

body.material-theme .b3-result-damage,
body.material-theme .b3-formation-damage,
body.material-theme .formation-total-damage {
  color: var(--md-primary);
  font-variant-numeric: tabular-nums;
}

body.material-theme .b3-result-damage {
  border-radius: 6px;
  background-color: var(--md-surface-container-low);
}

/* Dialogs */
body.material-theme .b3-result-modal,
body.material-theme .modal,
body.material-theme .formation-picker-overlay {
  background: var(--md-scrim);
}

body.material-theme :where(.b3-result-modal-panel, .detail-panel, .formation-picker-modal) {
  border: 0;
  border-radius: 8px;
  background: var(--md-surface-container-lowest);
  box-shadow: var(--md-elevation-3);
}

body.material-theme :where(.b3-result-modal-head, .detail-head, .formation-picker-modal > header) {
  border-bottom: 1px solid var(--md-outline-variant);
  background: var(--md-surface-container-low);
}

@media (max-width: 900px) {
  body.material-theme .topbar {
    padding: 10px 12px;
  }

  body.material-theme .top-nav {
    width: 100%;
    overflow-x: auto;
    flex-wrap: nowrap;
  }

  body.material-theme .top-link {
    flex: 0 0 auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  body.material-theme *,
  body.material-theme *::before,
  body.material-theme *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
