:root {
    color-scheme: light;
    --ink: #262324;
    --muted: #73706d;
    --line: #e5e2dd;
    --canvas: #f5f4f0;
    --accent: #4c665b;
    --indigo-600: #4f46e5;
    --indigo-700: #4338ca;
    --indigo-400: #818cf8;
    --slate-200: #e2e8f0;
    --slate-300: #cbd5e1;
    --slate-500: #64748b;
    --slate-600: #475569;
    --slate-700: #334155;
    --slate-800: #1e293b;
    --slate-900: #0f172a;
    --red-600: #dc2626;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", Arial, sans-serif;
    color: var(--ink);
    background: var(--canvas);
}

* { box-sizing: border-box; }
body { margin: 0; }
button, input, textarea { font: inherit; touch-action: manipulation; }
button { cursor: pointer; transition: background-color .16s, color .16s, border-color .16s, opacity .16s; }
button:disabled, input:disabled, textarea:disabled { cursor: not-allowed; opacity: .55; }
[hidden] { display: none !important; }
button:focus-visible, input:focus-visible, textarea:focus-visible, a:focus-visible, [tabindex]:focus-visible {
    outline: 3px solid var(--indigo-400);
    outline-offset: 2px;
}
h1, h2, h3, h4, p { margin: 0; }

.skip-link {
    position: fixed;
    top: 8px;
    left: 8px;
    z-index: 50;
    background: #fff;
    color: var(--ink);
    padding: 10px 14px;
    border-radius: 6px;
    border: 1px solid var(--line);
    transform: translateY(-180%);
}
.skip-link:focus { transform: translateY(0); }

/* =========================================================================
   Unified G2000 IAM Screens (matches PMS Auth.tsx & LoadingScreen.tsx)
   ========================================================================= */
.iam-screen {
    width: 100%;
    min-height: 100dvh;
    background: var(--slate-200);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 32px 24px;
}

.iam-loading-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: 16px;
}

.iam-loading-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--slate-800);
}

.iam-spinner {
    display: inline-block;
    border-radius: 50%;
    border-style: solid;
    animation: spin .9s linear infinite;
}

.iam-spinner-lg {
    width: 32px;
    height: 32px;
    border-width: 3px;
    border-color: #c7d2fe;
    border-top-color: var(--indigo-600);
}

.iam-spinner-sm {
    width: 16px;
    height: 16px;
    border-width: 2px;
    border-color: rgba(255, 255, 255, .4);
    border-top-color: #ffffff;
}

.iam-card {
    width: 100%;
    max-width: 384px;
    padding: 32px;
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 10px 15px -3px rgba(15, 23, 42, .1), 0 4px 6px -4px rgba(15, 23, 42, .1);
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.iam-card-header {
    text-align: center;
}

.iam-logo {
    height: 64px;
    width: auto;
    object-fit: contain;
    display: block;
    margin: 0 auto 16px;
    box-shadow: 0 4px 6px -1px rgba(15, 23, 42, .12), 0 2px 4px -2px rgba(15, 23, 42, .08);
}

.iam-title {
    font-size: 22px;
    font-weight: 700;
    color: var(--slate-900);
    line-height: 1.3;
}

.iam-subtitle {
    margin-top: 6px;
    font-size: 15px;
    color: var(--slate-600);
}

.iam-form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.iam-label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: var(--slate-700);
    margin-bottom: 6px;
}

.iam-input {
    display: block;
    width: 100%;
    min-height: 42px;
    padding: 8px 12px;
    background: #ffffff;
    border: 1px solid var(--slate-300);
    border-radius: 6px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, .05);
    color: var(--slate-900);
    font-size: 15px;
}

.iam-input::placeholder { color: #94a3b8; }

.iam-input:focus {
    outline: none;
    border-color: var(--indigo-600);
    box-shadow: 0 0 0 2px rgba(79, 70, 229, .2);
}

.iam-forgot-row {
    text-align: right;
    margin-top: 8px;
}

.iam-forgot-link {
    font-size: 14px;
    font-weight: 500;
    color: var(--indigo-600);
    text-decoration: none;
}

.iam-forgot-link:hover {
    color: var(--indigo-700);
    text-decoration: underline;
}

.iam-error {
    font-size: 14px;
    color: var(--red-600);
    line-height: 1.5;
}

.iam-submit-btn {
    width: 100%;
    min-height: 44px;
    padding: 10px 16px;
    border: 1px solid transparent;
    border-radius: 6px;
    background: var(--indigo-600);
    color: #ffffff;
    font-size: 14px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, .08);
}

