/* ============================================================
   CiA 402 Statusword & Controlword Decoder

   Two-column layout. Each column has its own numeric-base field
   (DEC / HEX / BIN-MSB / BIN-LSB) on top, then bit rows 0→15
   in grid-aligned rows: [bit# | bulb | short | name].
   Descriptions live in hover-tooltips, not inline, so rows stay
   dense and columns stay aligned.
   ============================================================ */

/* ---------- Lead paragraph ---------- */
.cia-lead {
    color: var(--fg-secondary);
    font-size: var(--text-sm);
    line-height: 1.55;
    margin: 0.5rem 0 1rem;
    max-width: 90ch;
}
.cia-lead strong { color: var(--fg); }

/* ---------- Two-column grid ---------- */
.cia-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}
@media (max-width: 860px) {
    .cia-grid { grid-template-columns: 1fr; gap: 1.75rem; }
}

/* ---------- Column header ---------- */
.cia-col {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-width: 0;
}

.cia-col-head {
    display: flex;
    align-items: baseline;
    gap: 0.625rem;
    flex-wrap: wrap;
    padding-bottom: 0.375rem;
    border-bottom: 1px solid var(--border);
}
.cia-col[data-word="controlword"] .cia-col-head { border-bottom-color: rgba(251,191,36,0.22); }
.cia-col[data-word="statusword"] .cia-col-head  { border-bottom-color: rgba(34,197,94,0.22); }

.cia-col-title {
    font-size: 1.0625rem;
    font-weight: 600;
    margin: 0;
    color: var(--fg);
    letter-spacing: -0.01em;
}
.cia-col[data-word="controlword"] .cia-col-title::before {
    content: '→ '; color: var(--accent); font-weight: 700;
}
.cia-col[data-word="statusword"] .cia-col-title::before {
    content: '← '; color: var(--success); font-weight: 700;
}
.cia-col-sub {
    font-size: 0.7rem;
    font-family: var(--font-mono);
    color: var(--fg-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    white-space: nowrap;
}

/* ---------- Reusable numeric-base field ----------
   Lives in templates/components/num_base_field.html and mirrors the
   value-field visual shell — but strictly for integer dec/hex/binary
   input, not unit conversion.
*/
.num-field { min-width: 0; }
.num-field-inner {
    display: flex;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--bg-input);
    overflow: hidden;
    transition: border-color 0.15s, box-shadow 0.15s;
}
.num-field-inner:hover { border-color: var(--border-hover); }
.num-field-inner:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
}
.num-field--error .num-field-inner {
    border-color: var(--error);
    box-shadow: 0 0 0 3px rgba(239,68,68,0.15);
}
.num-field-hint {
    font-size: 0.75rem;
    color: var(--error);
    margin-top: 0.25rem;
    min-height: 0;
    line-height: 1.3;
    display: none;
}
.num-field-hint--visible { display: block; }

.num-field-input {
    flex: 1;
    padding: 0.5rem 0.75rem;
    border: none;
    background: transparent;
    color: var(--fg);
    font-family: var(--font-mono);
    font-size: 0.9375rem;
    outline: none;
    caret-color: var(--accent);
    min-width: 0;
    font-variant-numeric: tabular-nums;
}
.num-field-input::placeholder { color: var(--fg-muted); }

.num-field-base {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    padding: 0.5rem 1.625rem 0.5rem 0.625rem;
    border: none;
    border-left: 1px solid var(--border);
    background-color: rgba(255,255,255,0.02);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%23adb5bd' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.5rem center;
    color: var(--fg-muted);
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 600;
    cursor: pointer;
    outline: none;
}
.num-field-base:focus-visible { background-color: var(--accent-subtle); color: var(--fg); }
[data-theme="light"] .num-field-base { background-color: rgba(0,0,0,0.03); }

/* Copy button — sits between input and base selector. No divider on the
   input side so it reads as part of the input field (same pattern as the
   site's other value-field-copy). The base selector keeps its own
   left-divider as the real visual split. */
.num-field-copy {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    border: none;
    background: transparent;
    color: var(--fg-muted);
    cursor: pointer;
    outline: none;
    padding: 0;
    transition: color 0.15s, background 0.15s;
}
.num-field-copy:hover { color: var(--fg); background: var(--bg-elevated); }
.num-field-copy:focus-visible { color: var(--fg); background: var(--accent-subtle); }
.num-field-copy-icon--done { display: none; color: var(--success); }
.num-field-copy--done .num-field-copy-icon--idle { display: none; }
.num-field-copy--done .num-field-copy-icon--done { display: inline-block; }
.num-field-copy--done { color: var(--success); }

