:root {
    --tm-radius-sm: 6px;
    --tm-radius-md: 8px;
    --tm-radius-lg: 12px;
    --tm-primary: #2563eb;
    --tm-primary-dark: #58a6ff;
}

html, body {
    margin: 0;
    font-family: Roboto, system-ui, sans-serif;
    transition: background-color 0.25s ease, color 0.25s ease;
    overflow-x: hidden;
    max-width: 100vw;
}

[data-theme='dark'] {
    --tm-bg: #0a0e14;
    --tm-surface-elevated: #1a2332;
    --tm-border: rgba(255, 255, 255, 0.08);
    --tm-border-strong: rgba(255, 255, 255, 0.12);
    --tm-code-bg: rgba(255, 255, 255, 0.06);
    --tm-accent-value: #6ea8ff;
    --tm-accent-qty: #7dd3fc;
    --tm-muted: #8b9cb3;
    --tm-auth-gradient: radial-gradient(ellipse 80% 60% at 50% -10%, rgba(88, 166, 255, 0.12), transparent),
        linear-gradient(180deg, #0a0e14 0%, #0d1219 100%);
    --tm-spinner-track: rgba(88, 166, 255, 0.15);
}

[data-theme='light'] {
    --tm-bg: #f1f5f9;
    --tm-surface-elevated: #ffffff;
    --tm-border: rgba(15, 23, 42, 0.08);
    --tm-border-strong: rgba(15, 23, 42, 0.12);
    --tm-code-bg: rgba(15, 23, 42, 0.05);
    --tm-accent-value: #2563eb;
    --tm-accent-qty: #0d9488;
    --tm-muted: #64748b;
    --tm-auth-gradient: radial-gradient(ellipse 80% 60% at 50% -10%, rgba(37, 99, 235, 0.08), transparent),
        linear-gradient(180deg, #f1f5f9 0%, #e2e8f0 100%);
    --tm-spinner-track: rgba(37, 99, 235, 0.12);
}

html, body {
    background-color: var(--tm-bg);
    color: var(--mud-palette-text-primary, inherit);
}

.loading-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    color: var(--tm-muted);
    background-color: var(--tm-bg);
}

.loading-spinner {
    width: 48px;
    height: 48px;
    border: 4px solid var(--tm-spinner-track);
    border-top-color: var(--tm-primary-dark, var(--tm-primary));
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 1rem;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ── Layout ─────────────────────────────────────────────── */

.tm-main-content {
    min-height: calc(100vh - var(--mud-appbar-height, 56px));
    background: var(--tm-bg);
}

.tm-page-shell {
    max-width: 100%;
    min-width: 0;
}

.tm-appbar {
    border-bottom: 1px solid var(--tm-border);
}

.tm-drawer {
    border-right: 1px solid var(--tm-border);
}

.tm-page-header {
    margin-bottom: 1.5rem;
    max-width: 100%;
    min-width: 0;
}

.tm-page-header__row {
    width: 100%;
    max-width: 100%;
    min-width: 0;
}

.tm-page-header__titles {
    min-width: 0;
    flex: 1 1 240px;
}

.tm-page-header__actions {
    flex: 1 1 auto;
    max-width: 100%;
    justify-content: flex-end;
}

.tm-list-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    width: 100%;
    max-width: 100%;
}

.tm-list-toolbar__search {
    flex: 1 1 220px;
    min-width: 0 !important;
    max-width: 100%;
}

.tm-list-toolbar__actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    margin-left: auto;
}

.tm-table-scroll {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.tm-data-table {
    width: 100%;
}

.tm-data-table .mud-table-root {
    background: transparent;
}

.tm-data-table .mud-table-cell::before {
    content: none !important;
}

.tm-impact-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    max-width: 100%;
}

.tm-impact-row__meta {
    margin-left: auto;
}

