.pigpen-tool {
    --pigpen-sidebar-width: 340px;
    --pigpen-panel: #f8f8f9;
    --pigpen-panel-strong: #fff;
    --pigpen-line: #d8dbe0;
    --pigpen-line-strong: #c9ced6;
    --pigpen-text: #152033;
    --pigpen-muted: #687385;
    --pigpen-blue: #dbe7f5;
    --pigpen-blue-strong: #b8c6da;
    --pigpen-red: #f3e4e4;
    --pigpen-red-strong: #e9d3d3;
    --pigpen-green: #eef8ef;
    --pigpen-green-strong: #cfe2d1;
    --pigpen-amber: #fff5e3;
    --pigpen-amber-strong: #ecd8ab;
    --pigpen-hover-filter: brightness(0.97);
    --pigpen-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
    color: var(--pigpen-text);
    font-family: "Inter", var(--font-sans);
}

html[data-theme="dark"] .pigpen-tool {
    --pigpen-panel: #20242a;
    --pigpen-panel-strong: #252a31;
    --pigpen-line: #323944;
    --pigpen-line-strong: #424b58;
    --pigpen-text: #f1f5f9;
    --pigpen-muted: #a1adbd;
    --pigpen-blue: #213147;
    --pigpen-blue-strong: #39526f;
    --pigpen-red: #3a2528;
    --pigpen-red-strong: #684147;
    --pigpen-green: #203629;
    --pigpen-green-strong: #3f684c;
    --pigpen-amber: #3c321f;
    --pigpen-amber-strong: #6f5b31;
    --pigpen-hover-filter: brightness(1.1);
}

.pigpen-tool *,
.pigpen-tool *::before,
.pigpen-tool *::after {
    box-sizing: border-box;
}

.pigpen-tool [hidden] {
    display: none !important;
}

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

.tool-template.tool-template-pigpen-wide {
    max-width: min(1480px, calc(100vw - 56px));
}

.tool-template-pigpen-wide .tool-template-card {
    overflow: hidden;
}

.pigpen-shell {
    max-width: 100%;
    padding: 8px;
    overflow: hidden;
}

.pigpen-topbar,
.pigpen-actions,
.pigpen-io-head,
.pigpen-io-footer,
.pigpen-mini-actions,
.pigpen-export-actions,
.pigpen-chips {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
}

.pigpen-topbar {
    justify-content: space-between;
    gap: 14px 18px;
    margin-bottom: 20px;
}

.pigpen-mode {
    display: inline-flex;
    gap: 4px;
    padding: 5px;
    border: 1px solid var(--pigpen-line);
    border-radius: 999px;
    background: #f1f2f4;
}

html[data-theme="dark"] .pigpen-mode {
    background: #1c2026;
}

.pigpen-mode-button,
.pigpen-button,
.pigpen-tool-button,
.pigpen-mini-button,
.pigpen-stepper-button,
.pigpen-alphabet-key,
.pigpen-collapse-toggle {
    color: var(--pigpen-text);
    font: inherit;
}

.pigpen-mode-button,
.pigpen-button,
.pigpen-tool-button,
.pigpen-mini-button,
.pigpen-stepper-button,
.pigpen-alphabet-key {
    cursor: pointer;
}

.pigpen-mode-button {
    min-height: 42px;
    padding: 10px 24px;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: #6f7786;
    font-size: 0.95rem;
    font-weight: 700;
    transition: background-color 0.18s ease, color 0.18s ease;
}

html[data-theme="dark"] .pigpen-mode-button {
    color: #aeb8c8;
}

.pigpen-mode-button[aria-pressed="true"] {
    background: var(--pigpen-blue);
    color: var(--pigpen-text);
    box-shadow: inset 0 0 0 1px rgba(180, 194, 214, 0.7);
}

.pigpen-actions {
    justify-content: flex-end;
    gap: 8px;
}

.pigpen-button,
.pigpen-tool-button,
.pigpen-mini-button,
.pigpen-stepper-button {
    border: 1px solid var(--pigpen-line);
    background: var(--pigpen-panel-strong);
    font-weight: 700;
    transition: background-color 0.16s ease, border-color 0.16s ease, filter 0.16s ease, opacity 0.16s ease;
}

.pigpen-button {
    min-height: 40px;
    padding: 10px 16px;
    border-radius: 12px;
    font-size: 0.9rem;
    white-space: nowrap;
}

.pigpen-button-danger {
    border-color: var(--pigpen-red-strong);
    background: var(--pigpen-red);
    color: #69484d;
}