/* ---------- Bit list (vertical, 0 at top) ---------- */
.cia-bit-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    /* No inter-row gap — rows sit flush with divider borders, like a table. */
    gap: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    overflow: hidden;
    background: var(--bg-elevated);
}

/* Single bit row — grid-aligned across all 16 rows. */
.cia-bit-row {
    /* bit# | bulb | short | name */
    display: grid;
    grid-template-columns: 1.75rem 1rem 3rem 1fr;
    align-items: center;
    gap: 0.625rem;
    padding: 0.3125rem 0.625rem;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    transition: background 0.12s;
    min-width: 0;
    position: relative;
    /* tabular numeric column looks aligned at any zoom */
    font-variant-numeric: tabular-nums;
}
.cia-bit-row:last-child { border-bottom: none; }
.cia-bit-row:hover { background: var(--bg-input); }
.cia-bit-row:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
    z-index: 2;
}

.cia-bit-num {
    font-size: 0.75rem;
    color: var(--fg-muted);
    text-align: right;
    font-weight: 600;
}

.cia-bit-bulb {
    width: 0.875rem;
    height: 0.875rem;
    border-radius: 50%;
    background: var(--bg-card);
    border: 2px solid var(--border);
    flex-shrink: 0;
    transition: all 0.15s;
    justify-self: center;
    /* Flash halo lives on ::after so it composites on top of the bulb's
       own box-shadow (the green glow on an active bulb) instead of
       replacing it — avoids the "snap back" at animation end. */
    position: relative;
}
.cia-bit-bulb::after {
    content: '';
    position: absolute;
    inset: 0;
    border-radius: 50%;
    pointer-events: none;
    box-shadow: 0 0 0 0 rgba(251,191,36,0);
}
.cia-bit-row--active .cia-bit-bulb {
    background: var(--success);
    border-color: var(--success);
    box-shadow:
        0 0 0 2px rgba(34,197,94,0.12),
        0 0 8px rgba(34,197,94,0.55);
}
.cia-bit-row--active { background: rgba(34,197,94,0.06); }
[data-theme="light"] .cia-bit-row--active { background: rgba(34,197,94,0.09); }

/* Flash-on-change — a gold ring that expands and fades on any bulb whose
   bit just flipped. Runs on ::after so the bulb's own shadow (green glow
   when active) stays visible underneath. Skipped on the bulb the user
   just clicked so the click stays the primary feedback. */
@keyframes cia-bit-flash {
      0% { box-shadow: 0 0 0 0   rgba(251,191,36,0.55); }
     60% { box-shadow: 0 0 0 6px rgba(251,191,36,0.25); }
    100% { box-shadow: 0 0 0 9px rgba(251,191,36,0);    }
}
.cia-bit-bulb--flash::after {
    animation: cia-bit-flash 0.55s ease-out;
}

.cia-bit-short {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--fg-muted);
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.cia-bit-row--active .cia-bit-short { color: var(--success); }

.cia-bit-name {
    font-size: 0.8125rem;
    font-weight: 500;
    color: var(--fg);
    line-height: 1.25;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ---------- Custom tooltip (reveals on row hover / focus) ---------- */
/* Native title attribute covers browsers / a11y; custom .cia-bit-tip
   provides a styled, themed tooltip with richer layout. */
.cia-bit-tip {
    position: absolute;
    z-index: 20;
    left: 50%;
    top: calc(100% + 4px);
    transform: translateX(-50%);
    padding: 0.4375rem 0.5625rem;
    background: var(--bg-card);
    border: 1px solid var(--border-hover);
    border-radius: var(--radius-sm);
    font-size: 0.7rem;
    line-height: 1.4;
    color: var(--fg-secondary);
    white-space: normal;
    max-width: 22rem;
    min-width: 12rem;
    box-shadow: 0 6px 20px rgba(0,0,0,0.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.12s ease-out, transform 0.12s ease-out;
}
[data-theme="light"] .cia-bit-tip {
    box-shadow: 0 6px 20px rgba(0,0,0,0.12);
}
.cia-bit-row:hover .cia-bit-tip,
.cia-bit-row:focus-visible .cia-bit-tip {
    opacity: 1;
    transform: translateX(-50%) translateY(1px);
    pointer-events: auto;
}
/* Arrow */
.cia-bit-tip::before {
    content: '';
    position: absolute;
    top: -5px;
    left: 50%;
    transform: translateX(-50%) rotate(45deg);
    width: 8px;
    height: 8px;
    background: var(--bg-card);
    border-top: 1px solid var(--border-hover);
    border-left: 1px solid var(--border-hover);
}
/* ---------- Op-mode picker (in decoder header) ---------- */
.cia-mode-picker {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.7rem;
    color: var(--fg-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
}
.cia-mode-label { color: var(--fg-muted); }
#cia-op-mode {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    padding: 0.375rem 1.75rem 0.375rem 0.625rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background-color: var(--bg-input);
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='10' viewBox='0 0 24 24' fill='none' stroke='%23adb5bd' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.5rem center;
    color: var(--fg);
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: none;
    letter-spacing: 0;
    cursor: pointer;
    outline: none;
}
#cia-op-mode:focus-visible {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-subtle);
}

/* Bit rows whose descriptions have been overridden by op-mode get a subtle
   accent-edge so the user knows the text is mode-specific, not canonical. */
.cia-bit-row--mode-override {
    box-shadow: inset 2px 0 0 var(--accent);
}
.cia-bit-row--mode-override .cia-bit-short {
    color: var(--accent);
}
.cia-bit-row--mode-override.cia-bit-row--active .cia-bit-short {
    /* Active-green wins on the active state so the bulb story stays clear. */
    color: var(--success);
}

/* ---------- State machine (clickable) ---------- */
.cia-sm-hint {
    font-size: 0.7rem;
    color: var(--fg-muted);
    font-style: italic;
}
.cia-kbd-hint {
    font-style: normal;
    color: var(--fg-muted);
    opacity: 0.7;
}
.cia-walk-btn {
    font-size: 0.75rem;
}
.cia-walk-btn--running {
    background: var(--accent);
    color: var(--bg-primary);
    border-color: var(--accent);
}
.cia-walk-btn--running:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

.state-machine {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(165px, 1fr));
    gap: 0.625rem;
}

