/*
 * Study plan styles.
 *
 * Hand-written, no build step. Encapsulated against the host page's stylesheet:
 * every selector is scoped under .studyplan-wrapper and prefixed with "studyplan-",
 * and the reset rules further down undo what host stylesheets (Tailwind Preflight, global heading
 * and paragraph rules) do to the elements this extension uses. No cascade layers - specificity is
 * kept flat on purpose (except the heading colour rule), as only tokens are meant to be overridden.
 *
 * The only supported customization surface is this set of public custom properties, set by a site
 * on .studyplan-wrapper or any ancestor (README.md, "Customising the look"):
 *
 *   --studyplan-color-ink, --studyplan-color-accent, --studyplan-color-accent-subtle,
 *   --studyplan-color-surface, --studyplan-appearance-<appearance>-color (filled, dashed, dotted,
 *   filled-dark, double-border, hatched), --studyplan-radius, --studyplan-radius-pill,
 *   --studyplan-panel-z-index
 *
 * This file only reads them, each with its default as the var() fallback, and never declares one:
 * a site's rule then wins regardless of load order, specificity or cascade layer. Everything named
 * --_studyplan-* is internal, set only by the extension, and may change in any release.
 */

/* A <length> so a bare 0 still works in calc()/min() and a typo falls back; initial-value may not use
   rem, so 4px stands for the 0.25rem default. */
@property --_studyplan-radius {
    syntax: "<length>";
    inherits: true;
    initial-value: 4px;
}

@property --_studyplan-radius-pill {
    syntax: "<length>";
    inherits: true;
    initial-value: 999px;
}