html[data-theme="dark"] .pigpen-button-danger {
    color: #ffadb5;
}

.pigpen-button:hover,
.pigpen-tool-button:hover,
.pigpen-mini-button:hover,
.pigpen-stepper-button:hover,
.pigpen-alphabet-key[data-pigpen-clickable="true"]:hover {
    border-color: var(--pigpen-line-strong);
    filter: var(--pigpen-hover-filter);
}

.pigpen-mode-button:focus-visible,
.pigpen-button:focus-visible,
.pigpen-tool-button:focus-visible,
.pigpen-mini-button:focus-visible,
.pigpen-stepper-button:focus-visible,
.pigpen-alphabet-key:focus-visible,
.pigpen-collapse-toggle:focus-visible,
.pigpen-tool textarea:focus-visible,
.pigpen-tool input:focus-visible {
    outline: 3px solid rgba(43, 110, 246, 0.22);
    outline-offset: 2px;
}

.pigpen-button:disabled,
.pigpen-tool-button:disabled,
.pigpen-mini-button:disabled,
.pigpen-stepper-button:disabled {
    cursor: not-allowed;
    opacity: 0.52;
    transform: none;
}

.pigpen-grid {
    display: grid;
    grid-template-columns: minmax(260px, var(--pigpen-sidebar-width)) minmax(0, 1fr);
    gap: 20px;
    align-items: start;
    min-width: 0;
}

.pigpen-sidebar {
    position: sticky;
    top: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-width: 0;
}

.pigpen-workspace {
    display: grid;
    gap: 20px;
    min-width: 0;
}

.pigpen-section,
.pigpen-io {
    min-width: 0;
    border: 1px solid var(--pigpen-line);
    border-radius: 16px;
    background: rgba(248, 248, 249, 0.9);
    padding: 18px;
}

html[data-theme="dark"] .pigpen-section,
html[data-theme="dark"] .pigpen-io {
    background: rgba(32, 36, 42, 0.78);
}

.pigpen-section-title {
    margin: 0 0 14px;
    color: var(--pigpen-text);
    font-size: 0.95rem;
    font-weight: 800;
    line-height: 1.2;
}

.pigpen-collapse-toggle {
    display: block;
    width: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    text-align: left;
}

.pigpen-collapse-action {
    display: none;
}

.pigpen-option-list {
    display: grid;
    gap: 13px;
}

.pigpen-option-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    color: #40495a;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
}

html[data-theme="dark"] .pigpen-option-row {
    color: #d8dee8;
}

.pigpen-option-row-stepper {
    cursor: default;
}

.pigpen-option-row input[type="checkbox"] {
    width: 20px;
    height: 20px;
    flex: 0 0 auto;
    margin: 0;
    accent-color: #3f7fd8;
    cursor: pointer;
}

.pigpen-stepper {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.pigpen-stepper-button {
    display: inline-grid;
    width: 36px;
    height: 36px;
    padding: 0;
    place-items: center;
    border-radius: 8px;
    color: var(--pigpen-muted);
    font-size: 1.1rem;
}

.pigpen-stepper-value {
    min-width: 38px;
    color: var(--pigpen-text);
    font-size: 0.86rem;
    font-weight: 800;
    text-align: center;
}

.pigpen-tool-buttons {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
}

.pigpen-tool-button {
    min-height: 40px;
    padding: 9px 10px;
    border-radius: 10px;
    color: var(--pigpen-muted);
    font-size: 0.8rem;
}

.pigpen-tool-button-primary {
    grid-column: 1 / -1;
    border-color: var(--pigpen-blue-strong);
    background: var(--pigpen-blue);
    color: var(--pigpen-text);
}

.pigpen-punctuation-row {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 6px;
    margin-top: 9px;
}

.pigpen-punctuation-row .pigpen-tool-button {
    min-width: 0;
    padding: 7px 3px;
    font-family: var(--pigpen-mono);
    font-size: 1rem;
}

.pigpen-validation {
    padding: 11px 12px;
    border: 1px solid var(--pigpen-green-strong);
    border-radius: 10px;
    background: var(--pigpen-green);
    color: #226332;
    font-size: 0.82rem;
    font-weight: 700;
    line-height: 1.45;
}

html[data-theme="dark"] .pigpen-validation {
    color: #a8e3b6;
}

.pigpen-help {
    margin: 10px 0 0;
    color: var(--pigpen-muted);
    font-size: 0.79rem;
    line-height: 1.5;
}

.pigpen-io-grid {
    display: grid;
    grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr);
    gap: 20px;
    min-width: 0;
}

