:root {
    color-scheme: dark;
    font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --bg: #030714;
    --panel: rgba(12, 20, 38, 0.82);
    --panel-strong: rgba(16, 28, 52, 0.95);
    --border: rgba(148, 163, 184, 0.16);
    --border-strong: rgba(96, 165, 250, 0.32);
    --text-primary: #e2e8f0;
    --text-muted: #94a3b8;
    --accent: #60a5fa;
    --accent-strong: #2563eb;
    --accent-soft: rgba(96, 165, 250, 0.2);
    --danger: #f87171;
    --warning: #fbbf24;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    background:
        radial-gradient(at top, rgba(37, 99, 235, 0.22), transparent 55%),
        radial-gradient(at 20% 100%, rgba(14, 165, 233, 0.18), transparent 60%),
        #030714;
    color: var(--text-primary);
    padding: clamp(1.5rem, 2vw, 2.5rem);
    position: relative;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
}

body::before {
    content: '';
    position: fixed;
    inset: 0;
    background:
        linear-gradient(120deg, rgba(59, 130, 246, 0.12), transparent 55%),
        linear-gradient(300deg, rgba(30, 64, 175, 0.18), transparent 40%);
    opacity: 0.9;
    pointer-events: none;
    z-index: 0;
}

main.layout {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    position: relative;
    z-index: 1;
    flex: 1;
    min-height: 0;
}

body.legal-page {
    padding: 4rem 1.5rem;
}

body.legal-page::before {
    opacity: 0.75;
}

h1,
h2,
h3 {
    font-family: 'Space Grotesk', 'Inter', sans-serif;
    font-weight: 600;
    margin: 0;
    color: #f8fafc;
}

p {
    margin: 0;
    line-height: 1.6;
}

small {
    font-size: 0.85rem;
}

.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;
}

.muted {
    color: var(--text-muted);
}

.card {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 1.25rem;
    padding: 1.75rem;
    box-shadow: 0 30px 60px rgba(2, 6, 23, 0.45);
    backdrop-filter: blur(18px);
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1.5rem;
}

.brand {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.brand__icon {
    display: grid;
    place-items: center;
    width: 56px;
    height: 56px;
    border-radius: 1rem;
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.95), rgba(14, 165, 233, 0.7));
    color: #020617;
    font-weight: 600;
    font-size: 1.8rem;
    letter-spacing: -0.01em;
}

.brand__copy h1 {
    font-size: clamp(1.9rem, 4vw, 2.4rem);
}

.brand__copy p {
    margin-top: 0.35rem;
}

.session {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.session__status {
    padding: 0.35rem 0.75rem;
    border-radius: 999px;
    background: rgba(148, 163, 184, 0.1);
    border: 1px solid rgba(148, 163, 184, 0.2);
}

button {
    font: inherit;
    font-weight: 600;
    color: #f8fafc;
    background: linear-gradient(135deg, var(--accent-strong), #38bdf8);
    border: none;
    border-radius: 0.95rem;
    padding: 0.75rem 1.4rem;
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.2s ease, background 0.2s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    box-shadow: 0 18px 35px rgba(37, 99, 235, 0.25);
}

button:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 20px 40px rgba(30, 64, 175, 0.35);
}

button:active:not(:disabled) {
    transform: translateY(0);
}

button:disabled {
    background: rgba(148, 163, 184, 0.25);
    color: rgba(148, 163, 184, 0.7);
    box-shadow: none;
    cursor: not-allowed;
}

button.outline {
    background: transparent;
    border: 1px solid rgba(96, 165, 250, 0.65);
    color: var(--accent);
    box-shadow: none;
}

button.outline:hover:not(:disabled) {
    background: rgba(37, 99, 235, 0.15);
}

button.ghost {
    background: transparent;
    border: 1px solid rgba(148, 163, 184, 0.25);
    color: var(--text-muted);
    box-shadow: none;
    padding-inline: 1.1rem;
}

button.ghost:hover:not(:disabled) {
    border-color: rgba(148, 163, 184, 0.45);
    color: var(--text-primary);
}

button.icon-button {
    width: 42px;
    height: 42px;
    border-radius: 999px;
    padding: 0;
    background: rgba(37, 99, 235, 0.16);
    border: 1px solid rgba(96, 165, 250, 0.45);
    box-shadow: none;
}

button.icon-button:hover:not(:disabled) {
    background: rgba(37, 99, 235, 0.24);
}

button.icon-button.ghost {
    background: transparent;
    border-color: rgba(148, 163, 184, 0.35);
}

.auth-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    width: 100%;
    margin-top: 1rem;
}