.iam-submit-btn:hover:not(:disabled) {
    background: var(--indigo-700);
}

.iam-submit-btn:disabled {
    background: var(--indigo-400);
    opacity: 1;
}

.iam-card-footer {
    text-align: center;
    padding-top: 4px;
}

.iam-copyright {
    font-size: 12px;
    color: var(--slate-500);
}

/* =========================================================================
   Authenticated Chat Workspace
   ========================================================================= */
.app-shell {
    height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--canvas);
}

.brand-bar {
    background: #231f20;
    color: #ffffff;
    flex-shrink: 0;
}

.brand-inner {
    max-width: 1120px;
    margin: 0 auto;
    min-height: 70px;
    padding: 0 24px;
    display: flex;
    align-items: center;
    gap: 18px;
}

.brand-logo {
    width: 128px;
    height: 64px;
    object-fit: contain;
    flex-shrink: 0;
}

.brand-divider {
    height: 24px;
    width: 1px;
    background: #5a5556;
    flex-shrink: 0;
}

.brand-label {
    font-size: 11px;
    letter-spacing: 2px;
    color: #d4cfcf;
    white-space: nowrap;
}

.brand-user {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.user-email-pill {
    font-size: 12px;
    color: #e5e2dd;
    background: rgba(255, 255, 255, .1);
    border: 1px solid rgba(255, 255, 255, .16);
    border-radius: 999px;
    padding: 5px 12px;
    max-width: 240px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.logout-btn {
    min-height: 36px;
    padding: 0 14px;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, .28);
    background: transparent;
    color: #ffffff;
    font-size: 12px;
    white-space: nowrap;
}

.logout-btn:hover {
    background: rgba(255, 255, 255, .14);
}

.workspace {
    width: 100%;
    max-width: 960px;
    margin: 0 auto;
    padding: 18px 20px 16px;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.status-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    border-radius: 8px;
    background: #fff7ed;
    border: 1px solid #fed7aa;
    color: #9a3412;
    font-size: 13px;
    line-height: 1.5;
    flex-shrink: 0;
}

.status-banner.is-error {
    background: #fef2f2;
    border-color: #fecaca;
    color: #991b1b;
}

.chat-card {
    flex: 1;
    min-height: 0;
    background: #ffffff;
    border: 1px solid var(--line);
    border-radius: 12px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(38, 35, 36, .04);
}

.chat-header {
    padding: 16px 22px;
    border-bottom: 1px solid var(--line);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
    flex-shrink: 0;
    background: #ffffff;
}

.chat-header h1 {
    font-size: 17px;
    font-weight: 600;
    color: var(--ink);
    line-height: 1.35;
}

.chat-subtitle {
    margin-top: 3px;
    font-size: 12px;
    color: var(--muted);
    line-height: 1.5;
}

.secondary-btn {
    min-height: 38px;
    padding: 0 12px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: #faf9f6;
    color: var(--ink);
    font-size: 12px;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.secondary-btn:hover:not(:disabled) {
    background: #efede8;
    border-color: #cbc6be;
}

/* Messages Stream */
.chat-messages {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 22px 24px;
    scrollbar-width: thin;
    scrollbar-color: #d5d1c9 transparent;
    overscroll-behavior: contain;
    background: #fafaf8;
}

.message {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-bottom: 20px;
}

.message.human {
    align-items: flex-end;
}

.message-label {
    font-size: 11px;
    color: var(--muted);
    margin: 0 4px 6px;
}

.message-bubble {
    max-width: 88%;
    min-width: 0;
    background: #ffffff;
    border: 1px solid var(--line);
    color: var(--ink);
    padding: 14px 18px;
    border-radius: 4px 14px 14px 14px;
    font-size: var(--message-font-size, 14px);
    line-height: 1.72;
    overflow-wrap: anywhere;
    box-shadow: 0 1px 2px rgba(38, 35, 36, .03);
}

.message.human .message-bubble {
    background: #eaf0ec;
    border-color: #d4e0d8;
    color: #21372d;
    border-radius: 14px 4px 14px 14px;
    white-space: pre-wrap;
}

.message.ai .message-bubble > * + * {
    margin-top: 10px;
}

.message.ai .message-bubble h2,
.message.ai .message-bubble h3,
.message.ai .message-bubble h4 {
    font-weight: 600;
    line-height: 1.4;
    color: #18181b;
}

.message.ai .message-bubble h2 { font-size: 1.15em; }
.message.ai .message-bubble h3 { font-size: 1.06em; }
.message.ai .message-bubble h4 { font-size: 1em; }

.message.ai .message-bubble ul,
.message.ai .message-bubble ol {
    margin: 0;
    padding-left: 20px;
}

.message.ai .message-bubble li + li {
    margin-top: 4px;
}

.message.ai .message-bubble hr {
    border: 0;
    border-top: 1px solid var(--line);
    margin: 12px 0;
}

.message.ai .message-bubble blockquote {
    margin: 0;
    padding: 8px 12px;
    border-left: 3px solid var(--accent);
    background: #f5f4f0;
    border-radius: 0 6px 6px 0;
    color: #44403c;
}

.message.ai .message-bubble code {
    font-family: Consolas, Monaco, monospace;
    font-size: .9em;
    background: #f2f1ed;
    padding: 1px 5px;
    border-radius: 4px;
}

.message.ai .message-bubble pre {
    margin: 0;
    padding: 10px 12px;
    background: #f2f1ed;
    border-radius: 6px;
    overflow-x: auto;
}

.message.ai .message-bubble pre code {
    background: transparent;
    padding: 0;
}

.message.ai .message-bubble a {
    color: var(--indigo-600);
    text-decoration: underline;
    word-break: break-all;
}

/* Horizontally scrollable Markdown tables (never clipped on narrow mobile viewports) */
.table-scroll {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #ffffff;
}

.table-scroll table {
    border-collapse: collapse;
    width: max-content;
    min-width: 100%;
    font-size: .92em;
}

.table-scroll th,
.table-scroll td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--line);
    border-right: 1px solid #f0ede7;
    text-align: left;
    vertical-align: top;
    white-space: nowrap;
}

