/* Feature 042 — the shared story presentation: badge, strip, segments, section heads, toolbars, empty and loading
   states, run facts, fields card, status chip, prompts tab and overview card, for every reusable demo experience.
   Tokens come from brand.css only; the experience stylesheets keep their own views and never override these
   blocks. The token map is documented in docs/demo-story-engine.html and follows the ArchitectNow Style Guide:
   colour (pp. 10–16), type (pp. 18–22), spacing on 4 (p. 25), radii 4/8/12/16 (p. 26), buttons and focus (pp.
   29–30), cards (p. 31), validation (p. 34) and the section label, numerals and proof bar (p. 38). */

/* ---- Hero badge (dark ground: red lifts to red-light, hierarchy is white alpha) ---------------------------- */
.story-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 4px 10px;
    border: 1px solid rgba(255, 255, 255, .20);
    border-radius: 8px;
    background: rgba(255, 255, 255, .06);
    color: rgba(255, 255, 255, .80);
    font-size: 13px;
    font-weight: 600;
    line-height: 1.5;
    white-space: nowrap;
}

.story-badge .mud-icon-root {
    width: 16px;
    height: 16px;
    font-size: 16px;
    color: var(--an-red-light);
}

/* ---- Story strip: the proof bar, the hero's floor ---------------------------------------------------------- */
.story-strip {
    display: flex;
    flex-wrap: wrap;
    gap: 14px 40px;
    margin: 0;
    padding: 20px 32px;
    border-top: 1px solid rgba(255, 255, 255, .10);
}

.story-fact {
    display: flex;
    align-items: baseline;
    gap: 8px;
    min-width: 0;
}

.story-fact dd {
    margin: 0;
    color: var(--an-white);
    font-size: 22px;
    font-weight: 800;
    line-height: 1;
    letter-spacing: -0.02em;
    overflow-wrap: anywhere;
}

.story-fact dt {
    order: 2;
    color: rgba(255, 255, 255, .50);
    font-size: 13.5px;
    font-weight: 700;
    line-height: 1.3;
}

@media (min-width: 1280px) {
    .story-strip {
        flex-wrap: nowrap;
        justify-content: space-between;
        gap: 24px;
    }
}

/* Phones: two figure columns, so each fact stays on one line instead of wrapping mid-row. */
@media (max-width: 599.98px) {
    .story-strip {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 12px 16px;
        padding: 16px;
    }

    .story-fact {
        flex-wrap: wrap;
        gap: 4px 8px;
    }

    .story-fact dd {
        font-size: 20px;
    }
}

/* ---- Overview card: "What this demo shows" ---------------------------------------------------------------- */
.story-overview {
    margin-top: 24px;
    padding: 28px;
    border: 1px solid var(--an-border);
    border-radius: 16px;
    background: var(--an-white);
}

.story-label {
    display: flex;
    align-items: center;
    gap: 14px;
    margin: 0 0 20px;
}

.story-label::before {
    content: "";
    width: 36px;
    height: 3px;
    flex: 0 0 36px;
    border-radius: 999px;
    background: var(--an-red);
}

.story-label > span {
    color: var(--an-red);
    font-size: 18px;
    font-weight: 800;
    line-height: 1;
    letter-spacing: -0.01em;
}

.story-overview-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
    gap: 24px 40px;
}

.story-overview h3 {
    margin: 0 0 4px;
    font-size: 16px;
    font-weight: 700;
    line-height: 1.25;
    letter-spacing: -0.01em;
}

.story-outline {
    margin: 0;
    padding: 0;
    list-style: none;
}

.story-outline li {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 4px 10px;
    padding: 10px 0;
    border-top: 1px solid var(--an-border);
}

.story-outline li:first-child {
    border-top: 0;
}

.story-outline-name {
    color: var(--an-ink);
    font-size: 15px;
    font-weight: 600;
    overflow-wrap: anywhere;
}

.story-outline-note {
    flex-basis: 100%;
    color: var(--an-gray-3);
    font-size: 14px;
    line-height: 1.5;
}

@media (min-width: 1024px) {
    .story-overview {
        padding: 32px;
    }
}