.legal-card {
    width: min(780px, 100%);
    background: var(--panel-strong);
    border: 1px solid var(--border);
    border-radius: 1.5rem;
    padding: clamp(2rem, 4vw, 2.75rem);
    box-shadow: 0 30px 60px rgba(2, 6, 23, 0.45);
    backdrop-filter: blur(20px);
    display: flex;
    flex-direction: column;
    gap: 1.75rem;
    position: relative;
    z-index: 1;
}

.legal-card header h1 {
    font-size: clamp(2rem, 4vw, 2.4rem);
}

.legal-card h2 {
    font-size: 1.25rem;
    margin-bottom: 0.75rem;
}

.legal-card ul,
.legal-card ol {
    margin: 0;
    padding-left: 1.25rem;
    display: grid;
    gap: 0.6rem;
}

.legal-card li {
    line-height: 1.7;
}

.legal-card a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid transparent;
}

.legal-card a:hover {
    border-bottom-color: var(--accent);
}

.legal-footer,
.legal-links {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    font-size: 0.95rem;
    margin-top: 1rem;
}

.legal-footer a,
.legal-links a {
    color: var(--text-muted);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: color 0.2s ease, border-color 0.2s ease;
}

.legal-footer a:hover,
.legal-links a:hover {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.auth-button {
    width: 100%;
}

.notice-stack {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-height: 2rem;
}

.analysis {
    background: rgba(15, 23, 42, 0.6);
    border: 1px solid rgba(148, 163, 184, 0.25);
    border-radius: 1rem;
    padding: 1.25rem;
    margin-bottom: 1.25rem;
    transition: opacity 0.2s ease;
}

.analysis__header {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.75rem;
}

.analysis__header h3 {
    font-size: 1.1rem;
}

.analysis__diagram {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    padding: 0.75rem;
    background: rgba(15, 23, 42, 0.45);
    border-radius: 0.85rem;
    border: 1px solid rgba(148, 163, 184, 0.12);
}

.analysis__diagram[role='button'] {
    cursor: pointer;
}

.analysis__diagram[role='button']:focus {
    outline: 2px solid var(--accent);
    outline-offset: 4px;
}

.analysis__diagram-output {
    min-height: 120px;
    padding: 0.75rem;
    border-radius: 0.75rem;
    border: 1px solid rgba(148, 163, 184, 0.14);
    background: rgba(2, 6, 23, 0.55);
    overflow-x: auto;
}

.analysis__diagram::-webkit-scrollbar {
    height: 6px;
}

.analysis__diagram::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.35);
    border-radius: 999px;
}

.analysis__diagram-output::-webkit-scrollbar {
    height: 6px;
}

.analysis__diagram-output::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.25);
    border-radius: 999px;
}

.analysis__diagram .mermaid {
    min-width: 420px;
    font-size: 0.9rem;
}

body.workflow-overlay-open {
    overflow: hidden;
}

.workflow-overlay {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: clamp(1rem, 4vw, 2.75rem);
    background: rgba(2, 6, 23, 0.72);
    backdrop-filter: blur(10px);
    z-index: 999;
}

.workflow-overlay__surface {
    position: relative;
    width: min(960px, 100%);
    max-height: 90vh;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    overflow: hidden;
    padding: clamp(1.5rem, 2vw, 2.25rem);
}