.table-scroll th:last-child,
.table-scroll td:last-child {
    border-right: 0;
}

.table-scroll tr:last-child td {
    border-bottom: 0;
}

.table-scroll th {
    background: #f5f4f0;
    font-weight: 600;
    color: #3f3b38;
}

/* Inline Banner preview images */
.chat-image-trigger {
    display: inline-block;
    padding: 0;
    margin: 6px 4px 2px 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #f8f8f6;
    overflow: hidden;
    vertical-align: top;
}

.chat-image-trigger:hover {
    border-color: var(--accent);
}

.chat-preview-image {
    display: block;
    max-width: 100%;
    max-height: 240px;
    width: auto;
    height: auto;
    object-fit: contain;
}

.human-files-note {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 8px;
}

.human-file-tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
    padding: 3px 8px;
    border-radius: 999px;
    background: rgba(33, 55, 45, .1);
    color: #21372d;
}

.message-time {
    font-size: 10px;
    color: var(--muted);
    margin: 5px 4px 0;
    font-variant-numeric: tabular-nums;
}

.typing-indicator {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
    font-size: 13px;
}

.typing-dots {
    display: inline-flex;
    gap: 4px;
}

.typing-dots span {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--accent);
    animation: pulse 1.2s ease-in-out infinite;
}

.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }

/* Quick prompts */
.quick-prompts {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 20px;
    border-top: 1px solid var(--line);
    background: #ffffff;
    flex-shrink: 0;
}

.quick-prompt-btn {
    min-height: 34px;
    padding: 6px 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #faf9f6;
    color: #44403c;
    font-size: 12px;
}

.quick-prompt-btn:hover:not(:disabled) {
    background: #eaf0ec;
    border-color: #8aa598;
    color: #21372d;
}

/* Attachment staging bar */
.attachment-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 20px;
    border-top: 1px solid var(--line);
    background: #faf9f6;
    flex-shrink: 0;
}

.attachment-chip {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 5px 8px 5px 10px;
    border: 1px solid #d9d6d0;
    border-radius: 8px;
    background: #ffffff;
    font-size: 12px;
    max-width: 260px;
}

.attachment-chip-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--ink);
}

.attachment-chip-size {
    color: var(--muted);
    font-size: 11px;
    white-space: nowrap;
}

/* Composer */
.chat-composer {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 12px 16px;
    border-top: 1px solid var(--line);
    background: #ffffff;
    flex-shrink: 0;
}