.state-node {
    all: unset;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
    padding: 0.625rem 0.75rem;
    border: 2px solid var(--border);
    border-radius: var(--radius);
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--fg-secondary);
    background: var(--bg-elevated);
    transition: all 0.15s;
    text-align: left;
    cursor: pointer;
    min-width: 0;
}
.state-node:hover {
    border-color: var(--accent);
    background: var(--accent-subtle);
    color: var(--fg);
}
.state-node:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}
.state-node-name {
    font-weight: 600;
    color: var(--fg);
    font-size: 0.8125rem;
    line-height: 1.2;
}
.state-node-values {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    font-size: 0.65rem;
    color: var(--fg-muted);
}
.state-node-sw, .state-node-cw {
    padding: 1px 4px;
    border-radius: 3px;
    background: var(--bg-card);
    border: 1px solid var(--border);
}

.state-node--active {
    border-color: var(--accent);
    background: var(--accent-subtle);
    box-shadow: 0 0 12px rgba(251,191,36,0.25);
}
.state-node--active .state-node-name { color: var(--accent); }

.state-node--fault {
    border-color: var(--error);
    background: rgba(239,68,68,0.08);
}
.state-node--fault .state-node-name { color: var(--error); }

.cia-sm-active {
    margin-top: 0.875rem;
    padding: 0.625rem 0.75rem;
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    font-size: 0.8125rem;
    display: flex;
    gap: 0.5rem;
    align-items: baseline;
    flex-wrap: wrap;
}
.cia-sm-active-label {
    color: var(--fg-muted);
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 700;
}
.cia-sm-active-name { color: var(--accent); font-weight: 600; }
.cia-sm-active-hint { color: var(--fg-secondary); font-size: 0.75rem; flex: 1; min-width: 0; }

/* ---------- Diff mode (compare two captured values) ---------- */
.cia-diff {
    margin-top: 1rem;
    padding: 0.875rem 1rem;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.cia-diff > summary {
    cursor: pointer;
    list-style: none;
    display: flex;
    align-items: baseline;
    gap: 0.625rem;
    flex-wrap: wrap;
}
.cia-diff > summary::-webkit-details-marker { display: none; }
.cia-diff > summary::before {
    content: '▸';
    color: var(--accent);
    font-size: 0.75rem;
    transition: transform 0.15s;
    align-self: center;
}
.cia-diff[open] > summary::before { transform: rotate(90deg); }
.cia-diff-summary-label {
    font-weight: 600;
    color: var(--fg);
    font-size: 0.9375rem;
}
.cia-diff-summary-hint {
    color: var(--fg-muted);
    font-size: 0.75rem;
}
.cia-diff-body {
    margin-top: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.875rem;
}
.cia-diff-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.25rem;
}
.cia-diff-col {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-width: 0;
}
.cia-diff-col-head {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid var(--border);
}
.cia-diff-col-title {
    font-weight: 600;
    color: var(--fg);
    font-size: 0.875rem;
}
.cia-diff-col-sub {
    font-family: var(--font-mono);
    font-size: 0.7rem;
    color: var(--fg-muted);
}
.cia-diff-inputs {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}
.cia-diff-input {
    display: grid;
    grid-template-columns: 3.5rem 1fr;
    align-items: center;
    gap: 0.5rem;
}
.cia-diff-input-label {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--fg-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
}