@media (max-width: 599.98px) {
    .story-overview {
        padding: 20px;
    }
}

/* ---- Segmented control (12px track, 8px segments — the nesting rule) --------------------------------------- */
.story-segments {
    display: inline-flex;
    flex-wrap: wrap;
    gap: 2px;
    padding: 3px;
    border-radius: 12px;
    background: var(--an-border);
}

.story-segment {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 44px;
    padding: 0 16px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--an-gray-3);
    font: inherit;
    font-size: 15px;
    font-weight: 600;
    line-height: 1;
    cursor: pointer;
    transition: background-color .2s var(--an-ease), color .2s var(--an-ease);
}

.story-segment:hover {
    color: var(--an-ink);
}

.story-segment.is-active {
    background: var(--an-white);
    color: var(--an-ink);
    box-shadow: 0 10px 30px -22px rgba(0, 0, 0, .25);
}

.story-segment:focus-visible {
    outline: 2px solid var(--an-red);
    outline-offset: 2px;
}

.story-segment:disabled {
    opacity: .5;
    cursor: not-allowed;
}

.story-segment-count {
    padding: 1px 8px;
    border: 1px solid var(--an-border-2);
    border-radius: 4px;
    background: var(--an-paper);
    font-family: var(--an-font-mono);
    font-size: 13px;
    font-weight: 600;
    line-height: 1.4;
}

/* ---- Section head, lead, meta and counts ------------------------------------------------------------------- */
.story-section-head {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: space-between;
    gap: 12px 16px;
    margin: 0 0 16px;
}

.story-section-copy {
    flex: 1 1 320px;
    min-width: 0;
}

.story-section-head h2 {
    margin: 0;
    font-size: 22px;
    font-weight: 800;
    line-height: 1.15;
    letter-spacing: -0.02em;
    overflow-wrap: anywhere;
}

.story-section-head h3 {
    margin: 0;
    font-size: 17px;
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: -0.01em;
    overflow-wrap: anywhere;
}

.story-section-head.is-compact {
    align-items: center;
    margin: 0;
}

.story-section-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
}

.story-lead {
    margin: 4px 0 0;
    max-width: 640px;
    color: var(--an-gray-3);
    font-size: 15px;
    line-height: 1.5;
}

.story-meta {
    margin: 0;
    color: var(--an-gray-4);
    font-size: 14px;
    line-height: 1.5;
}

.story-count {
    margin-left: 8px;
    color: var(--an-gray-4);
    font-family: var(--an-font-mono);
    font-size: 13px;
    font-weight: 500;
    letter-spacing: 0;
    text-transform: none;
    vertical-align: middle;
}

/* ---- Toolbar ------------------------------------------------------------------------------------------------ */
.story-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 0 0 24px;
}

.story-toolbar-lead,
.story-toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

@media (max-width: 767.98px) {
    .story-toolbar,
    .story-toolbar-lead,
    .story-toolbar-actions {
        display: grid;
        grid-template-columns: minmax(0, 1fr);
    }

    .story-toolbar .story-segments {
        display: grid;
        grid-auto-flow: column;
        grid-auto-columns: minmax(0, 1fr);
    }

    .story-toolbar .story-segment {
        justify-content: center;
        padding: 0 8px;
    }
}

/* ---- Panels and the two-column split -------------------------------------------------------------------------- */
.story-panel {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 0;
    padding: 24px;
    border: 1px solid var(--an-border);
    border-radius: 16px;
    background: var(--an-white);
}

.story-panel h3 {
    margin: 0;
    font-size: 17px;
    font-weight: 700;
    line-height: 1.2;
    letter-spacing: -0.01em;
}

.story-panel h4 {
    margin: 0;
    font-size: 15px;
    font-weight: 700;
}

.story-split {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 24rem;
    gap: 16px;
    align-items: start;
}

@media (max-width: 1200px) {
    .story-split {
        grid-template-columns: minmax(0, 1fr);
    }
}

@media (max-width: 599.98px) {
    .story-panel {
        padding: 16px;
    }
}

