/* Reset and base styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    line-height: 1.6;
    color: #333;
    background-color: #f5f5f5;
    min-height: 100vh;
}

/* Header */
header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 1.5rem 2rem;
    text-align: center;
}

header h1 {
    margin-bottom: 0.25rem;
    font-size: 1.75rem;
}

header p {
    opacity: 0.9;
    font-size: 0.9rem;
}

/* Main content */
main {
    padding: 1.5rem;
    max-width: 1600px;
    margin: 0 auto;
}

.container {
    display: grid;
    grid-template-columns: 1fr 1.5fr 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

@media (max-width: 1200px) {
    .container {
        grid-template-columns: 1fr 1fr;
    }
    .visual-panel {
        grid-column: span 2;
        order: -1;
    }
}

@media (max-width: 768px) {
    .container {
        grid-template-columns: 1fr;
    }
    .visual-panel {
        grid-column: span 1;
    }
}

/* Panels */
.panel {
    background: white;
    border-radius: 8px;
    padding: 1.25rem;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.panel h2 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px solid #667eea;
    color: #444;
}

.panel h3 {
    font-size: 0.95rem;
    margin: 1rem 0 0.5rem;
    color: #555;
}

/* Form elements */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.35rem;
    font-weight: 500;
    color: #555;
    font-size: 0.9rem;
}

select, input[type="text"], textarea {
    width: 100%;
    padding: 0.6rem;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 0.9rem;
    font-family: inherit;
    transition: border-color 0.2s, box-shadow 0.2s;
}

select:focus, input[type="text"]:focus, textarea:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

textarea {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 0.8rem;
    resize: vertical;
}