.studyplan-wrapper {
    --_studyplan-color-ink: var(--studyplan-color-ink, #1f2937);
    --_studyplan-color-accent: var(--studyplan-color-accent, #1d4ed8);
    --_studyplan-color-surface: var(--studyplan-color-surface, #ffffff);
    --_studyplan-color-accent-subtle: var(
        --studyplan-color-accent-subtle,
        color-mix(in srgb, var(--_studyplan-color-accent) 10%, var(--_studyplan-color-surface))
    );
    --_studyplan-radius: var(--studyplan-radius, 0.25rem);
    --_studyplan-radius-pill: var(--studyplan-radius-pill, 999px);

    /* Derived tones. They reach WCAG AA as long as the palette, light or dark, meets the conditions
       in the README, for example ink >= 13:1 against a light surface. */
    --_studyplan-color-ink-muted: color-mix(in srgb, var(--_studyplan-color-ink) 72%, transparent);
    --_studyplan-color-surface-muted: color-mix(in srgb, var(--_studyplan-color-ink) 8%, var(--_studyplan-color-surface));
    --_studyplan-color-border-subtle: color-mix(in srgb, var(--_studyplan-color-ink) 17%, var(--_studyplan-color-surface));

    /* Borders that identify a control as such need >=3:1 (WCAG 1.4.11); border-subtle is only for
       decorative frames and dividers. */
    --_studyplan-color-border: color-mix(in srgb, var(--_studyplan-color-ink) 52%, var(--_studyplan-color-surface));

    /* Text on the strong fills (section bar on accent, pressed plan switcher on ink, filled-dark
       tile): the surface colour, so its contrast is that of the fill against the surface. */
    --_studyplan-color-on-strong: var(--_studyplan-color-surface);

    /* Backdrop and panel shadow. Refined below so that a light ink still gives a dark shade. */
    --_studyplan-color-shade: color-mix(in srgb, var(--_studyplan-color-ink) 72%, #000000);

    --_studyplan-radius-control: calc(var(--_studyplan-radius) * 1.5);
    --_studyplan-focus-outline: 3px solid var(--_studyplan-color-accent);

    /* Tile state, reassigned per appearance further down. */
    --_studyplan-tile-title-color: var(--_studyplan-color-ink);
    --_studyplan-tile-meta-color: var(--_studyplan-color-ink-muted);
    --_studyplan-tile-hint-color: var(--_studyplan-color-accent);
    --_studyplan-credits-bg: color-mix(in srgb, var(--_studyplan-color-ink) 8%, transparent);

    --_studyplan-row-gap: 0.5rem;
    --_studyplan-tile-gap: 0.5rem;
    --_studyplan-tile-basis: 8.125rem;
    --_studyplan-tile-border-width: 2px;
    --_studyplan-hatch-size: 0.375rem;

    font-size: 1rem;
    font-weight: 400;
    line-height: 1.5;
    color: var(--_studyplan-color-ink);

    /* The plan never sits on the page background, so a dark page cannot undercut its contrast. */
    background-color: var(--_studyplan-color-surface);
}

/* A dark palette has a light ink, which the mix above would only lighten. Mirroring its lightness
   in oklch makes it near-black; a dark ink (lightness below 0.5) passes unchanged. */
@supports (color: oklch(from #000000 min(l, 1 - l) c h)) {
    .studyplan-wrapper {
        --_studyplan-color-shade: color-mix(in srgb, oklch(from var(--_studyplan-color-ink) min(l, 1 - l) c h) 72%, #000000);
    }
}

/* Defensive local reset for the elements this extension renders itself. Wrapped in :where() so
   it carries no more specificity than a single class - otherwise it would beat the single-class
   rules further down regardless of source order. Attached content elements are host markup and
   keep the host's own styles. */
.studyplan-wrapper :where(button):where(:not(.studyplan-panel-content-elements *)) {
    margin: 0;
    padding: 0;
    border: 0;
    background: none;
    font: inherit;
    color: inherit;
    text-align: inherit;
    cursor: pointer;
    box-sizing: border-box;
}

.studyplan-wrapper :where(h1, h2, h3, h4, h5, h6):where(:not(.studyplan-panel-content-elements *)) {
    margin: 0;
    font-family: inherit;
    font-size: inherit;
    font-weight: 700;
    line-height: inherit;
    letter-spacing: normal;
    text-transform: none;
}

/* (0,1,1) so host dark-mode rules at class specificity, such as `:is(h2, .h2):where(.dark *)`, cannot
   recolour the plan's headings; attached content elements keep the host's styles. */
.studyplan-wrapper :is(h1, h2, h3, h4, h5, h6):where(:not(.studyplan-panel-content-elements *)) {
    color: var(--_studyplan-color-ink);
}

.studyplan-wrapper :where(p, figure, fieldset, legend):where(:not(.studyplan-panel-content-elements *)) {
    margin: 0;
    font-size: inherit;
    line-height: inherit;
}

/* Host pages commonly space text with rules like p:not(:last-child) or
   :is(h2, .h2):not(:last-child), which outrank a single class. This one outranks both, so
   spacing in here is always set as margin-top or gap, never as a text block's margin-bottom. */
.studyplan-wrapper :is(p, h1, h2, h3, h4, h5, h6):not(:last-child):where(:not(.studyplan-panel-content-elements *)) {
    margin-bottom: 0;
}

.studyplan-wrapper :where(ul, ol):where(:not(.studyplan-panel-content-elements *)) {
    margin: 0;
    padding: 0;
    list-style: none;
}

.studyplan-wrapper *,
.studyplan-wrapper *::before,
.studyplan-wrapper *::after {
    box-sizing: border-box;
}

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

/* Needed only without a CSS framework: an author display rule beats the user-agent [hidden]
   rule, so `hidden` alone would not hide these. */
.studyplan-row[hidden],
.studyplan-block[hidden],
.studyplan-grid[hidden],
.studyplan-tiles[hidden],
.studyplan-side-column[hidden],
.studyplan-tile[hidden],
.studyplan-panel[hidden],
.studyplan-panel-badge[hidden] {
    display: none;
}

/* ---------- Plan switcher ---------- */

.studyplan-plan-switcher {
    margin: 0 0 0.75rem 0;
}

.studyplan-plan-switcher-list {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    padding: 0.25rem;
    background-color: var(--_studyplan-color-surface-muted);
    border-radius: var(--_studyplan-radius-control);
}

/* Nested in the switcher's padded frame, so its corners follow that frame's radius. */
.studyplan-plan-switcher-toggle {
    padding: 0.4375rem 0.8125rem;
    border-radius: max(0px, calc(var(--_studyplan-radius-control) - 0.125rem));
    font-size: 0.8125rem;
    font-weight: 600;
    line-height: 1.15;
    color: var(--_studyplan-color-ink);
}

.studyplan-plan-switcher-toggle:hover {
    background-color: color-mix(in srgb, var(--_studyplan-color-ink) 8%, transparent);
}

.studyplan-plan-switcher-toggle[aria-pressed="true"] {
    background-color: var(--_studyplan-color-ink);
    color: var(--_studyplan-color-on-strong);
}

.studyplan-plan-switcher-toggle:focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

/* ---------- Toolbar: legend/filter and compact-view toggle ---------- */

.studyplan-toolbar {
    display: flex;
    align-items: stretch;
    gap: 0.5rem;
    margin: 0 0 0.625rem 0;
}

.studyplan-legend-group {
    flex: 1 1 auto;
    padding: 0.5rem;
    border: 1px solid var(--_studyplan-color-border-subtle);
    border-radius: var(--_studyplan-radius-control);
}

.studyplan-legend {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
}

.studyplan-legend-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-height: 1.5rem;
    padding: 0.375rem 0.75rem;
    border: 1px solid var(--_studyplan-color-border);
    border-radius: var(--_studyplan-radius-pill);
    background-color: var(--_studyplan-color-surface);
    font-size: 0.8125rem;
    line-height: 1.2;
    color: var(--_studyplan-color-ink-muted);
}

.studyplan-legend-toggle:hover {
    border-color: var(--_studyplan-color-accent);
    background-color: color-mix(in srgb, var(--_studyplan-color-ink) 6%, var(--_studyplan-color-surface));
}

.studyplan-legend-toggle[aria-pressed="true"] {
    border-color: var(--_studyplan-color-accent);
    background-color: var(--_studyplan-color-accent-subtle);
    color: var(--_studyplan-color-ink);
    font-weight: 700;
}

.studyplan-legend-toggle:focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

/* A miniature tile: the appearance classes further down style it like the tiles, and its
   corners stay square whenever the tiles are. */
.studyplan-legend-swatch {
    --_studyplan-hatch-size: 0.1875rem;

    flex: none;
    width: 0.875rem;
    height: 0.875rem;
    border: var(--_studyplan-tile-border-width) solid var(--_studyplan-color-border);
    border-radius: min(2px, var(--_studyplan-radius));
    background-color: var(--_studyplan-color-surface);
}

.studyplan-view-toggle {
    display: flex;
    margin-left: auto;
}

.studyplan-view-toggle-button {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--_studyplan-color-border);
    border-radius: var(--_studyplan-radius-control);
    background-color: var(--_studyplan-color-surface);
    font-size: 0.8125rem;
    color: var(--_studyplan-color-ink-muted);
    white-space: nowrap;
}

.studyplan-view-toggle-button:hover {
    background-color: var(--_studyplan-color-surface-muted);
}

/* Label and icon stay constant, so this is the sighted counterpart to aria-pressed. */
.studyplan-view-toggle-button[aria-pressed="true"] {
    border-color: var(--_studyplan-color-accent);
    background-color: var(--_studyplan-color-accent-subtle);
    color: var(--_studyplan-color-ink);
    font-weight: 600;
}

.studyplan-view-toggle-button:focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

.studyplan-view-toggle-icon {
    display: inline-flex;
    flex: none;
    width: 1.0625rem;
    height: 1.0625rem;
}

.studyplan-view-toggle-icon svg {
    width: 100%;
    height: 100%;
}

.studyplan-filter-status-row {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.25rem;
    min-height: 1rem;
    margin: 0 0 1.25rem 0;
    font-size: 0.75rem;
    color: var(--_studyplan-color-ink-muted);
}

.studyplan-filter-reset {
    font-weight: 700;
    color: var(--_studyplan-color-accent);
    text-decoration: underline;
}

.studyplan-filter-reset:focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

.studyplan-empty-state {
    padding: 1.5rem;
    border: 1px dashed var(--_studyplan-color-border-subtle);
    border-radius: var(--_studyplan-radius-control);
    text-align: center;
    font-size: 0.875rem;
    color: var(--_studyplan-color-ink-muted);
}

/* Compact view: removed from layout and accessibility tree alike, so screen readers stop
   announcing what is no longer shown. The state lives on the grid container. */
.studyplan-grid--compact .studyplan-compact-hide {
    display: none;
}

/* ---------- Plan grid ---------- */

.studyplan-grid {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.studyplan-block {
    display: flex;
    flex-direction: column;
    gap: var(--_studyplan-row-gap);
}

.studyplan-block-heading {
    padding: 0.5625rem 0.75rem;
    font-weight: 700;
    line-height: 1.15;
    border-radius: var(--_studyplan-radius);
    background-color: var(--_studyplan-color-accent);
    color: var(--_studyplan-color-on-strong);
    text-align: center;
    font-size: 0.875rem;
    letter-spacing: 0.03em;
}

.studyplan-row {
    display: flex;
    align-items: stretch;
    gap: 0.625rem;
}

/* The semester number is never rendered in an accent colour, in any row - a deliberate
   default, not a placeholder awaiting a customer override. */
.studyplan-semester-label {
    display: flex;
    flex: 0 0 2.375rem;
    font-weight: 700;
    align-items: center;
    justify-content: center;
    min-width: 0;
    border-radius: var(--_studyplan-radius);
    background-color: var(--_studyplan-color-surface-muted);
    color: var(--_studyplan-color-ink);
    text-align: center;
}

.studyplan-semester-number {
    font-size: 1rem;
}

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

/* A free-text label ("Fall term") needs more room than a digit; hyphens need the page lang. */
.studyplan-row--semester-label .studyplan-semester-label {
    flex-basis: 4.875rem;
    padding: 0.375rem 0.25rem;
}

.studyplan-row--semester-label .studyplan-semester-number {
    font-size: 0.75rem;
    line-height: 1.25;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    hyphens: auto;
    overflow-wrap: anywhere;
}

/* Tile grid and side column are two siblings here, never both inside .studyplan-tiles: as a
   further tile the column would wrap into a second row once a semester is full. */
.studyplan-row-body {
    display: flex;
    flex: 1 1 0%;
    align-items: stretch;
    gap: var(--_studyplan-tile-gap);
    min-width: 0;
}

.studyplan-tiles {
    display: flex;
    flex: 1 1 auto;
    flex-wrap: wrap;
    align-items: stretch;
    gap: var(--_studyplan-tile-gap);
    min-width: 0;
}

/* Smaller than the default tile basis so the tile grid next to a side column stays one row
   instead of wrapping. */
.studyplan-tiles--reduced {
    --_studyplan-tile-basis: 6.5rem;
}

/* Width comes from the category's side column width field, set inline per row. */
.studyplan-side-column {
    display: flex;
    flex-direction: column;
    flex: 0 0 var(--_studyplan-side-column-width, 30%);
    gap: var(--_studyplan-tile-gap);
}

/* ---------- Tile ---------- */

/* A column flexbox starting at the top: titles line up along the top edge and the meta line
   is pushed to the bottom edge, so every row reads as two straight lines. */
.studyplan-tile {
    position: relative;
    display: flex;
    flex: 1 1 var(--_studyplan-tile-basis);
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    min-width: 0;
    padding: 0.5625rem 0.75rem;
    border: var(--_studyplan-tile-border-width) solid var(--_studyplan-color-border);
    border-radius: var(--_studyplan-radius);
    background-color: var(--_studyplan-color-surface);
    text-align: left;
}

.studyplan-tile:hover {
    filter: brightness(0.98);
    box-shadow: 0 2px 8px color-mix(in srgb, var(--_studyplan-color-ink) 12%, transparent);
}

.studyplan-tile:focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

.studyplan-tile-title {
    display: block;
    margin-bottom: 0.25rem;
    padding-right: 1rem;
    font-size: 0.8125rem;
    font-weight: 700;
    line-height: 1.3;
    color: var(--_studyplan-tile-title-color);
    hyphens: auto;

    /* "anywhere", not "break-word": only "anywhere" counts towards min-content size, so a
       single long unbreakable title cannot push the row into horizontal scroll. */
    overflow-wrap: anywhere;
}

.studyplan-tile-meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.25rem 0.625rem;
    margin-top: auto;
    padding-top: 0.25rem;
}

.studyplan-tile-category {
    display: flex;
    align-items: center;
    gap: 0.3125rem;
    min-width: 0;
    font-size: 0.6875rem;
    line-height: 1.3;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    color: var(--_studyplan-tile-meta-color);
    hyphens: auto;
    overflow-wrap: break-word;
}

/* Mirrors the tile: appearances with an unfilled tile set a hollow marker in their line style. */
.studyplan-tile-category-marker {
    flex: none;
    width: 7px;
    height: 7px;
    border: 2px var(--_studyplan-marker-line, solid) var(--_studyplan-marker-color, var(--_studyplan-appearance-color, currentColor));
    border-radius: min(2px, var(--_studyplan-radius));
    background-color: var(--_studyplan-marker-fill, var(--_studyplan-marker-color, var(--_studyplan-appearance-color, currentColor)));
}

.studyplan-tile-credits {
    flex: none;
    padding: 0.125rem 0.5rem;
    border-radius: var(--_studyplan-radius-pill);
    background-color: var(--_studyplan-credits-bg);
    font-size: 0.65625rem;
    font-weight: 700;
    line-height: 1.3;
    letter-spacing: 0.02em;
    color: var(--_studyplan-tile-title-color);
    white-space: nowrap;
}

/* Drawn rather than typed: a "›" glyph takes its size and weight from the host font. */
.studyplan-tile-open-hint {
    position: absolute;
    top: 0.8125rem;
    right: 0.625rem;
    display: flex;
    width: 0.25rem;
    height: 0.375rem;
    color: var(--_studyplan-tile-hint-color);
}

.studyplan-tile-open-hint svg {
    width: 100%;
    height: 100%;
}

/* The side column repeats the same statement in every row of a phase: it marks the row rather
   than carrying information, so it is set smaller and keeps its meta for screen readers only. */
.studyplan-tile--side-column {
    flex: 1 1 auto;
    padding: 0.5625rem 0.5rem;
}

.studyplan-tile--side-column .studyplan-tile-title {
    font-size: 0.75rem;
}

.studyplan-tile--side-column .studyplan-tile-meta {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ---------- Appearances (tiles and legend swatches) ---------- */


/* Every appearance differs from the others in line style or fill pattern, never in colour alone
   (WCAG 1.4.1). Its colour comes from a public token; four follow the palette by default. */
.studyplan-appearance--filled {
    --_studyplan-appearance-color: var(--studyplan-appearance-filled-color, var(--_studyplan-color-accent));

    border-style: solid;
    border-color: var(--_studyplan-appearance-color);
    background-color: color-mix(in srgb, var(--_studyplan-appearance-color) 10%, var(--_studyplan-color-surface));
}

.studyplan-appearance--dashed {
    --_studyplan-appearance-color: var(--studyplan-appearance-dashed-color, var(--_studyplan-color-accent));
    --_studyplan-marker-line: dashed;
    --_studyplan-marker-fill: var(--_studyplan-color-surface);
    --_studyplan-credits-bg: color-mix(in srgb, var(--_studyplan-appearance-color) 8%, transparent);

    border-style: dashed;
    border-color: var(--_studyplan-appearance-color);
}

/* The quietest edge of all appearances, so its credits pill is a shade lighter too. */
.studyplan-appearance--dotted {
    --_studyplan-appearance-color: var(
        --studyplan-appearance-dotted-color,
        color-mix(in srgb, var(--_studyplan-color-ink) 58%, var(--_studyplan-color-surface))
    );
    --_studyplan-marker-line: dotted;
    --_studyplan-marker-fill: var(--_studyplan-color-surface);
    --_studyplan-credits-bg: color-mix(in srgb, var(--_studyplan-color-ink) 6%, transparent);

    border-style: dotted;
    border-color: var(--_studyplan-appearance-color);
}

/* The strongest fill: ink by default, which a dark palette turns into a light fill. Its text is
   the surface colour, so the colour has to clear 4.5:1 against the surface. */
.studyplan-appearance--filled-dark {
    --_studyplan-appearance-color: var(--studyplan-appearance-filled-dark-color, var(--_studyplan-color-ink));

    border-style: solid;
    border-color: var(--_studyplan-appearance-color);
    background-color: var(--_studyplan-appearance-color);
}

/* Category label and open hint take the appearance colour, so it has to clear 4.5:1 as text. */
.studyplan-appearance--double-border {
    --_studyplan-appearance-color: var(--studyplan-appearance-double-border-color, #0f766e);
    --_studyplan-marker-fill: var(--_studyplan-color-surface);
    --_studyplan-tile-meta-color: var(--_studyplan-appearance-color);
    --_studyplan-tile-hint-color: var(--_studyplan-appearance-color);
    --_studyplan-credits-bg: color-mix(in srgb, var(--_studyplan-appearance-color) 10%, transparent);

    border-style: double;
    border-width: calc(var(--_studyplan-tile-border-width) + 1px);
    border-color: var(--_studyplan-appearance-color);
}

/* Diagonal stripes behind a solid edge. The darker stripe stays light enough that tile text
   keeps >=4.5:1 on it. */
.studyplan-appearance--hatched {
    --_studyplan-appearance-color: var(--studyplan-appearance-hatched-color, #15803d);

    border-style: solid;
    border-color: var(--_studyplan-appearance-color);
    background-color: var(--_studyplan-color-surface);
    background-image: repeating-linear-gradient(
        135deg,
        color-mix(in srgb, var(--_studyplan-appearance-color) 16%, var(--_studyplan-color-surface)) 0 var(--_studyplan-hatch-size),
        transparent var(--_studyplan-hatch-size) calc(var(--_studyplan-hatch-size) * 2)
    );
}

/* Category text from accent-subtle, marker, arrow and credits towards on-strong; the README lists
   the contrasts to re-check when the fill or the palette changes. */
.studyplan-tile.studyplan-appearance--filled-dark {
    --_studyplan-tile-title-color: var(--_studyplan-color-on-strong);
    --_studyplan-tile-meta-color: color-mix(in srgb, var(--_studyplan-color-accent-subtle) 75%, var(--_studyplan-appearance-color));
    --_studyplan-marker-color: color-mix(in srgb, var(--_studyplan-color-accent) 65%, var(--_studyplan-color-on-strong));
    --_studyplan-tile-hint-color: color-mix(in srgb, var(--_studyplan-color-accent) 45%, var(--_studyplan-color-on-strong));
    --_studyplan-credits-bg: color-mix(in srgb, var(--_studyplan-color-on-strong) 18%, transparent);
}

.studyplan-noscript {
    margin-top: 0.75rem;
    font-size: 0.875rem;
    color: var(--_studyplan-color-ink);
}

/* ---------- Detail panel ---------- */

/* The panel itself is the full-viewport backdrop; .studyplan-panel-dialog is the visible box
   anchored to the side. A click directly on the backdrop (event.target === panel) closes it. */
.studyplan-panel {
    position: fixed;
    inset: 0;
    z-index: var(--studyplan-panel-z-index, 1000);
    display: flex;
    justify-content: flex-end;
    background-color: color-mix(in srgb, var(--_studyplan-color-shade) 45%, transparent);
}

/* The dialog itself scrolls, header included, so arrow keys scroll from any focused control;
   the header stays in view by sticking instead of sitting outside the scroller. */
.studyplan-panel-dialog {
    width: min(26.25rem, 100%);
    height: 100%;
    overflow-y: auto;
    background-color: var(--_studyplan-color-surface);
    box-shadow: -6px 0 24px color-mix(in srgb, var(--_studyplan-color-shade) 22%, transparent);
}

@media (prefers-reduced-motion: no-preference) {
    .studyplan-panel-dialog {
        animation: studyplan-panel-slide-in 0.28s cubic-bezier(0.2, 0.8, 0.3, 1);
    }
}

@keyframes studyplan-panel-slide-in {
    from {
        transform: translateX(100%);
    }
    to {
        transform: translateX(0);
    }
}

.studyplan-panel-header {
    position: sticky;
    top: 0;
    z-index: 1;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 1rem 1rem 0.875rem 1.25rem;
    border-bottom: 1px solid var(--_studyplan-color-border-subtle);
    background-color: var(--_studyplan-color-surface);
}

.studyplan-panel-badges {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
}

.studyplan-panel-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.25rem 0.625rem;
    border-radius: var(--_studyplan-radius-pill);
    background-color: var(--_studyplan-credits-bg);
    font-size: 0.6875rem;
    font-weight: 700;
    line-height: 1.3;
    letter-spacing: 0.03em;
    color: var(--_studyplan-color-ink);
}

.studyplan-panel-badge--category {
    background-color: var(--_studyplan-color-accent-subtle);
    text-transform: uppercase;
}

.studyplan-panel-badge-marker {
    flex: none;
    width: 7px;
    height: 7px;
    border-radius: min(2px, var(--_studyplan-radius));
    background-color: var(--_studyplan-color-accent);
}

.studyplan-panel-close {
    display: inline-flex;
    flex: none;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    border: 1px solid var(--_studyplan-color-border);
    border-radius: 50%;
    background-color: var(--_studyplan-color-surface);
    color: var(--_studyplan-color-ink);
}

.studyplan-panel-close:hover {
    background-color: var(--_studyplan-color-surface-muted);
}

.studyplan-panel-close svg {
    width: 1rem;
    height: 1rem;
}

.studyplan-panel-close:focus-visible,
.studyplan-panel-dialog :where(a, button):focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

.studyplan-panel-main {
    padding: 1.25rem 1.25rem 2rem;
}

.studyplan-panel-title {
    font-size: 1.375rem;
    font-weight: 800;
    line-height: 1.25;
}

.studyplan-panel-meta {
    margin-top: 0.25rem;
    font-size: 0.8125rem;
    color: var(--_studyplan-color-ink-muted);
}

.studyplan-panel-body {
    margin-top: 1.25rem;
    font-size: 0.875rem;
    line-height: 1.6;
}

.studyplan-panel-status {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 1.25rem 0;
    color: var(--_studyplan-color-ink-muted);
}

.studyplan-panel-status::before {
    content: "";
    flex: none;
    width: 1.125rem;
    height: 1.125rem;
    border: 3px solid var(--_studyplan-color-border-subtle);
    border-top-color: var(--_studyplan-color-accent);
    border-radius: 50%;
}

@media (prefers-reduced-motion: no-preference) {
    .studyplan-panel-status::before {
        animation: studyplan-panel-spin 0.7s linear infinite;
    }
}

@keyframes studyplan-panel-spin {
    to {
        transform: rotate(360deg);
    }
}

.studyplan-panel-status--error {
    font-weight: 600;
    color: var(--_studyplan-color-ink);
}

.studyplan-panel-status--error::before {
    content: none;
}

/* The RTE description is the only free-form rich text here: spacing, list markers and
   headings are restored against the reset, headings as small caps-style section labels. */
.studyplan-panel-description > :where(* + *) {
    margin-top: 0.625rem;
}

/* :is(), not :where(): it has to outrank the heading colour rule at the top. */
.studyplan-panel-description :is(h2, h3, h4, h5, h6) {
    font-size: 0.75rem;
    line-height: 1.3;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--_studyplan-color-accent);
}

.studyplan-panel-description > :where(h2, h3, h4, h5, h6):not(:first-child) {
    margin-top: 1.375rem;
}

.studyplan-panel-description > :where(h2, h3, h4, h5, h6) + * {
    margin-top: 0.5rem;
}

.studyplan-panel-description :where(ul) {
    padding-left: 1.25rem;
    list-style: disc;
}

.studyplan-panel-description :where(ol) {
    padding-left: 1.25rem;
    list-style: decimal;
}

.studyplan-panel-description :where(li + li) {
    margin-top: 0.375rem;
}

.studyplan-panel-description :where(a) {
    color: var(--_studyplan-color-accent);
    text-decoration: underline;
}

.studyplan-panel-content-elements {
    margin-top: 1.375rem;
}

/* ---------- Panel media ---------- */

.studyplan-panel-media {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    margin-top: 1.375rem;
}

.studyplan-media-image {
    display: block;
    width: 100%;
    height: auto;
    border-radius: var(--_studyplan-radius);
}

.studyplan-media-placeholder {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    width: 100%;
    aspect-ratio: 16 / 9;
    border: 2px dashed var(--_studyplan-color-border-subtle);
    border-radius: var(--_studyplan-radius);
    background-color: var(--_studyplan-color-surface-muted);
    color: var(--_studyplan-color-ink-muted);
}

.studyplan-media-placeholder svg {
    width: 2.125rem;
    height: 2.125rem;
}

.studyplan-media-placeholder-text {
    font-size: 0.8125rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.studyplan-media-caption {
    margin-top: 0.5rem;
    font-size: 0.8125rem;
    line-height: 1.45;
    color: var(--_studyplan-color-ink-muted);
}

/* ---------- Choice catalogue ---------- */

.studyplan-choice-catalogue {
    margin-top: 1.375rem;
}

.studyplan-choice-catalogue-fieldset {
    min-width: 0;
    padding: 0;
    border: 0;
}

.studyplan-choice-catalogue-legend,
.studyplan-choice-catalogue-title {
    margin-bottom: 0.625rem;
    padding: 0;
    font-size: 0.8125rem;
    font-weight: 700;
    line-height: 1.35;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: var(--_studyplan-color-ink);
}

.studyplan-choice-catalogue-legend-note {
    font-weight: 400;
    letter-spacing: 0;
    text-transform: none;
    color: var(--_studyplan-color-ink-muted);
}

.studyplan-choice-catalogue-options {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* The whole card is the label, so the full area toggles the checkbox. */
.studyplan-choice-catalogue-option {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 0.875rem;
    border: 1px solid var(--_studyplan-color-border);
    border-radius: var(--_studyplan-radius-control);
    background-color: var(--_studyplan-color-surface);
    font-size: 0.9375rem;
    line-height: 1.35;
    cursor: pointer;
}

.studyplan-choice-catalogue-option:hover {
    background-color: var(--_studyplan-color-surface-muted);
}

.studyplan-choice-catalogue-option:has(.studyplan-choice-catalogue-checkbox:checked) {
    border-color: var(--_studyplan-color-accent);
    background-color: var(--_studyplan-color-accent-subtle);
    box-shadow: inset 0 0 0 1px var(--_studyplan-color-accent);
    font-weight: 600;
}

.studyplan-choice-catalogue-checkbox {
    flex: none;
    width: 1.0625rem;
    height: 1.0625rem;
    margin: 0;
    accent-color: var(--_studyplan-color-accent);
    cursor: pointer;
}

.studyplan-choice-catalogue-checkbox:focus-visible {
    outline: var(--_studyplan-focus-outline);
    outline-offset: 2px;
}

.studyplan-choice-catalogue-hint {
    margin-top: 1.125rem;
    padding: 0.875rem;
    border: 1px dashed var(--_studyplan-color-border-subtle);
    border-radius: var(--_studyplan-radius-control);
    color: var(--_studyplan-color-ink-muted);
}

/* Every block stays in the DOM and only toggles `hidden`, so spacing keys off visibility
   rather than DOM position. */
.studyplan-choice-group:not([hidden]) {
    margin-top: 1.125rem;
    padding: 0.125rem 0 0.125rem 1rem;
    border-left: 3px solid var(--_studyplan-color-accent);
}

.studyplan-choice-group-title {
    font-size: 1rem;
    line-height: 1.35;
}

.studyplan-choice-catalogue .studyplan-panel-description {
    font-size: 0.9375rem;
}

.studyplan-choice-group-title + *,
.studyplan-choice-catalogue .studyplan-panel-description + .studyplan-choice-group-list {
    margin-top: 0.5rem;
}

/* Restores the markers the reset strips from lists. */
.studyplan-choice-group-list {
    padding-left: 1.25rem;
    list-style: disc;
    font-size: 0.9375rem;
    line-height: 1.4;
}

.studyplan-choice-group-list > li + li {
    margin-top: 0.3125rem;
}

/* ---------- Tablet ---------- */

@media (max-width: 51.25rem) {
    .studyplan-tiles:not(.studyplan-tiles--reduced) {
        --_studyplan-tile-basis: 13.75rem;
    }
}

/* ---------- Mobile ---------- */

/* The semester column turns into a heading bar above each row and every row form stacks
   its tiles in one column. Declared last so it wins the source-order ties with the rules above. */
@media (max-width: 30rem) {
    .studyplan-legend-group {
        padding: 0.375rem;
    }

    .studyplan-legend {
        gap: 0.375rem;
    }

    .studyplan-legend-toggle {
        padding: 0.375rem 0.625rem;
        font-size: 0.75rem;
    }

    /* Tiles already stand one per line here, so hiding details would gain no overview. */
    .studyplan-view-toggle {
        display: none;
    }

    .studyplan-row {
        flex-direction: column;
        gap: var(--_studyplan-row-gap);
    }

    /* Neutral ground with a dark edge, so the heading cannot be mistaken for a tile. */
    .studyplan-semester-label,
    .studyplan-row--semester-label .studyplan-semester-label {
        flex-basis: auto;
        justify-content: flex-start;
        padding: 0.5625rem 0.75rem;
        border-left: 4px solid var(--_studyplan-color-ink);
        background-color: var(--_studyplan-color-surface-muted);
        text-align: left;
        font-size: 0.875rem;
        font-weight: 800;
        line-height: 1.15;
    }

    .studyplan-semester-number {
        display: none;
    }

    .studyplan-semester-text {
        position: static;
        width: auto;
        height: auto;
        margin: 0;
        overflow: visible;
        clip: auto;
        white-space: normal;
    }

    .studyplan-row-body {
        flex-direction: column;
    }

    .studyplan-tiles {
        flex-direction: column;
        flex-wrap: nowrap;
    }

    /* In a column flex container the width basis would act as a fixed height instead. */
    .studyplan-tile,
    .studyplan-side-column {
        flex-basis: auto;
    }

    .studyplan-tile-title {
        margin-bottom: 0;
        font-weight: normal;
    }

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

    .studyplan-panel-dialog {
        width: 100%;
    }
}