/* ---- Empty and loading states -------------------------------------------------------------------------------- */
.story-empty {
    padding: 24px;
    border: 1px dashed var(--an-border-2);
    border-radius: 16px;
    background: var(--an-white);
}

.story-empty h2,
.story-empty h3 {
    margin: 0 0 8px;
    font-weight: 800;
    line-height: 1.2;
    letter-spacing: -0.02em;
}

.story-empty h2 {
    font-size: 22px;
}

.story-empty h3 {
    font-size: 19px;
}

.story-empty p {
    margin: 0;
    max-width: 640px;
    color: var(--an-gray-3);
    font-size: 15px;
    line-height: 1.65;
}

.story-empty p + p {
    margin-top: 8px;
}

.story-empty a {
    color: var(--an-red);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.story-loading {
    display: grid;
    gap: 12px;
    max-width: 640px;
    padding: 8px 0;
}

.story-skeleton {
    display: block;
    height: 10px;
    border-radius: 999px;
    background: var(--an-border);
}

.story-skeleton:nth-child(2) {
    width: 80%;
}

.story-skeleton:nth-child(3) {
    width: 90%;
}

.story-loading-text {
    margin: 0;
    color: var(--an-gray-3);
    font-size: 14px;
    font-weight: 600;
}

/* ---- Run facts (label · value grid) ------------------------------------------------------------------------- */
.story-facts {
    display: grid;
    grid-template-columns: max-content minmax(0, 1fr);
    gap: 8px 24px;
    margin: 0;
    font-size: 15px;
    line-height: 1.5;
}

.story-facts dt {
    color: var(--an-gray-4);
}

.story-facts dd {
    margin: 0;
    color: var(--an-ink);
    font-weight: 600;
    overflow-wrap: anywhere;
}

.story-facts.is-boxed {
    padding: 12px 16px;
    border: 1px solid var(--an-border);
    border-radius: 12px;
    background: var(--an-paper);
}

/* Meta copy in gray-4 (AA on white and paper); never gray-5 below 18.66px bold, never italic. */
.story-muted {
    color: var(--an-gray-4);
    font-weight: 400;
}

@media (max-width: 599.98px) {
    .story-facts {
        grid-template-columns: minmax(0, 1fr);
        gap: 2px 0;
    }

    .story-facts dd {
        margin-bottom: 8px;
    }
}

/* ---- Fields card: one row per story field ------------------------------------------------------------------ */
.story-fields-section {
    min-width: 0;
}

.story-fields-section + .story-fields-section {
    padding-top: 16px;
    border-top: 1px solid var(--an-border);
}

.story-subhead {
    margin: 0 0 8px;
    color: var(--an-gray-4);
    font-size: 12px;
    font-weight: 700;
    letter-spacing: .08em;
    text-transform: uppercase;
}

.story-progress {
    margin: 0 0 8px;
    color: var(--an-gray-4);
    font-size: 13px;
}

.story-meter {
    height: 4px;
    border-radius: 999px;
    background: var(--an-border);
    overflow: hidden;
}

.story-meter-fill {
    display: block;
    height: 100%;
    background: var(--an-red);
    transition: width .3s var(--an-ease);
}

.story-meter + .story-fields {
    margin-top: 14px;
}

/* Label above value, so a long story-defined label never squeezes its value in a narrow column. */
.story-fields {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    margin: 0;
}

.story-fields dt {
    margin-top: 12px;
    color: var(--an-gray-4);
    font-size: 13px;
    line-height: 1.4;
    overflow-wrap: anywhere;
}

.story-fields dt:first-of-type {
    margin-top: 0;
}

.story-fields dd {
    margin: 2px 0 0;
    color: var(--an-ink);
    font-size: 15px;
    font-weight: 600;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

.story-fields dd .story-tag,
.story-fields dd .story-status {
    margin-left: 6px;
    vertical-align: middle;
}

/* The caller's own words, or the scanner's source, behind a value. */
.story-evidence {
    display: block;
    margin-top: 4px;
    color: var(--an-gray-4);
    font-size: 13px;
    font-weight: 400;
    line-height: 1.45;
    overflow-wrap: anywhere;
}

@media (min-width: 600px) and (max-width: 1200px) {
    .story-fields {
        grid-template-columns: minmax(8rem, 14rem) minmax(0, 1fr);
        column-gap: 24px;
    }

    .story-fields dt,
    .story-fields dd {
        margin-top: 10px;
    }

    .story-fields dt:first-of-type,
    .story-fields dd:first-of-type {
        margin-top: 0;
    }
}

/* ---- Tags and chips: squared, never pills ------------------------------------------------------------------ */
.story-tag {
    display: inline-block;
    padding: 2px 8px;
    border: 1px solid var(--an-border-2);
    border-radius: 4px;
    background: var(--an-paper);
    color: var(--an-gray-3);
    font-family: var(--an-font-mono);
    font-size: 11px;
    font-weight: 500;
    letter-spacing: .04em;
    line-height: 1.5;
    white-space: nowrap;
    vertical-align: middle;
}

.story-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.story-chip {
    display: inline-block;
    padding: 1px 8px;
    border: 1px solid var(--an-border-2);
    border-radius: 4px;
    background: var(--an-paper);
    color: var(--an-gray-1);
    font-size: 13px;
    font-weight: 600;
    line-height: 1.5;
}

.story-chip.is-no {
    border-color: var(--an-red);
    background: var(--an-red-wash);
    color: var(--an-red);
}

/* Five status styles. Complete uses the one sanctioned green (Style Guide p. 10); review and failure are brand
   red with a border and text, never colour alone; partial is a hint of red under ink; pending stays neutral. */
.story-status {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 2px 10px;
    border: 1px solid var(--an-border-2);
    border-radius: 4px;
    background: var(--an-paper);
    color: var(--an-gray-2);
    font-size: 13px;
    font-weight: 700;
    line-height: 1.5;
    white-space: nowrap;
}

.story-status.is-complete {
    border-color: rgba(46, 125, 67, .30);
    background: #EDF6EF;
    color: #2E7D43;
}

.story-status.is-needsreview {
    border-color: rgba(167, 37, 44, .30);
    background: var(--an-red-wash);
    color: var(--an-red);
}

.story-status.is-partial {
    border-color: var(--an-red-tint);
    background: var(--an-red-wash);
    color: var(--an-gray-1);
}

.story-status.is-failed {
    border-color: var(--an-red);
    background: var(--an-white);
    color: var(--an-red);
}

.story-status.is-pending {
    color: var(--an-gray-3);
}

.story-status.is-busy::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 999px;
    background: var(--an-red);
    animation: story-pulse 1.2s ease-in-out infinite;
}

@keyframes story-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: .35; }
}