/* Result list */
.cia-diff-result {
    margin-top: 0.25rem;
    min-height: 1px;
}
.cia-diff-count {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--fg-muted);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-bottom: 0.375rem;
}
.cia-diff-empty {
    font-size: 0.8125rem;
    font-style: italic;
    color: var(--fg-muted);
    padding: 0.375rem 0;
}
.cia-diff-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
}
.cia-diff-row {
    display: grid;
    grid-template-columns: 1.75rem 3.25rem 3rem 1fr;
    gap: 0.5rem;
    align-items: center;
    padding: 0.3125rem 0.5rem;
    border-radius: var(--radius-sm);
    background: var(--bg-elevated);
    font-size: 0.8125rem;
}
.cia-diff-row--set { box-shadow: inset 2px 0 0 var(--success); }
.cia-diff-row--clr { box-shadow: inset 2px 0 0 var(--error); }
.cia-diff-bit {
    color: var(--fg-muted);
    font-weight: 600;
    text-align: right;
    font-size: 0.75rem;
}
.cia-diff-arrow {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--fg);
}
.cia-diff-row--set .cia-diff-arrow { color: var(--success); }
.cia-diff-row--clr .cia-diff-arrow { color: var(--error); }
.cia-diff-short {
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--fg-muted);
}
.cia-diff-name {
    color: var(--fg);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Transition line — separate from per-column result because it summarises
   both statusword inputs at once. */
.cia-diff-state {
    padding: 0.5rem 0.75rem;
    background: var(--bg-elevated);
    border-radius: var(--radius-sm);
    font-size: 0.8125rem;
    color: var(--fg-secondary);
    font-family: var(--font-mono);
    min-height: 1px;
}
.cia-diff-state:empty { display: none; }

@media (max-width: 860px) {
    .cia-diff-grid { grid-template-columns: 1fr; gap: 1.25rem; }
}

/* ---------- More-about collapsible ---------- */
.cia-more {
    margin-top: 1rem;
    padding: 0.875rem 1rem;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: var(--radius);
}
.cia-more > summary {
    cursor: pointer;
    font-weight: 600;
    color: var(--fg);
    font-size: 0.875rem;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.cia-more > summary::-webkit-details-marker { display: none; }
.cia-more > summary::before {
    content: '▸';
    color: var(--accent);
    font-size: 0.75rem;
    transition: transform 0.15s;
}
.cia-more[open] > summary::before { transform: rotate(90deg); }
.cia-more-body {
    margin-top: 0.625rem;
    color: var(--fg-secondary);
    font-size: 0.8125rem;
    line-height: 1.55;
}
.cia-more-body p { margin: 0 0 0.625rem; }
.cia-more-body p:last-child { margin-bottom: 0; }
.cia-more-body strong { color: var(--fg); }
.cia-more-body a { color: var(--accent); }
.cia-more-body a:hover { color: var(--accent-hover); }

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
    .cia-col-head { flex-wrap: wrap; }
    .state-machine { grid-template-columns: 1fr 1fr; }
    .cia-bit-row {
        grid-template-columns: 1.5rem 1rem 2.75rem 1fr;
        gap: 0.5rem;
        padding: 0.375rem 0.5rem;
    }
    /* Narrower base selector — "BIN 15→0" is still readable at 4.5rem. */
    .num-field-base {
        min-width: 4.5rem;
        padding: 0.5rem 1.375rem 0.5rem 0.5rem;
        font-size: 0.7rem;
    }
}
@media (max-width: 480px) {
    .state-machine { grid-template-columns: 1fr; }
    .cia-bit-row { grid-template-columns: 1.25rem 0.875rem 2.5rem 1fr; gap: 0.4375rem; }
    .cia-bit-name { font-size: 0.75rem; }
    .cia-bit-short { font-size: 0.65rem; }
    .cia-bit-tip { font-size: 0.65rem; max-width: 14rem; }
    .cia-lead { font-size: 0.8125rem; }
    /* Even tighter on phones — clamp width + ellipsis on the closed state.
       Native mobile pickers expand the dropdown full-screen when open, so
       truncation only hides the unselected labels briefly. */
    .num-field-base {
        min-width: 0;
        width: 4rem;
        padding: 0.5rem 1.125rem 0.5rem 0.375rem;
        font-size: 0.65rem;
        text-overflow: ellipsis;
        overflow: hidden;
    }
    .num-field-copy { width: 1.75rem; }
}