.chat-composer.is-dragover {
    background: #eef5f0;
    border-top-color: var(--accent);
}

.attach-btn,
.icon-btn {
    position: relative;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #faf9f6;
    color: var(--ink);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.icon-btn {
    min-width: 32px;
    min-height: 32px;
    border-color: transparent;
    background: transparent;
}

.attach-btn:hover:not(:disabled) {
    background: #efede8;
    border-color: #b7b1a8;
}

.icon-btn:hover:not(:disabled) {
    background: rgba(38, 35, 36, .08);
}

/* Visible CSS hover/focus tooltip for icon-only SVG buttons */
.has-tooltip::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    transform: translateX(-50%);
    padding: 5px 9px;
    border-radius: 5px;
    background: #18181b;
    color: #ffffff;
    font-size: 11px;
    line-height: 1.3;
    white-space: nowrap;
    pointer-events: none;
    opacity: 0;
    transition: opacity .15s;
    z-index: 40;
}

.has-tooltip:hover::after,
.has-tooltip:focus-visible::after {
    opacity: 1;
}

.chat-input {
    flex: 1;
    min-width: 0;
    min-height: 44px;
    max-height: 160px;
    padding: 10px 14px;
    border: 1px solid #d9d6d0;
    border-radius: 8px;
    background: #ffffff;
    color: var(--ink);
    font-size: 14px;
    line-height: 1.5;
    resize: none;
}

.chat-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(76, 102, 91, .18);
}

.send-btn {
    min-height: 44px;
    padding: 0 18px;
    border: 1px solid #18181b;
    border-radius: 8px;
    background: #18181b;
    color: #ffffff;
    font-size: 13px;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) {
    background: #333338;
}

/* Footer */
.chat-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 6px 20px;
    border-top: 1px solid var(--line);
    background: #faf9f6;
    font-size: 11px;
    color: var(--muted);
    flex-shrink: 0;
}

.font-size-control {
    display: flex;
    align-items: center;
    gap: 10px;
    flex: 1;
    max-width: 280px;
    min-width: 0;
}

.font-size-control label { white-space: nowrap; }

.font-size-control input[type="range"] {
    flex: 1;
    min-width: 0;
    min-height: 36px;
    accent-color: var(--accent);
    cursor: pointer;
}

.font-size-control output {
    min-width: 34px;
    font-variant-numeric: tabular-nums;
}

/* Lightbox Modal */
.image-modal {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.image-modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(15, 23, 42, .72);
}

.image-modal-content {
    position: relative;
    z-index: 1;
    max-width: min(92vw, 1100px);
    max-height: 90dvh;
    background: #ffffff;
    border-radius: 12px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 40px rgba(0, 0, 0, .35);
}

.image-modal-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--line);
}

.image-modal-caption {
    font-size: 13px;
    font-weight: 500;
    color: var(--ink);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.image-modal-img {
    display: block;
    max-width: 100%;
    max-height: calc(90dvh - 56px);
    object-fit: contain;
    background: #111;
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse {
    0%, 80%, 100% { opacity: .25; transform: scale(.85); }
    40% { opacity: 1; transform: scale(1); }
}

@media (max-width: 680px) {
    .brand-inner {
        min-height: 60px;
        padding: 0 14px;
        gap: 10px;
    }
    .brand-logo {
        width: 100px;
        height: 52px;
    }
    .brand-divider,
    .brand-label {
        display: none;
    }
    .user-email-pill {
        max-width: 145px;
        font-size: 11px;
        padding: 4px 9px;
    }
    .workspace {
        padding: 10px 10px max(10px, env(safe-area-inset-bottom));
        gap: 8px;
    }
    .chat-header {
        padding: 12px 14px;
    }
    .chat-header h1 {
        font-size: 15px;
    }
    .chat-subtitle {
        font-size: 11px;
    }
    .chat-messages {
        padding: 14px 12px;
    }
    .message-bubble {
        max-width: 94%;
        padding: 12px 14px;
    }
    .quick-prompts {
        padding: 8px 12px;
    }
    .chat-composer {
        padding: 10px 12px;
        gap: 8px;
    }
    .chat-input {
        font-size: 16px;
    }
    .send-btn {
        padding: 0 14px;
    }
    .chat-footer {
        padding: 4px 12px;
    }
    .footer-timezone {
        display: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation: none !important;
        transition: none !important;
    }
}