@media (max-width: 600px) {
    .tm-page-header__actions {
        width: 100%;
        justify-content: flex-start;
    }

    .tm-page-header__actions .mud-button-root {
        flex: 1 1 calc(50% - 0.5rem);
        min-width: 0;
    }

    .tm-list-toolbar__actions {
        width: 100%;
        margin-left: 0;
    }

    .tm-list-toolbar__actions .mud-button-root {
        flex: 1 1 auto;
        min-width: 0;
    }

    .tm-impact-row__meta {
        margin-left: 0;
        width: 100%;
    }

    /* Portfolio chat mobile readability tweaks */
    #chat-panel {
        --tm-chat-bubble-width: 92%;
    }

    #chat-input-area {
        padding: 6px 8px !important;
    }

    #chat-messages {
        padding: 12px !important;
    }
}

/* ── Portfolio chat panel visuals ─────────────────────────── */

.tm-chat-panel {
    border: 1px solid rgba(148, 163, 184, 0.35);
    border-radius: 4px;
    transition: border-color 0.2s ease;
}

.tm-chat-panel--active {
    border-color: rgba(34, 197, 94, 0.65) !important;
}

.tm-chat-panel--archived {
    border-color: rgba(148, 163, 184, 0.35) !important;
}

.tm-chat-session-archived-label {
    color: var(--tm-muted);
    font-size: 0.85em;
    margin-left: 0.35rem;
}

/* ── Portfolio chat input compactness ───────────────────── */

#chat-input-area .mud-input-control {
    min-height: 34px;
    max-height: 40px;
}

#chat-input-area .mud-input-slot {
    padding-top: 0 !important;
    padding-bottom: 0 !important;
    min-height: 34px;
}

#chat-input-area .mud-button-root {
    min-height: 34px;
}

/* Constrain the MudTextField root element (Class= adds to .mud-input-control). */
#chat-input-area .tm-chat-textfield {
    height: 40px !important;
    max-height: 40px !important;
    overflow: hidden;
}

#chat-input-area .tm-chat-textfield .mud-input-control-input-container,
#chat-input-area .tm-chat-textfield .mud-input-root {
    height: 100% !important;
    max-height: 40px !important;
}

/* Single-line textarea: prevent auto-growth. */
#chat-input-area .tm-chat-textfield textarea {
    height: 34px !important;
    min-height: 34px !important;
    max-height: 34px !important;
    resize: none;
    overflow: hidden;
}

/* Editor drawers render inside @Body, not as direct MudLayout children.
   MudBlazor skips mud-drawer-fixed there and uses absolute positioning,
   which reserves horizontal space and clips page content on the right. */
.tm-editor-drawer.mud-drawer:not(.mud-drawer-fixed) {
    position: fixed !important;
    top: var(--mud-appbar-height, 56px) !important;
    bottom: 0 !important;
    height: auto !important;
    z-index: var(--mud-zindex-drawer, 1300);
}

.tm-editor-drawer.mud-drawer-pos-right:not(.mud-drawer-fixed) {
    right: 0 !important;
    left: auto !important;
}

.tm-editor-drawer.mud-drawer--closed:not(.mud-drawer-fixed) {
    pointer-events: none;
}

.tm-page-shell .mud-drawer-overlay.mud-drawer-overlay-temporary {
    position: fixed !important;
    inset: 0 !important;
    z-index: calc(var(--mud-zindex-drawer, 1300) - 1);
}

@media (max-width: 639px) {
    .tm-editor-drawer.mud-drawer--open {
        width: 100vw !important;
        max-width: 100vw;
    }
}

@media (min-width: 640px) {
    .tm-editor-drawer.mud-drawer--open {
        width: var(--tm-editor-drawer-width, 32rem) !important;
        max-width: min(var(--tm-editor-drawer-width, 32rem), 100vw);
    }
}