.workflow-overlay__header {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.workflow-overlay__close {
    position: absolute;
    top: 1rem;
    right: 1rem;
}

.workflow-overlay__diagram {
    flex: 1;
    overflow: hidden;
}

.workflow-overlay__diagram .analysis__diagram-output {
    min-height: 280px;
    max-height: 65vh;
    overflow: auto;
}

.analysis__fallback {
    margin: 0;
    padding: 0.75rem;
    background: rgba(15, 23, 42, 0.7);
    border-radius: 0.75rem;
    border: 1px dashed rgba(148, 163, 184, 0.35);
    color: var(--text-muted);
    white-space: pre-wrap;
    word-break: break-word;
}

.is-hidden {
    display: none !important;
}

.toast {
    display: none;
    align-items: center;
    gap: 0.65rem;
    padding: 0.65rem 1rem;
    border-radius: 0.9rem;
    background: rgba(251, 191, 36, 0.15);
    border: 1px solid rgba(251, 191, 36, 0.35);
    color: var(--warning);
    font-size: 0.95rem;
}

.toast:not(:empty) {
    display: inline-flex;
}

.toast--error {
    background: rgba(248, 113, 113, 0.15);
    border-color: rgba(248, 113, 113, 0.4);
    color: var(--danger);
}

.content-grid {
    display: grid;
    gap: 1.5rem;
    grid-template-columns: minmax(280px, 360px) minmax(0, 1fr);
    align-items: stretch;
    width: 100%;
}

@media (max-width: 1080px) {
    .content-grid {
        grid-template-columns: 1fr;
    }
}

.panel {
    position: relative;
}

.panel--signin {
    background: linear-gradient(160deg, rgba(30, 64, 175, 0.4), rgba(15, 23, 42, 0.78));
    border: 1px solid rgba(59, 130, 246, 0.28);
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.panel__header h2 {
    font-size: 1.45rem;
}

.panel__header p {
    margin-top: 0.5rem;
}

.panel--signin small {
    color: rgba(226, 232, 240, 0.75);
}

#app {
    display: none;
    gap: 1.5rem;
}

#app.active {
    display: grid;
    grid-template-columns: minmax(220px, 0.7fr) minmax(640px, 3.2fr) minmax(260px, 0.9fr);
    grid-template-areas: 'history chat workspace';
    grid-column: 1 / -1;
    gap: 1.5rem;
    align-items: stretch;
    min-height: clamp(560px, 78vh, 860px);
    width: 100%;
}

#app.active.history-collapsed {
    grid-template-columns: auto minmax(520px, 3.2fr) minmax(260px, 0.9fr);
}

#app.active.history-collapsed .history {
    width: 72px;
    padding: 1rem 0.65rem;
    align-items: center;
    background: rgba(15, 23, 42, 0.72);
    border: 1px solid rgba(148, 163, 184, 0.35);
}

#app.active.history-collapsed .history__title,
#app.active.history-collapsed #new-chat,
#app.active.history-collapsed #chat-list,
#app.active.history-collapsed .history__hint {
    display: none;
}

#app.active.history-collapsed .history__toggle {
    transform: rotate(180deg);
}

#app.active.history-collapsed .history__actions {
    width: 100%;
    justify-content: center;
}

@media (max-width: 1440px) {
    #app.active {
        grid-template-columns: minmax(200px, 0.6fr) minmax(520px, 3fr) minmax(240px, 0.9fr);
    }
}

@media (max-width: 1280px) {
    #app.active {
        grid-template-columns: minmax(0, 1fr);
        grid-template-areas:
            'history'
            'chat'
            'workspace';
    }

    #app.active.history-collapsed {
        grid-template-areas:
            'chat'
            'workspace'
            'history';
    }

    #app.active .history {
        width: 100%;
    }

    #app.active.history-collapsed .history {
        width: auto;
        padding: 0.5rem;
        position: fixed;
        bottom: clamp(1rem, 3vh, 2rem);
        left: clamp(1rem, 3vw, 2rem);
        z-index: 40;
        border-radius: 999px;
        display: flex;
    }

    #app.active .workspace {
        position: static;
        max-height: none;
        box-shadow: none;
    }
}

@media (max-width: 900px) {
    #app.active {
        gap: 1rem;
    }

    #app.active.history-collapsed .history {
        bottom: clamp(0.75rem, 3vh, 1.5rem);
        left: clamp(0.75rem, 4vw, 1.5rem);
    }
}

.history {
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
    height: 100%;
    grid-area: history;
    transition: width 0.2s ease, padding 0.2s ease, background 0.2s ease, border 0.2s ease;
    overflow: hidden;
}

.history__title h2 {
    font-size: 1.2rem;
}

.history__title p {
    margin-top: 0.35rem;
    font-size: 0.95rem;
}

.history__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
}

.history__actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.history__toggle {
    transition: transform 0.2s ease;
}

#new-chat {
    width: 100%;
}

#chat-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    overflow-y: auto;
    flex: 1;
}

#chat-list::-webkit-scrollbar {
    width: 8px;
}

#chat-list::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.25);
    border-radius: 999px;
}

#chat-list {
    scrollbar-width: thin;
    scrollbar-color: rgba(148, 163, 184, 0.3) transparent;
}