.pigpen-io {
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: border-color 0.18s ease, box-shadow 0.18s ease;
}

.pigpen-io:focus-within {
    border-color: var(--pigpen-blue-strong);
    box-shadow: 0 0 0 3px rgba(78, 127, 210, 0.1);
}

.pigpen-io-head {
    justify-content: space-between;
    gap: 10px;
}

.pigpen-io-head h2 {
    margin: 0;
    color: var(--pigpen-text);
    font-size: 1.05rem;
    font-weight: 750;
    letter-spacing: -0.01em;
}

.pigpen-mini-actions,
.pigpen-export-actions {
    justify-content: flex-end;
    gap: 6px;
}

.pigpen-mini-button {
    display: inline-flex;
    min-height: 32px;
    padding: 6px 12px;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    color: var(--pigpen-muted);
    font-size: 0.78rem;
}

.pigpen-mini-button.is-done {
    border-color: var(--pigpen-green-strong);
    background: var(--pigpen-green);
    color: #226332;
}

.pigpen-mini-button.is-error {
    border-color: var(--pigpen-amber-strong);
    background: var(--pigpen-amber);
    color: #7b5c1c;
}

html[data-theme="dark"] .pigpen-mini-button.is-done {
    color: #a8e3b6;
}

html[data-theme="dark"] .pigpen-mini-button.is-error {
    color: #f4d58b;
}

.tool-template.tool-template-pigpen-wide .pigpen-io textarea {
    width: 100%;
    max-width: 100%;
    min-height: 300px;
    height: 300px;
    resize: vertical;
    border: 1px solid var(--pigpen-line-strong);
    border-radius: 12px;
    background: var(--pigpen-panel-strong);
    padding: 14px;
    color: var(--pigpen-text);
    font-family: var(--pigpen-mono);
    font-size: 1rem;
    line-height: 1.5;
}

.pigpen-tool textarea::placeholder {
    color: #8a94a4;
}

.pigpen-output {
    font-weight: 500;
}

.pigpen-visual-output {
    display: flex;
    width: 100%;
    min-height: 300px;
    max-height: 520px;
    padding: 18px;
    align-content: flex-start;
    align-items: flex-start;
    flex-wrap: wrap;
    overflow: auto;
    border: 1px solid var(--pigpen-line-strong);
    border-radius: 12px;
    background:
        linear-gradient(rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.9)),
        repeating-linear-gradient(0deg, transparent 0, transparent 31px, rgba(216, 219, 224, 0.6) 32px);
    color: var(--pigpen-text);
}

html[data-theme="dark"] .pigpen-visual-output {
    background:
        linear-gradient(rgba(37, 42, 49, 0.94), rgba(37, 42, 49, 0.94)),
        repeating-linear-gradient(0deg, transparent 0, transparent 31px, rgba(66, 75, 88, 0.7) 32px);
}

.pigpen-visual-output-empty {
    align-items: center;
    justify-content: center;
}

.pigpen-empty-state {
    max-width: 350px;
    padding: 30px 16px;
    color: var(--pigpen-muted);
    font-size: 0.9rem;
    line-height: 1.55;
    text-align: center;
}

.pigpen-glyph-wrap {
    display: inline-flex;
    flex: 0 0 auto;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    color: var(--pigpen-text);
}

.pigpen-glyph-wrap svg,
.pigpen-alphabet-symbol svg {
    display: block;
    overflow: visible;
}

.pigpen-glyph-hint {
    margin-top: 2px;
    color: var(--pigpen-muted);
    font-family: var(--pigpen-mono);
    font-size: 0.68rem;
    font-weight: 800;
    line-height: 1;
}

.pigpen-glyph-space {
    display: inline-block;
    height: 1px;
    flex: 0 0 auto;
}

.pigpen-glyph-break {
    width: 0;
    height: 0;
    flex-basis: 100%;
}

.pigpen-glyph-punctuation {
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    color: var(--pigpen-text);
    font-family: var(--pigpen-mono);
    font-weight: 800;
}

.pigpen-io-footer {
    gap: 12px;
    padding-top: 4px;
    margin-top: 4px;
    border-top: 1px solid var(--pigpen-line);
}

.pigpen-stat {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding-top: 8px;
    color: var(--pigpen-muted);
    font-size: 0.8rem;
    font-weight: 600;
}

.pigpen-stat strong {
    color: var(--pigpen-text);
    font-weight: 800;
}