.tm-editor-drawer .mud-drawer-content {
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

.tm-editor-drawer__body {
    padding: 1rem;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: calc(100dvh - var(--mud-appbar-height, 56px) - 4.5rem);
    box-sizing: border-box;
    width: 100%;
}

.tm-editor-drawer .mud-drawer-header {
    max-width: 100%;
    box-sizing: border-box;
}

.tm-filter-field {
    flex: 0 1 160px;
    min-width: 0;
    max-width: 100%;
}

@media (max-width: 600px) {
    .tm-filter-field {
        flex: 1 1 100%;
    }
}

.settings-card {
    border-radius: var(--tm-radius-md) !important;
    border-color: var(--tm-border) !important;
    max-width: 100%;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.settings-card--equal-height {
    height: 100%;
}

.settings-card__header {
    flex: 0 0 auto;
}

.settings-card__header--clickable {
    cursor: pointer;
}

.settings-card__content {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    width: 100%;
    min-width: 0;
}

.settings-card__content--grow {
    flex: 1 1 auto;
}

.settings-card__actions {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    justify-content: flex-start;
    gap: 0.5rem;
    width: 100%;
    flex-shrink: 0;
    margin-top: 0.5rem;
    padding-top: 1rem;
    border-top: 1px solid var(--tm-border);
}

.settings-card--equal-height .settings-card__actions {
    margin-top: auto;
}

.settings-card__hint {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 0.35rem 0.5rem;
}

.settings-card__time-fields {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 0.25rem;
}

.settings-card__time-field {
    flex: 1 1 12rem;
    max-width: 16rem;
    min-height: 5.75rem;
    position: relative;
}

.email-template-editor textarea {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.45;
}

.email-template-preview {
    border: 1px solid var(--tm-border);
    border-radius: var(--tm-radius-md);
    overflow: hidden;
    background: #fff;
    min-height: 28rem;
}

.email-template-preview iframe {
    display: block;
    width: 100%;
    min-height: 28rem;
    border: 0;
    background: #fff;
}

.content-template-toolbar {
    display: flex;
    align-items: flex-start;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 0.5rem;
}

.content-placeholder-picker__toolbar {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
}

.content-placeholder-picker__search {
    flex: 1 1 18rem;
    min-width: 0;
}

.content-placeholder-picker__option,
.content-placeholder-picker__menu-item {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    min-width: 14rem;
}

.content-placeholder-picker__key {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.82rem;
}

.content-placeholder-picker__menu {
    max-height: 16rem;
    overflow-y: auto;
    min-width: 18rem;
}

.content-template-text-preview {
    min-height: 28rem;
}

.content-template-text-preview pre {
    white-space: pre-wrap;
    word-break: break-word;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.85rem;
    line-height: 1.45;
}

.collapsible-settings-section {
    margin-top: 0.5rem;
}

.collapsible-settings-section__toggle {
    text-transform: none;
    font-weight: 600;
    letter-spacing: normal;
}

.collapsible-settings-section__content {
    margin-top: 0.75rem;
    padding-top: 0.25rem;
    border-top: 1px solid var(--tm-border);
}

.portfolio-card {
    border-radius: var(--tm-radius-md) !important;
    border: 1px solid var(--tm-border);
    overflow: hidden;
    transition: border-color 0.2s ease;
}

.portfolio-card:hover {
    border-color: var(--tm-border-strong);
}

.portfolio-card__summary {
    background: var(--tm-surface-elevated);
    border-bottom: 1px solid var(--tm-border);
}

.pnl-positive {
    color: var(--mud-palette-success, #3fb950);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.pnl-negative {
    color: var(--mud-palette-error, #f85149);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ── Auth ───────────────────────────────────────────────── */

.auth-layout {
    min-height: 100vh;
    background: var(--tm-auth-gradient);
    position: relative;
}

.auth-layout__toolbar {
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.25rem;
    z-index: 1;
}

.auth-layout__container {
    padding-top: 4rem;
    padding-bottom: 3rem;
}

.auth-layout__logo {
    box-shadow: 0 4px 24px rgba(37, 99, 235, 0.25);
}

.auth-card {
    width: 100%;
    max-width: 440px;
    margin: 0 auto;
    border-radius: var(--tm-radius-lg) !important;
    border: 1px solid var(--tm-border);
    overflow: hidden;
}

.auth-divider {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
}

.auth-divider__text {
    font-size: 0.75rem;
    line-height: 1.25rem;
    white-space: nowrap;
}

.password-rule--met {
    color: var(--mud-palette-success);
}

/* ── Brand typography ───────────────────────────────────── */

.brand-title {
    font-weight: 700;
    letter-spacing: -0.02em;
}

.brand-subtitle {
    color: var(--tm-muted);
}

/* ── Dashboard stat cards ───────────────────────────────── */

.stat-card {
    width: 100%;
    border-radius: var(--tm-radius-md) !important;
    border-color: var(--tm-border) !important;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.stat-card:hover {
    border-color: var(--tm-border-strong) !important;
}

.stat-card__value {
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.02em;
}

.stat-card__subtitle {
    min-height: 1.125rem;
}

.stat-card__icon {
    opacity: 0.9;
}

/* ── Chat markdown ──────────────────────────────────────── */

.chat-markdown {
    word-break: break-word;
    line-height: 1.55;
}

.chat-markdown > :first-child {
    margin-top: 0;
}

.chat-markdown > :last-child {
    margin-bottom: 0;
}

.chat-markdown h1,
.chat-markdown h2,
.chat-markdown h3,
.chat-markdown h4 {
    margin: 0.75rem 0 0.4rem;
    font-weight: 600;
    line-height: 1.3;
}

.chat-markdown h2 { font-size: 1.05rem; }
.chat-markdown h3 { font-size: 0.98rem; }

.tm-layout-loading {
    position: fixed;
    top: var(--mud-appbar-height, 56px);
    left: 0;
    right: 0;
    z-index: 1199;
    pointer-events: none;
    max-height: 0;
    opacity: 0;
    overflow: hidden;
    transition: opacity 0.15s ease, max-height 0.15s ease;
}

.tm-layout-loading--active {
    max-height: 4px;
    opacity: 1;
}

.chat-markdown p {
    margin: 0.35rem 0;
}

.chat-markdown ul,
.chat-markdown ol {
    margin: 0.35rem 0 0.35rem 1.1rem;
    padding-left: 0.9rem;
}

.chat-markdown li {
    margin: 0.2rem 0;
}

.chat-markdown strong {
    font-weight: 600;
}

.chat-markdown code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 0.9em;
    padding: 0.1rem 0.35rem;
    border-radius: 4px;
    background: var(--tm-code-bg);
}

.chat-markdown pre {
    margin: 0.5rem 0;
    padding: 0.75rem;
    border-radius: var(--tm-radius-sm);
    overflow-x: auto;
    background: var(--tm-code-bg);
    border: 1px solid var(--tm-border);
}

.chat-markdown pre code {
    padding: 0;
    background: transparent;
    border: none;
}

.chat-message-image {
    display: block;
    max-width: 160px;
    max-height: 120px;
    border-radius: var(--tm-radius-sm);
    object-fit: cover;
}

.chat-attachment-thumb {
    display: block;
    max-width: 72px;
    max-height: 56px;
    border-radius: 6px;
    object-fit: cover;
}

.chat-attachment-preview {
    position: relative;
    border-radius: var(--tm-radius-sm);
    overflow: hidden;
    line-height: 0;
}

.chat-attachment-remove {
    position: absolute;
    top: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.55) !important;
}

.chat-bubble-user {
    color: var(--mud-palette-primary-text, #fff) !important;
}

/* ── Portfolio holdings sidebar ─────────────────────────── */

.holding-card {
    border: 1px solid var(--tm-border);
    border-radius: var(--tm-radius-sm);
    transition: border-color 0.15s ease;
}

.holding-card:hover {
    border-color: var(--tm-border-strong);
}

.holding-portfolio-label {
    color: var(--tm-muted);
    letter-spacing: 0.03em;
    text-transform: uppercase;
    font-size: 0.68rem;
}

.holding-ticker {
    font-weight: 700;
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    letter-spacing: 0.04em;
}

.holding-value {
    color: var(--tm-accent-value);
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}

.holding-qty {
    color: var(--tm-accent-qty);
    font-variant-numeric: tabular-nums;
}

.holding-avg {
    color: var(--tm-muted);
    font-variant-numeric: tabular-nums;
}

/* ── Empty states ───────────────────────────────────────── */

.tm-empty-state {
    border-radius: var(--tm-radius-md);
    border: 1px dashed var(--tm-border-strong);
}