/* Buttons */
.button-group {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.btn {
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 4px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: #667eea;
    color: white;
}

.btn-primary:hover:not(:disabled) {
    background: #5a6fd6;
}

.btn-secondary {
    background: #e9ecef;
    color: #495057;
}

.btn-secondary:hover:not(:disabled) {
    background: #dee2e6;
}

/* Result boxes */
.result-box {
    margin-top: 1rem;
    padding: 0.75rem;
    border-radius: 4px;
    font-size: 0.9rem;
}

.result-box.success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.result-box.error {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.result-box.info {
    background: #cce5ff;
    color: #004085;
    border: 1px solid #b8daff;
}

.hidden {
    display: none !important;
}

/* Live semantics panel */
.semantics-group {
    margin-top: 1rem;
}

.semantics-group label {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    margin-bottom: 0.35rem;
    font-weight: 500;
    color: #555;
    font-size: 0.9rem;
}

.semantics-hint {
    font-weight: 400;
    font-size: 0.72rem;
    color: #999;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

/* A blank alphabet field means Σ = ∅; shown inline only while it's empty. */
.alphabet-empty-note {
    display: none;
    color: #667eea;
    font-weight: 500;
}

.alphabet-empty-note.visible {
    display: inline;
}

.field-hint {
    display: block;
    margin-top: 0.2rem;
    font-size: 0.72rem;
    color: #999;
}

.field-warn {
    display: block;
    position: relative;
    margin-top: 0.3rem;
    font-size: 0.74rem;
    color: #b02a37;
    background: #fdecee;
    border: 1px solid #f3c2c7;
    border-radius: 4px;
    padding: 0.3rem 1.4rem 0.3rem 0.45rem;
}

.field-warn-close {
    position: absolute;
    top: 0.15rem;
    right: 0.2rem;
    width: 1.1rem;
    height: 1.1rem;
    padding: 0;
    border: none;
    background: transparent;
    color: #b02a37;
    font-size: 1rem;
    line-height: 1;
    cursor: pointer;
    border-radius: 3px;
}

.field-warn-close:hover {
    background: #f3c2c7;
}

.field-warn.hidden {
    display: none;
}

/* Informational counterpart to .field-warn: something happened and here is what
   it means. Not an error, so it reads calm rather than red. */
.field-note {
    display: block;
    position: relative;
    margin-top: 0.3rem;
    font-size: 0.74rem;
    color: #33507a;
    background: #eef3fb;
    border: 1px solid #c9d8ef;
    border-radius: 4px;
    padding: 0.4rem 1.4rem 0.4rem 0.5rem;
    line-height: 1.4;
}

.field-note.hidden {
    display: none;
}

/* Consequences of the cascade that its button label cannot carry. Muted
   against the warning body: this is context, not a second alarm. */
.field-warn-note {
    margin-top: 0.3rem;
    color: #7d4f55;
    line-height: 1.4;
}

.field-warn-actions {
    display: flex;
    gap: 0.4rem;
    flex-wrap: wrap;
    margin-top: 0.4rem;
}

.btn-warn {
    padding: 0.25rem 0.55rem;
    border: 1px solid #e2a7ac;
    border-radius: 4px;
    background: #fff;
    color: #b02a37;
    font-size: 0.72rem;
    font-weight: 500;
    cursor: pointer;
    width: auto;
}

.btn-warn:hover {
    background: #fbe3e5;
}

.btn-warn.danger {
    background: #dc3545;
    color: #fff;
    border-color: #dc3545;
}

.btn-warn.danger:hover {
    background: #c42d3b;
}

.semantics-panel {
    border: 1px solid #e3e6ea;
    border-radius: 6px;
    background: #f8f9fb;
    padding: 0.4rem 0.75rem;
    font-size: 0.85rem;
}

.semantics-empty {
    color: #888;
    font-style: italic;
    padding: 0.4rem 0;
}

.sem-incomplete {
    color: #9a6a00;
    padding: 0.4rem 0;
}

/* Structural integrity verdict banner (rendered above the value rows). */
.sem-structural {
    border-radius: 5px;
    padding: 0.45rem 0.55rem;
    margin-bottom: 0.5rem;
    border: 1px solid transparent;
}

.sem-structural.ok {
    background: #eaf7ee;
    border-color: #bfe3c9;
}

.sem-structural.warn {
    background: #fff6e5;
    border-color: #f0d9a8;
}

.sem-structural-head {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.sem-structural-badge {
    font-weight: 700;
    font-size: 0.8rem;
    white-space: nowrap;
}

.sem-structural.ok .sem-structural-badge { color: #1c7c3a; }
.sem-structural.warn .sem-structural-badge { color: #9a6a00; }

.sem-structural-summary {
    color: #444;
    font-size: 0.82rem;
    line-height: 1.35;
}

.sem-issues {
    margin: 0.35rem 0 0;
    padding-left: 1.1rem;
    color: #6b5323;
    font-size: 0.8rem;
    line-height: 1.4;
}

.sem-issues li { margin: 0.1rem 0; }

.sem-row {
    display: grid;
    grid-template-columns: 9.5rem 1fr;
    gap: 0.5rem;
    align-items: baseline;
    padding: 0.3rem 0;
    border-bottom: 1px solid #eef0f3;
}

.sem-row:last-child {
    border-bottom: none;
}

.sem-label {
    color: #667;
    font-weight: 500;
}

.sem-value code {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    background: #eef1ff;
    color: #2b2f77;
    padding: 0.05rem 0.3rem;
    border-radius: 3px;
    margin-right: 0.2rem;
    display: inline-block;
}

/* Regex can be long and unsimplified — let it fill the column and wrap,
   breaking between tokens where possible and only mid-token when forced. */
.sem-value .sem-regex {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    background: #eef1ff;
    color: #2b2f77;
    font-weight: 600;
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 0.35rem 0.45rem;
    border-radius: 4px;
    line-height: 1.5;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: normal;
    max-height: 8rem;
    overflow-y: auto;
}

.sem-english {
    color: #2b2f4a;
    font-weight: 500;
    line-height: 1.45;
}

.sem-delta-desc {
    margin-top: 0.25rem;
    color: #555;
    font-size: 0.8rem;
    font-style: italic;
}

.sem-eps {
    font-style: italic;
    color: #667eea;
    margin-right: 0.2rem;
}

.sem-none {
    color: #aab;
    font-style: italic;
}

.sem-special {
    color: #555;
    font-style: italic;
}

.sem-value.sem-warn code {
    background: #fff1d6;
    color: #8a5a00;
}

.sem-muted {
    color: #999;
}

/* Perspective sections (Behavioral / Structural / Evolution) */
.sem-section {
    margin-top: 0.6rem;
    padding-top: 0.5rem;
    border-top: 2px solid #e3e6ea;
}

.sem-section:first-child {
    margin-top: 0;
    padding-top: 0;
    border-top: none;
}

.sem-section-head {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 0.35rem;
}

.sem-section-title {
    font-weight: 700;
    font-size: 0.9rem;
    color: #2b2f4a;
}

.sem-section-q {
    color: #999;
    font-size: 0.78rem;
    font-style: italic;
}

.sem-label.sem-added {
    color: #1c7c3a;
    font-weight: 600;
}

.sem-label.sem-removed {
    color: #b3261e;
    font-weight: 600;
}

.sem-delta-line {
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0.2rem 0;
}

.sem-delta-line.sem-added {
    color: #1c7c3a;
}

.sem-delta-line.sem-removed {
    color: #b3261e;
}

/* Diagram container */
#diagram-container {
    min-height: 300px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #fafafa;
    border: 1px dashed #ddd;
    border-radius: 4px;
    overflow: auto;
}

#diagram-container .placeholder {
    color: #999;
    font-style: italic;
}

#diagram-container img, #diagram-container svg {
    max-width: 100%;
    height: auto;
}

/* Tape visualization */
#tape {
    display: flex;
    gap: 2px;
    padding: 0.5rem;
    background: #f8f9fa;
    border-radius: 4px;
    overflow-x: auto;
    min-height: 50px;
    align-items: center;
}

.tape-cell {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #dee2e6;
    background: white;
    font-family: monospace;
    font-size: 1.1rem;
    font-weight: bold;
    border-radius: 4px;
    transition: all 0.2s;
}

.tape-cell.current {
    border-color: #667eea;
    background: #667eea;
    color: white;
    transform: scale(1.1);
}

.tape-cell.processed {
    background: #e9ecef;
    color: #6c757d;
}

.tape-cell.blank {
    color: #ccc;
}

/* Stack visualization */
#stack {
    display: flex;
    flex-direction: column-reverse;
    gap: 2px;
    padding: 0.5rem;
    background: #f8f9fa;
    border-radius: 4px;
    min-height: 100px;
    max-height: 200px;
    overflow-y: auto;
}

.stack-cell {
    padding: 0.4rem 0.75rem;
    background: white;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9rem;
    text-align: center;
}

.stack-cell.top {
    background: #667eea;
    color: white;
    border-color: #667eea;
}

/* Current state display */
#current-state {
    padding: 0.75rem;
    background: #f8f9fa;
    border-radius: 4px;
    font-family: monospace;
    font-size: 1rem;
}