#chat-list .chat-select {
    width: 100%;
    text-align: left;
    padding: 0.75rem 1rem;
    border-radius: 0.95rem;
    border: 1px solid transparent;
    background: rgba(148, 163, 184, 0.05);
    color: inherit;
    font-weight: 500;
    line-height: 1.4;
    transition: background 0.2s ease, border 0.2s ease, transform 0.2s ease;
}

#chat-list .chat-select:hover {
    background: rgba(96, 165, 250, 0.12);
    transform: translateX(2px);
}

#chat-list .chat-select.active {
    border-color: var(--border-strong);
    background: rgba(37, 99, 235, 0.2);
}

.chat-item {
    display: flex;
    align-items: center;
    gap: 0.45rem;
}

.chat-item .chat-select {
    flex: 1;
}

.chat-item .chat-menu {
    position: relative;
}

.chat-menu-toggle {
    padding: 0.45rem 0.55rem;
    border-radius: 0.75rem;
    border: 1px solid rgba(148, 163, 184, 0.25);
    background: rgba(15, 23, 42, 0.7);
    color: var(--text-muted);
    font-size: 1.05rem;
    line-height: 1;
}

.chat-menu-toggle:hover {
    color: #f8fafc;
    border-color: rgba(148, 163, 184, 0.45);
}

.chat-menu-dropdown {
    position: absolute;
    right: 0;
    top: calc(100% + 0.4rem);
    background: rgba(9, 17, 34, 0.96);
    border: 1px solid rgba(148, 163, 184, 0.3);
    border-radius: 0.75rem;
    padding: 0.35rem;
    display: none;
    flex-direction: column;
    min-width: 160px;
    box-shadow: 0 18px 40px rgba(2, 6, 23, 0.55);
    z-index: 20;
}

.chat-menu-dropdown.open {
    display: flex;
}

.chat-menu-item {
    padding: 0.6rem 0.8rem;
    background: transparent;
    border: none;
    text-align: left;
    color: var(--text-primary);
    font-size: 0.9rem;
    border-radius: 0.6rem;
}

.chat-menu-item:hover {
    background: rgba(59, 130, 246, 0.12);
}

.chat-menu-item.destructive {
    color: var(--danger);
}

.chat-menu-item.destructive:hover {
    background: rgba(248, 113, 113, 0.18);
}

.chat-window {
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
    height: 100%;
    background: var(--panel-strong);
    border: 1px solid rgba(148, 163, 184, 0.18);
    width: 100%;
    grid-area: chat;
    min-height: 0;
}

.chat-window__header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 1rem;
}

.chat-body {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
    flex: 1;
    min-height: 0;
}

.conversation {
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
    min-width: 0;
}

.workspace {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(148, 163, 184, 0.2);
    border-radius: 1.1rem;
    padding: 1.25rem;
    min-width: 0;
    position: sticky;
    top: clamp(1.5rem, 4vh, 2.75rem);
    max-height: calc(100vh - 4rem);
    overflow-y: auto;
    box-shadow: 0 30px 60px rgba(2, 6, 23, 0.45);
    grid-area: workspace;
}

.workspace .analysis {
    background: transparent;
    border: none;
    padding: 0;
    margin-bottom: 0;
}

.workspace .analysis__diagram {
    max-height: none;
}

@media (max-width: 900px) {
    .workspace {
        margin-top: 0.5rem;
        box-shadow: none;
        border: 1px solid rgba(148, 163, 184, 0.18);
    }
}

.chat-window__header h2 {
    font-size: 1.3rem;
}

.chat-window__header p {
    margin-top: 0.35rem;
    font-size: 0.95rem;
}

.messages {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.9rem;
    padding-right: 0.5rem;
    width: 100%;
    min-height: 0;
}

.messages::-webkit-scrollbar {
    width: 8px;
}

.messages::-webkit-scrollbar-thumb {
    background: rgba(148, 163, 184, 0.25);
    border-radius: 999px;
}

.messages {
    scrollbar-width: thin;
    scrollbar-color: rgba(148, 163, 184, 0.3) transparent;
}

.message {
    padding: 0.85rem 1.1rem;
    border-radius: 1rem;
    background: rgba(15, 23, 42, 0.85);
    border: 1px solid rgba(148, 163, 184, 0.12);
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    width: 100%;
    box-shadow: 0 12px 25px rgba(15, 23, 42, 0.35);
}