.pigpen-alphabet-section {
    background: var(--pigpen-panel-strong);
}

.pigpen-alphabet-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 14px;
}

.pigpen-alphabet-head .pigpen-section-title {
    margin-bottom: 0;
}

.pigpen-alphabet-head .pigpen-help {
    max-width: 75ch;
    margin-top: 6px;
}

.pigpen-mode-note {
    max-width: 42ch;
    margin: 0;
    color: var(--pigpen-muted);
    font-size: 0.78rem;
    line-height: 1.45;
    text-align: right;
}

.pigpen-decoder-tip {
    display: flex;
    align-items: flex-start;
    gap: 9px;
    padding: 10px 12px;
    margin-bottom: 12px;
    border: 1px solid var(--pigpen-blue-strong);
    border-radius: 11px;
    background: #f2f7fd;
    color: #354967;
    font-size: 0.82rem;
    font-weight: 600;
    line-height: 1.45;
}

html[data-theme="dark"] .pigpen-decoder-tip {
    background: #213147;
    color: #c7dcf7;
}

.pigpen-alphabet-grid {
    display: grid;
    grid-template-columns: repeat(13, minmax(54px, 1fr));
    gap: 8px;
}

.pigpen-alphabet-key {
    display: flex;
    min-width: 0;
    min-height: 92px;
    padding: 8px 4px 7px;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border: 1px solid var(--pigpen-line);
    border-radius: 12px;
    background: var(--pigpen-panel-strong);
    color: var(--pigpen-text);
    cursor: default;
    transition: transform 0.15s ease, background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
}

.pigpen-alphabet-key[data-pigpen-clickable="true"] {
    cursor: pointer;
}

.pigpen-alphabet-key[data-pigpen-clickable="true"]:hover {
    border-color: var(--pigpen-blue-strong);
    background: #f8fbff;
    box-shadow: 0 5px 14px rgba(34, 56, 88, 0.08);
}

html[data-theme="dark"] .pigpen-alphabet-key[data-pigpen-clickable="true"]:hover {
    background: #293442;
}

.pigpen-alphabet-letter {
    color: var(--pigpen-muted);
    font-family: var(--pigpen-mono);
    font-size: 0.78rem;
    font-weight: 800;
}

.pigpen-alphabet-symbol {
    color: var(--pigpen-text);
}

.pigpen-chips {
    gap: 7px;
}

.pigpen-chip {
    gap: 4px;
    border-color: var(--pigpen-line);
}

@media (min-width: 1101px) {
    .pigpen-topbar {
        display: grid;
        grid-template-columns: var(--pigpen-sidebar-width) minmax(0, 1fr);
    }

    .pigpen-topbar .tool-topbar-start,
    .pigpen-mode {
        width: 100%;
    }

    .pigpen-mode-button {
        flex: 1 1 0;
        text-align: center;
    }

    .pigpen-topbar .tool-topbar-end {
        display: flex;
        justify-content: flex-end;
    }
}

@media (max-width: 1260px) {
    .pigpen-alphabet-grid {
        grid-template-columns: repeat(9, minmax(54px, 1fr));
    }
}

@media (max-width: 1100px) {
    .pigpen-topbar {
        justify-content: center;
    }

    .pigpen-topbar .tool-topbar-start,
    .pigpen-topbar .tool-topbar-end {
        display: flex;
        width: auto;
        max-width: 100%;
        justify-content: center;
    }

    .pigpen-grid {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 18px;
    }

    .pigpen-sidebar,
    .pigpen-workspace {
        display: contents;
    }

    .pigpen-io-grid {
        order: 1;
    }

    .pigpen-decoder-section {
        order: 2;
    }

    .pigpen-alphabet-section {
        order: 3;
    }

    .pigpen-display-section {
        order: 4;
    }

    .pigpen-key-section {
        order: 5;
    }

    .pigpen-properties-section {
        order: 6;
    }

    .pigpen-display-section,
    .pigpen-decoder-section,
    .pigpen-key-section {
        width: min(100%, 620px);
        align-self: center;
    }
}

@media (min-width: 761px) and (max-width: 1100px) {
    .pigpen-actions {
        width: fit-content;
        max-width: 100%;
        justify-content: center;
    }
}

