:root {
  --progress-track: #dce7ef;
  --progress-fill: #167d85;
  --focus-ring: #38bdf8;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b121c;
  --panel: #15202d;
  --ink: #e7eef7;
  --muted: #9fb0c2;
  --line: #304155;
  --ice: #152535;
  --snow: #0f1824;
  --progress-track: #2a3a4c;
  --progress-fill: #49b6bd;
  --shadow: 0 18px 45px rgba(0, 0, 0, .28);
}

.portal-tools,
.start-tools {
  position: relative;
  z-index: 80;
}

.start-tools {
  position: fixed;
  top: 14px;
  right: 14px;
}

.portal-preferences {
  position: relative;
  color: var(--ink, #17324d);
}

.portal-preferences > summary {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  border: 1px solid rgba(255, 255, 255, .28);
  border-radius: 7px;
  padding: 6px 10px;
  color: #fff;
  background: rgba(255, 255, 255, .12);
  cursor: pointer;
  font-size: 13px;
  font-weight: 750;
  list-style: none;
  white-space: nowrap;
}

.start-tools .portal-preferences > summary {
  border-color: var(--line);
  color: var(--ink);
  background: color-mix(in srgb, var(--panel, #fff) 92%, transparent);
  box-shadow: 0 8px 24px rgba(23, 50, 77, .1);
}

.portal-preferences > summary::-webkit-details-marker { display: none; }

.portal-preferences > summary::after {
  content: "⌄";
  margin-left: 7px;
  font-size: 11px;
}

.portal-preferences[open] > summary::after { content: "⌃"; }

.portal-preferences-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  width: 230px;
  display: grid;
  gap: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  color: var(--ink);
  background: var(--panel, #fff);
  box-shadow: 0 18px 44px rgba(8, 20, 34, .22);
}

.portal-preferences-panel button,
.portal-import {
  width: 100%;
  min-height: 38px;
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 7px 8px;
  color: var(--ink);
  background: transparent;
  cursor: pointer;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  text-align: left;
}

.portal-preferences-panel button:hover,
.portal-import:hover {
  border-color: var(--line);
  background: color-mix(in srgb, var(--progress-fill) 10%, var(--panel));
}

.portal-import input { display: none; }

.pref-icon {
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  border-radius: 5px;
  color: var(--progress-fill);
  background: color-mix(in srgb, var(--progress-fill) 12%, transparent);
  font-size: 15px;
  font-weight: 900;
}

.portal-preferences-panel p {
  margin: 4px 4px 2px;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
}

.portal-toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 200;
  max-width: min(380px, calc(100% - 36px));
  transform: translateY(12px);
  border: 1px solid var(--line);
  border-left: 4px solid var(--progress-fill);
  border-radius: 8px;
  padding: 11px 14px;
  color: var(--ink);
  background: var(--panel, #fff);
  box-shadow: 0 18px 44px rgba(8, 20, 34, .22);
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s ease, transform .18s ease;
}

.portal-toast.visible { transform: translateY(0); opacity: 1; }
.portal-toast[data-tone="error"] { border-left-color: #dc2626; }
.portal-toast[data-tone="success"] { border-left-color: #16845b; }

html[data-theme="dark"] body {
  color: var(--ink);
  background: #0b121c;
}

html,
body {
  max-width: 100%;
  overflow-x: clip;
}

body.phase-open .learning-toolbar,
body.phase-open .phase-toolbar {
  position: sticky;
  top: 0;
  z-index: 35;
}

body.chapter-open .chapter-title {
  position: sticky;
  top: 44px;
  z-index: 35;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--line);
  padding: 7px 8px;
  background: color-mix(in srgb, var(--panel, #fff) 96%, transparent);
  backdrop-filter: blur(12px);
}

body.chapter-open .learning-goals {
  display: none;
}

html[data-theme="dark"] .topbar {
  border-color: #28394c;
  background: linear-gradient(90deg, rgba(10, 24, 39, .98), rgba(17, 49, 65, .96));
}

html[data-theme="dark"] :is(
  .hero-grid, .hero-panel, .summary-card, .map-stage, .journey-label, .module-card,
  .phase-window, .phase-toolbar, .learning-window, .learning-toolbar, .chapter-card,
  .asset-rail, .material-viewer, .company-card, .category-card, .asset-group,
  .progress-panel, .toolbar, .empty, .workspace-controls
) {
  color: var(--ink);
  border-color: var(--line) !important;
  background-color: var(--panel) !important;
  background-image: none !important;
}

html[data-theme="dark"] :is(.lead, .module-card p, .phase-title p, .chapter-card p, .asset-detail, .viewer-note, .status-line, .scan-status) {
  color: var(--muted) !important;
}

html[data-theme="dark"] :is(.search, input, select, textarea, .goal-pill, .chip, .badge, .tag, .reset-progress, .button:not(.primary)) {
  color: var(--ink);
  border-color: var(--line);
  background: #101a27;
}

html[data-theme="dark"] :is(.back-button, .band-banner, .open-link) {
  color: #b8dcf7 !important;
  border-color: #40566e !important;
  background: #17283a !important;
}

html[data-theme="dark"] .company-frame { background: #0b121c; }

.bar,
.chapter-progress-track,
.progress-track {
  position: relative;
  height: 8px !important;
  overflow: hidden !important;
  border-radius: 999px !important;
  background: var(--progress-track) !important;
  box-shadow: none !important;
}

.bar::before,
.bar span::after,
.chapter-progress-track::before,
.progress-track::before {
  display: none !important;
}

.bar span,
.chapter-progress-fill,
.progress-fill {
  position: absolute !important;
  inset: 0 auto 0 0 !important;
  width: var(--progress, 0%);
  height: 100% !important;
  border-radius: inherit !important;
  background: linear-gradient(90deg, var(--phase-color, var(--progress-fill)), var(--progress-fill)) !important;
  transition: width .22s ease !important;
}

.chapter-progress-track,
.progress-track { margin: 6px 0 !important; }

.progress-overview,
.portal-overall-progress {
  display: grid;
  gap: 7px;
  margin: 14px;
}

.progress-overview-head,
.portal-overall-progress .progress-line {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.progress-overview-head strong,
.portal-overall-progress .progress-line strong {
  color: var(--ink);
  font-size: 20px;
}

.portal-overall-progress p,
.progress-meta {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

html[data-theme="dark"] :is(
  .panel-head, .stat, .chapter-title, .asset-card, .viewer-head, .sidebar-header,
  .material-body, .chapter-viewer-head, .asset-rail-head, .material-viewer-head,
  .detail-head, .add-card
) {
  color: var(--ink) !important;
  border-color: var(--line) !important;
  background-color: var(--panel) !important;
  background-image: none !important;
}

@media (max-width: 780px) {
  .portal-preferences-panel {
    position: fixed;
    top: 58px;
    right: 10px;
    left: 10px;
    width: auto;
  }
  .start-tools { top: 8px; right: 8px; }
}

@media print {
  .portal-tools, .start-tools, .portal-toast { display: none !important; }
}