.message .role {
    text-transform: uppercase;
    font-size: 0.7rem;
    letter-spacing: 0.08em;
    color: var(--text-muted);
}

.message .body {
    line-height: 1.5;
    white-space: pre-wrap;
}

.message.assistant {
    background: rgba(37, 99, 235, 0.18);
    border-color: rgba(37, 99, 235, 0.35);
    align-self: flex-start;
}

.message.assistant .role {
    color: rgba(125, 211, 252, 0.9);
    text-align: left;
}

.message.user {
    background: rgba(129, 140, 248, 0.18);
    border-color: rgba(129, 140, 248, 0.4);
    align-self: flex-end;
    text-align: right;
}

.message.user .role {
    color: rgba(165, 180, 252, 0.9);
    text-align: right;
}

.message.system {
    background: rgba(100, 116, 139, 0.18);
    border-color: rgba(148, 163, 184, 0.4);
    align-self: center;
    text-align: center;
}

.message.pending {
    opacity: 0.7;
    font-style: italic;
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
}

.controls form {
    display: flex;
    gap: 0.85rem;
}

.controls textarea {
    flex: 1;
    resize: vertical;
    min-height: 80px;
    max-height: 220px;
    padding: 0.9rem 1.1rem;
    border-radius: 1rem;
    border: 1px solid rgba(148, 163, 184, 0.32);
    background: rgba(15, 23, 42, 0.72);
    color: inherit;
    transition: border 0.2s ease, box-shadow 0.2s ease;
}

.controls textarea:focus {
    outline: none;
    border-color: rgba(96, 165, 250, 0.55);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.25);
}

.controls button {
    align-self: flex-end;
    min-width: 110px;
}

.upload {
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    align-items: flex-start;
}

.record-button {
    background: rgba(37, 99, 235, 0.16);
    border: 1px dashed rgba(96, 165, 250, 0.45);
    color: #e2e8f0;
    padding: 0.75rem 1.6rem;
    min-width: 200px;
    box-shadow: none;
    transition: background 0.2s ease, border 0.2s ease, box-shadow 0.2s ease, color 0.2s ease;
}

.record-button:hover:not(:disabled) {
    background: rgba(37, 99, 235, 0.26);
    border-color: rgba(96, 165, 250, 0.68);
}

.record-button:disabled {
    cursor: not-allowed;
    opacity: 0.7;
    box-shadow: none;
}

.record-button--preparing {
    background: rgba(59, 130, 246, 0.22);
    border-style: solid;
    border-color: rgba(59, 130, 246, 0.55);
}

.record-button--recording {
    background: rgba(239, 68, 68, 0.24);
    border-style: solid;
    border-color: rgba(248, 113, 113, 0.72);
    color: #fecaca;
    box-shadow: 0 0 0 4px rgba(248, 113, 113, 0.2);
}

.record-button--error {
    background: rgba(248, 113, 113, 0.2);
    border-style: solid;
    border-color: rgba(248, 113, 113, 0.65);
    color: #fecaca;
}

@media (max-width: 720px) {
    body {
        padding: 2.5rem 1rem;
    }

    .topbar {
        flex-direction: column;
        align-items: flex-start;
    }

    .session {
        width: 100%;
        justify-content: space-between;
    }

    .controls form {
        flex-direction: column;
    }

    .controls button {
        width: 100%;
        align-self: stretch;
    }
}

small.muted {
    color: var(--text-muted);
}

@media (max-width: 768px) {
    #app.active,
    #app.active.history-collapsed {
        display: flex;
        flex-direction: column;
        gap: 0.9rem;
        min-height: 0;
    }

    #app.active > *,
    #app.active.history-collapsed > * {
        width: 100%;
    }

    .chat-window {
        order: 1;
        height: auto;
    }

    .workspace {
        order: 2;
        top: auto;
        max-height: none;
        margin-top: 0;
    }

    .history {
        order: 3;
    }

    #app.active.history-collapsed .history {
        position: static;
        bottom: auto;
        left: auto;
        z-index: auto;
        align-self: flex-end;
        border-radius: 999px;
        padding: 0.65rem;
        box-shadow: none;
        background: rgba(15, 23, 42, 0.72);
        margin-top: 0.75rem;
    }

    #app.active.history-collapsed .history__actions {
        width: auto;
        justify-content: center;
    }

    .chat-window,
    .workspace {
        padding: 1.4rem 1.1rem;
    }
}