@media (max-width: 900px) {
    .pigpen-io-grid {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 760px) {
    .tool-template.tool-template-pigpen-wide {
        max-width: min(100%, calc(100vw - 24px));
    }

    .pigpen-shell {
        padding: 0;
    }

    .pigpen-topbar {
        align-items: stretch;
        flex-direction: column;
        gap: 12px;
    }

    .pigpen-topbar .tool-topbar-start,
    .pigpen-topbar .tool-topbar-end,
    .pigpen-mode,
    .pigpen-actions {
        width: 100%;
    }

    .pigpen-mode-button,
    .pigpen-actions .pigpen-button {
        flex: 1 1 0;
    }

    .pigpen-actions {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .pigpen-actions .pigpen-button {
        width: 100%;
        padding-right: 9px;
        padding-left: 9px;
        white-space: normal;
    }

    .pigpen-grid {
        gap: 16px;
    }

    .pigpen-section,
    .pigpen-io {
        padding: 14px;
        border-radius: 14px;
    }

    .tool-template.tool-template-pigpen-wide .pigpen-io textarea,
    .pigpen-visual-output {
        min-height: 250px;
        height: clamp(250px, 38vh, 420px);
    }

    .pigpen-alphabet-head {
        flex-direction: column;
        gap: 8px;
    }

    .pigpen-mode-note {
        text-align: left;
    }

    .pigpen-collapse-toggle {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 12px;
        cursor: pointer;
    }

    .pigpen-collapse-action {
        display: inline-flex;
        align-items: center;
        gap: 8px;
        color: var(--pigpen-muted);
        font-size: 0.72rem;
        font-weight: 800;
        text-transform: uppercase;
    }

    .pigpen-collapse-action-text::before {
        content: "Hide";
    }

    .pigpen-section.is-collapsed .pigpen-collapse-action-text::before {
        content: "Show";
    }

    .pigpen-collapse-icon {
        position: relative;
        display: inline-grid;
        width: 28px;
        height: 28px;
        flex: 0 0 auto;
        place-items: center;
        border: 1px solid var(--pigpen-line);
        border-radius: 999px;
        background: var(--pigpen-panel-strong);
    }

    .pigpen-collapse-icon::before,
    .pigpen-collapse-icon::after {
        content: "";
        position: absolute;
        width: 10px;
        height: 2px;
        border-radius: 999px;
        background: currentColor;
        transition: opacity 0.16s ease, transform 0.16s ease;
    }

    .pigpen-collapse-icon::after {
        transform: rotate(90deg);
    }

    .pigpen-section:not(.is-collapsed) .pigpen-collapse-icon::after {
        opacity: 0;
        transform: rotate(90deg) scaleX(0);
    }

    .pigpen-section.is-collapsed .pigpen-section-title {
        margin-bottom: 0;
    }

    .pigpen-section.is-collapsed [data-pigpen-collapsible-panel],
    .pigpen-section.is-collapsed .pigpen-alphabet-head .pigpen-help,
    .pigpen-section.is-collapsed .pigpen-mode-note {
        display: none;
    }

    .pigpen-section.is-collapsed .pigpen-alphabet-head {
        margin-bottom: 0;
    }

    .pigpen-alphabet-grid {
        grid-template-columns: repeat(5, minmax(52px, 1fr));
        gap: 7px;
    }

    .pigpen-alphabet-key {
        min-height: 84px;
    }

    .pigpen-properties-section .pigpen-chips {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 8px;
    }

    .pigpen-chip {
        justify-content: flex-start;
        min-height: 34px;
        padding: 7px 9px;
        font-size: 0.72rem;
        line-height: 1.1;
    }
}

@media (max-width: 480px) {
    .pigpen-mode-button,
    .pigpen-button {
        padding-right: 11px;
        padding-left: 11px;
        font-size: 0.82rem;
    }

    .pigpen-io-head {
        align-items: flex-start;
    }

    .pigpen-io-head h2 {
        font-size: 1rem;
    }

    .pigpen-visual-output {
        padding: 12px;
    }

    .pigpen-alphabet-grid {
        grid-template-columns: repeat(4, minmax(50px, 1fr));
    }

    .pigpen-properties-section .pigpen-chips {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 350px) {
    .pigpen-option-row-stepper {
        align-items: flex-start;
        flex-direction: column;
    }

    .pigpen-stepper {
        align-self: flex-end;
    }

    .pigpen-alphabet-grid {
        grid-template-columns: repeat(3, minmax(50px, 1fr));
    }
}

@media (prefers-reduced-motion: reduce) {
    .pigpen-tool *,
    .pigpen-tool *::before,
    .pigpen-tool *::after {
        animation: none !important;
        scroll-behavior: auto !important;
        transition: none !important;
    }
}