.state-badge {
    display: inline-block;
    padding: 0.25rem 0.75rem;
    background: #667eea;
    color: white;
    border-radius: 20px;
    margin: 0.2rem;
    font-weight: 500;
}

.state-badge.final {
    background: #28a745;
}

.state-badge.rejected {
    background: #dc3545;
}

/* Trace display */
#trace {
    max-height: 200px;
    overflow-y: auto;
    font-family: monospace;
    font-size: 0.8rem;
    background: #f8f9fa;
    padding: 0.5rem;
    border-radius: 4px;
}

.trace-step {
    padding: 0.35rem;
    border-bottom: 1px solid #eee;
}

.trace-step:last-child {
    border-bottom: none;
}

.trace-step.current {
    background: #fff3cd;
}

.trace-step.accepted {
    background: #d4edda;
}

.trace-step.rejected {
    background: #f8d7da;
}

/* Transformations panel */
.transformations-panel {
    margin-top: 0;
}

.transformations-panel .button-group {
    margin-bottom: 1rem;
}

.regex-input {
    display: flex;
    gap: 0.5rem;
    align-items: flex-end;
}

.regex-input .form-group {
    flex: 1;
    margin-bottom: 0;
}

.regex-input input {
    margin-top: 0.35rem;
}

/* Step controls */
.step-controls {
    margin-top: 1.5rem;
    padding-top: 1rem;
    border-top: 1px solid #eee;
}