/* ---- Prompts tab --------------------------------------------------------------------------------------------- */
.story-prompts {
    display: grid;
    gap: 16px;
}

.story-prompt-slot {
    display: grid;
    gap: 8px;
}

.story-prompt-slot + .story-prompt-slot {
    margin-top: 8px;
}

/* The control base (13 × 16 padding, 12px radius, border-2 hairline) in the mono register, because a prompt is
   machine input. */
.story-prompt {
    width: 100%;
    min-height: 12rem;
    padding: 13px 16px;
    border: 1px solid var(--an-border-2);
    border-radius: 12px;
    background: var(--an-white);
    color: var(--an-ink);
    font-family: var(--an-font-mono);
    font-size: 13px;
    line-height: 1.6;
    resize: vertical;
    transition: border-color .2s var(--an-ease);
}

.story-prompt:hover {
    border-color: var(--an-gray-7);
}

.story-prompt:focus-visible {
    outline: 2px solid var(--an-red);
    outline-offset: 2px;
}

pre.story-prompt {
    min-height: 0;
    max-height: 18rem;
    margin: 0;
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.story-prompt-edited {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    margin: 0;
    color: var(--an-gray-4);
    font-size: 14px;
}

@media (prefers-reduced-motion: reduce) {
    .story-segment,
    .story-meter-fill,
    .story-prompt {
        transition: none;
    }

    .story-status.is-busy::before {
        animation: none;
    }
}
