:root {
  --bg: #f5f7f9;
  --panel: #ffffff;
  --text: #111827;
  --muted: #64748b;
  --border: #d7dee8;
  --accent: #0f766e;
  --danger: #b42318;
  --warning: #92400e;
}

* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; color: var(--text); background: var(--bg); }
a { color: var(--accent); text-decoration: none; }
.shell { display: flex; min-height: 100vh; }
.sidebar { width: 220px; background: #101820; color: white; padding: 20px 14px; }
.brand { font-weight: 700; margin: 0 0 24px; }
.sidebar nav { display: grid; gap: 6px; }
.sidebar a { color: #cbd5e1; padding: 9px 10px; border-radius: 6px; }
.sidebar a.active, .sidebar a:hover { color: white; background: rgba(255,255,255,.12); }
.main { flex: 1; padding: 24px; max-width: 1500px; }
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.env-switcher { display: inline-flex; align-items: center; gap: 3px; padding: 3px; border: 1px solid var(--border); border-radius: 999px; background: #edf2f7; box-shadow: inset 0 1px 2px rgba(15, 23, 42, .06); }
.env-switcher button { min-height: 30px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); padding: 5px 12px; font-size: 13px; font-weight: 700; }
.env-switcher .selected { background: white; color: var(--text); box-shadow: 0 1px 3px rgba(15, 23, 42, .14); }
.page-head { margin-bottom: 18px; }
.page-head h1 { margin: 0 0 4px; font-size: 28px; }
.page-head p { margin: 0; color: var(--muted); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.actions, .sync-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.search-form { display: flex; gap: 10px; align-items: center; min-width: min(520px, 100%); }
.search-form input { min-width: 260px; }
.filter-bar { display: flex; gap: 8px; flex-wrap: wrap; margin: -4px 0 18px; }
.filter-chip { display: inline-flex; align-items: center; min-height: 32px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 999px; color: var(--text); background: white; }
.filter-chip.active { border-color: var(--accent); color: var(--accent); background: #eef6f5; font-weight: 700; }
button, .button { display: inline-flex; align-items: center; justify-content: center; min-height: 36px; padding: 8px 13px; border: 1px solid var(--accent); border-radius: 6px; background: var(--accent); color: white; font-weight: 650; cursor: pointer; }
.secondary { color: var(--text); border-color: var(--border); background: white; }
.link-button { background: transparent; border: 0; color: var(--muted); min-height: auto; padding: 0; }
.danger-button { background: var(--danger); border-color: var(--danger); color: white; }
.notice { padding: 10px 12px; border-radius: 6px; margin: 0 0 16px; }
.notice.error { background: #fef3f2; color: var(--danger); border: 1px solid #fecdca; }
.notice.success { background: #ecfdf3; color: #067647; border: 1px solid #abefc6; }
.login-panel { max-width: 420px; margin: 12vh auto; background: white; border: 1px solid var(--border); border-radius: 8px; padding: 28px; }
.stack, .merchant-form { display: grid; gap: 14px; }
label { display: grid; gap: 6px; font-weight: 650; }
input, textarea, select { width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: 9px 10px; font: inherit; background: white; }
textarea { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.grid { display: grid; gap: 14px; }
.grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.checkbox, .inline { display: flex; align-items: center; gap: 8px; }
.checkbox input, .inline input { width: auto; }
.import-bar, .panel, .table-wrap, .danger-zone { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px; margin-bottom: 18px; }
.import-bar form { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
tr.clickable-row { cursor: pointer; }
tr.clickable-row:hover td { background: #f8fbfc; }
tr.clickable-row:focus { outline: 2px solid rgba(15, 118, 110, .35); outline-offset: -2px; }
.merchant-cell { display: flex; align-items: flex-start; gap: 10px; }
.merchant-cell img, .thumb-empty { width: 42px; height: 42px; border-radius: 7px; border: 1px solid var(--border); object-fit: contain; background: #f8fafc; flex: 0 0 auto; }
.muted { color: var(--muted); }
.danger { color: var(--danger); }
.pill { display: inline-flex; padding: 2px 7px; border-radius: 999px; font-size: 12px; margin-right: 4px; border: 1px solid var(--border); }
.pill.ok { background: #ecfdf3; color: #067647; border-color: #abefc6; }
.pill.off { background: #f8fafc; color: var(--muted); }
.chips { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 4px; }
.chips span { padding: 2px 6px; border-radius: 999px; background: #eef6f5; color: #115e59; font-size: 12px; }
.table-summary { margin-bottom: 10px; color: var(--muted); }
.email-subject { font-weight: 700; color: var(--text); }
.unread-row .email-subject { font-weight: 800; }
.inbox-head { align-items: flex-start; }
.email-list-wrap { padding: 0; overflow-x: auto; }
.email-list-wrap .table-summary { padding: 18px 20px 0; margin-bottom: 0; }
.email-table { table-layout: fixed; min-width: 1120px; }
.email-table th, .email-table td { padding: 16px; }
.email-table th:first-child, .email-table td:first-child { padding-left: 20px; }
.email-table th:last-child, .email-table td:last-child { padding-right: 20px; }
.email-table th:nth-child(1) { width: 45%; }
.email-table th:nth-child(2) { width: 15%; }
.email-table th:nth-child(3) { width: 24%; }
.email-table th:nth-child(4) { width: 116px; }
.email-table th:nth-child(5) { width: 116px; }
.email-table th:nth-child(6) { width: 64px; }
.email-table tbody tr:hover td { background: #f8fbfc; }
.email-message-cell { min-width: 0; }
.email-preview { max-width: 100%; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.email-address { color: var(--text); overflow-wrap: anywhere; }
.email-date-cell { min-width: 116px; }
.email-date { display: grid; gap: 2px; }
.email-date-primary { font-weight: 700; white-space: nowrap; }
.email-time { color: var(--muted); white-space: nowrap; }
.email-action { text-align: right; white-space: nowrap; }
.email-meta { display: grid; gap: 10px; }
.email-meta div { display: grid; grid-template-columns: 120px minmax(0, 1fr); gap: 12px; }
.email-meta span { color: var(--muted); }
.email-meta strong { min-width: 0; overflow-wrap: anywhere; }
.email-body { background: #f8fafc; color: var(--text); border: 1px solid var(--border); max-height: 560px; }
.attachment-badge { display: inline-flex; margin-left: 8px; padding: 2px 7px; border-radius: 999px; font-size: 12px; color: var(--warning); background: #fffbeb; border: 1px solid #fedf89; }
.attachment-list { display: grid; gap: 10px; }
.attachment-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; border: 1px solid var(--border); border-radius: 7px; padding: 10px; background: #fbfdff; }
.section-title-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.section-title-row h2 { margin: 0; }
.copy-button.secondary, .copy-inline { color: var(--text); border-color: var(--border); background: white; }
.copy-inline { min-height: 24px; padding: 2px 7px; margin-left: 6px; font-size: 12px; }
.empty-state { display: grid; gap: 4px; padding: 22px; color: var(--muted); }
.empty-state strong { color: var(--text); }
.empty-state p { margin: 0; }
.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; margin: 12px 0; }
.metric-grid div { border: 1px solid var(--border); border-radius: 7px; padding: 10px; background: #fbfdff; }
.metric-grid span { display: block; color: var(--muted); font-size: 12px; }
.metric-grid strong { font-size: 18px; }
.danger-zone { border-color: #fecdca; display: grid; gap: 16px; }
.confirm-box { margin-top: 16px; padding-top: 16px; border-top: 1px solid var(--border); display: grid; gap: 10px; max-width: 520px; }
pre { white-space: pre-wrap; overflow: auto; background: #0f172a; color: white; padding: 12px; border-radius: 7px; }
.ask-panel h2, .analytics-card h2 { margin: 0 0 12px; font-size: 18px; }
.ask-form { display: grid; gap: 12px; }
.analytics-preview { display: grid; gap: 12px; margin-top: 16px; }
.ask-workspace { display: grid; grid-template-columns: minmax(280px, 0.85fr) minmax(420px, 1.15fr); gap: 14px; align-items: start; }
.sql-panel, .result-card, .result-placeholder { min-width: 0; }
.sql-editor { min-height: 270px; line-height: 1.55; resize: vertical; }
.form-footer { display: flex; align-items: center; gap: 12px; min-height: 38px; }
.loading-status { color: var(--muted); min-height: 20px; }
button:disabled { opacity: .72; cursor: wait; }
.promote-grid { margin-top: 14px; }
.ask-result-table { margin-top: 14px; }
.result-placeholder { min-height: 180px; }
.analytics-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; align-items: start; }
.analytics-card { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px; min-width: 0; }
.analytics-card-wide { grid-column: 1 / -1; }
.chart-frame { position: relative; height: 320px; }
.analytics-table-wrap { max-height: 420px; overflow: auto; margin-bottom: 0; padding: 0; }
.analytics-table-wrap table { min-width: 720px; }
.user-overview { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 14px; }
.user-overview div { min-width: 0; }
.user-overview span { display: block; color: var(--muted); font-size: 12px; }
.user-overview strong { overflow-wrap: anywhere; }
.allowance-grid { display: grid; gap: 14px; }
.allowance-card { display: grid; grid-template-columns: 190px minmax(0, 1fr); gap: 16px; border: 1px solid var(--border); border-radius: 8px; padding: 14px; background: #fff; }
.allowance-card-art { aspect-ratio: 1.58; border-radius: 8px; overflow: hidden; background: linear-gradient(135deg, #0f172a, #0f766e); align-self: start; box-shadow: inset 0 0 0 1px rgba(255,255,255,.18); }
.allowance-card-art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card-art-fallback { height: 100%; display: flex; flex-direction: column; justify-content: space-between; padding: 14px; color: #fff; }
.card-art-fallback span { font-size: 12px; opacity: .82; }
.card-art-fallback strong { font-size: 18px; letter-spacing: .03em; }
.allowance-main { min-width: 0; display: grid; gap: 12px; }
.allowance-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.allowance-head h3 { margin: 0; font-size: 20px; }
.allowance-head p { margin: 3px 0 0; color: var(--muted); overflow-wrap: anywhere; }
.status-pill { border-radius: 999px; padding: 5px 10px; font-size: 12px; font-weight: 800; text-transform: uppercase; background: #edf2f7; color: var(--muted); white-space: nowrap; }
.status-active { background: #dcfce7; color: #166534; }
.status-paused { background: #fef3c7; color: #92400e; }
.status-revoked, .status-expired { background: #fee2e2; color: #991b1b; }
.allowance-metrics, .credential-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px; }
.allowance-metrics div, .credential-grid div { border: 1px solid var(--border); border-radius: 7px; padding: 9px; background: #fbfdff; min-width: 0; }
.allowance-metrics span, .credential-grid span { display: block; color: var(--muted); font-size: 12px; }
.allowance-metrics strong, .credential-grid strong { display: block; overflow-wrap: anywhere; }
.allowance-edit-form { display: grid; grid-template-columns: minmax(120px, 180px) minmax(190px, 240px) auto; gap: 10px; align-items: end; }
.allowance-edit-form label { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }
.allowance-edit-form input { width: 100%; min-width: 0; }
.allowance-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.credential-result { display: grid; gap: 12px; border-color: #99f6e4; background: #f0fdfa; }
.credential-result h2 { margin: 0; }
.credential-result p { margin: 3px 0 0; color: var(--muted); }
.loading-panel { min-height: 150px; }
.section-loading { display: flex; align-items: center; gap: 12px; padding: 28px 8px 12px; color: var(--muted); }
.section-loading strong { display: block; color: var(--text); }
.section-loading p { margin: 2px 0 0; }
.spinner { width: 22px; height: 22px; border-radius: 999px; border: 3px solid #d7dee8; border-top-color: var(--accent); animation: spin .8s linear infinite; flex: 0 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.chat-layout { display: grid; grid-template-columns: minmax(240px, 320px) minmax(0, 1fr); gap: 14px; align-items: start; }
.thread-list { position: sticky; top: 16px; max-height: calc(100vh - 48px); overflow: auto; }
.thread-row { display: grid; gap: 3px; padding: 10px 0; border-bottom: 1px solid var(--border); color: var(--text); }
.thread-row:last-child { border-bottom: 0; }
.thread-row span, .thread-row small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-transcripts { display: grid; gap: 14px; min-width: 0; }
.transcript-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.transcript-head h2 { margin: 0; font-size: 20px; }
.transcript-head p { margin: 3px 0 0; color: var(--muted); }
.task-strip { display: grid; gap: 8px; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); margin: 12px 0; }
.task-strip div { border: 1px solid var(--border); border-radius: 7px; padding: 9px; background: #fbfdff; }
.task-strip span, .task-strip small { display: block; color: var(--muted); }
.messages { display: grid; gap: 12px; }
.message-row { display: grid; gap: 6px; max-width: 820px; }
.message-meta { display: flex; gap: 8px; align-items: baseline; color: var(--muted); font-size: 12px; }
.message-meta strong { color: var(--text); text-transform: uppercase; letter-spacing: .04em; }
.message-bubble { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; background: white; white-space: pre-wrap; overflow-wrap: anywhere; }
.message-row.user .message-bubble { background: #eef6f5; border-color: #b7d8d4; }
.message-row.assistant .message-bubble { background: #f8fafc; }
.message-blocks summary { color: var(--muted); cursor: pointer; }

@media (max-width: 900px) {
  .shell { display: block; }
  .sidebar { width: auto; }
  .row, .topbar, .search-form { align-items: flex-start; flex-direction: column; }
  .search-form input { min-width: 0; }
  .email-meta div { grid-template-columns: 1fr; gap: 2px; }
  .attachment-item, .section-title-row { align-items: flex-start; flex-direction: column; }
  .grid.two, .grid.three, .metric-grid, .analytics-grid, .ask-workspace, .user-overview, .allowance-card, .allowance-metrics, .credential-grid, .allowance-edit-form, .chat-layout { grid-template-columns: 1fr; }
  .thread-list { position: static; max-height: none; }
  .main { padding: 16px; }
}