/* Loading state */
.loading {
    opacity: 0.6;
    pointer-events: none;
}

/* Animation for state transitions */
@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

.state-badge.active {
    animation: pulse 0.5s ease-in-out;
}

/* Mode toggle */
.mode-toggle {
    display: flex;
    margin-bottom: 1rem;
    border-radius: 4px;
    overflow: hidden;
    border: 1px solid #ddd;
}

.mode-btn {
    flex: 1;
    padding: 0.5rem 1rem;
    border: none;
    background: #f8f9fa;
    color: #555;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
}

.mode-btn:first-child {
    border-right: 1px solid #ddd;
}

.mode-btn.active {
    background: #667eea;
    color: white;
}

.mode-btn:hover:not(.active) {
    background: #e9ecef;
}

/* Editor tabs (Visual Editor / Machine JSON) */
.editor-tabs {
    display: flex;
    gap: 0.25rem;
    margin-bottom: 1rem;
    border-bottom: 2px solid #667eea;
}

.editor-tab {
    padding: 0.5rem 1rem;
    border: none;
    background: transparent;
    color: #888;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    border-radius: 6px 6px 0 0;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: color 0.2s, background 0.2s, border-color 0.2s;
}

.editor-tab:hover {
    color: #667eea;
    background: #f0f2ff;
}

.editor-tab.active {
    color: #667eea;
    border-bottom-color: #667eea;
}

.editor-tab-pane .form-group {
    margin-bottom: 0;
}

/* Visual editor */
.visual-editor {
    border: 1px solid #ddd;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 1rem;
}

#cytoscape-container {
    width: 100%;
    height: 450px;
    background: #fafafa;
}

/* Visual panel takes more space */
.visual-panel .visual-editor {
    margin-bottom: 0;
}

.visual-editor-toolbar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem;
    background: #f8f9fa;
    border-top: 1px solid #eee;
}

.visual-editor-toolbar .btn-sm {
    padding: 0.35rem 0.75rem;
    font-size: 0.8rem;
}

.toolbar-hint {
    font-size: 0.75rem;
    color: #888;
    margin-left: auto;
}

.visual-editor-info {
    display: flex;
    gap: 1rem;
    padding: 0.4rem 0.5rem;
    background: #f0f0f0;
    font-size: 0.8rem;
    color: #666;
    border-top: 1px solid #eee;
}

/* Symbol manager */
.symbol-manager {
    margin-top: 1rem;
    padding-top: 1rem;
    border-top: 1px solid #eee;
}

.symbol-manager h4 {
    margin: 0 0 0.75rem;
    font-size: 0.95rem;
    color: #555;
}

.symbol-list {
    display: flex;
    flex-wrap: wrap;
    gap: 0.25rem;
    margin-bottom: 0.5rem;
    min-height: 28px;
}

.symbol-tag {
    display: inline-flex;
    align-items: center;
    padding: 0.2rem 0.5rem;
    background: #e9ecef;
    border-radius: 4px;
    font-size: 0.85rem;
    font-family: monospace;
}

.symbol-tag .remove-symbol {
    margin-left: 0.35rem;
    cursor: pointer;
    color: #999;
    font-weight: bold;
}

.symbol-tag .remove-symbol:hover {
    color: #dc3545;
}

.symbol-input {
    width: 100px;
    padding: 0.3rem 0.5rem;
    font-size: 0.85rem;
}

/* Context menu */
.context-menu {
    position: fixed;
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    z-index: 1001;
    min-width: 150px;
}

.context-menu-item {
    padding: 0.5rem 1rem;
    cursor: pointer;
    font-size: 0.9rem;
}

.context-menu-item:hover {
    background: #f0f0f0;
}

.context-menu-item.danger {
    color: #dc3545;
}

.context-menu-item.danger:hover {
    background: #f8d7da;
}

/* Visual editor empty state */
.visual-editor-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #888;
    text-align: center;
    padding: 2rem;
}

.visual-editor-empty p {
    margin: 0.5rem 0;
}

.visual-editor-empty .hint {
    font-size: 0.85rem;
    color: #aaa;
}
