:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --fg: #111827;
  --fg-dim: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --border: #e5e7eb;
  --bubble-in: #e5e7eb;
  --bubble-out: #2563eb;
  --danger: #dc2626;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; cursor: pointer; }

/* App header — fixed top bar */
.app-header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: .75rem;
  padding: .85rem 1rem; background: var(--panel); border-bottom: 1px solid var(--border);
  padding-top: calc(.85rem + env(safe-area-inset-top));
}
.app-header .brand { font-weight: 700; font-size: 1.05rem; flex: 1; }
.app-header .who { font-size: .8rem; color: var(--fg-dim); }
.app-header .back { font-size: .95rem; padding: .25rem .5rem; margin-left: -.25rem; }

/* Inbox */
.inbox { max-width: 720px; margin: 0 auto; padding: 0 0 2rem; }
.toolbar { padding: .75rem 1rem; display: flex; justify-content: flex-end; }
.new-btn { background: var(--accent); color: #fff; padding: .55rem .9rem; border-radius: 999px; font-weight: 600; }
.new-btn:hover { background: var(--accent-hover); color: #fff; }

ul.threads { list-style: none; margin: 0; padding: 0; background: var(--panel); border-top: 1px solid var(--border); }
.thread-row { border-bottom: 1px solid var(--border); }
.thread-row a { display: block; color: inherit; padding: .8rem 1rem; }
.thread-row a:active { background: #f1f5f9; }
.row-top { display: flex; justify-content: space-between; align-items: center; }
.row-name { font-weight: 600; font-size: 1rem; }
.row-time { font-size: .78rem; color: var(--fg-dim); }
.row-preview { color: var(--fg-dim); font-size: .92rem; margin-top: .2rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-line { font-size: .72rem; color: var(--fg-dim); margin-top: .25rem; }
.unread-dot { display: inline-block; width: 8px; height: 8px; background: var(--accent); border-radius: 50%; margin-right: .4rem; vertical-align: middle; }
.empty { text-align: center; color: var(--fg-dim); padding: 3rem 1rem; }

/* Thread view */
.thread { max-width: 720px; margin: 0 auto; padding: 1rem 1rem 6rem; }
ul.bubbles { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
.bubble { max-width: 80%; padding: .55rem .8rem; border-radius: 16px; word-wrap: break-word; }
.bubble.in  { align-self: flex-start; background: var(--bubble-in); color: var(--fg); border-bottom-left-radius: 4px; }
.bubble.out { align-self: flex-end;   background: var(--bubble-out); color: #fff; border-bottom-right-radius: 4px; }
.bubble .text { white-space: pre-wrap; font-size: 1rem; line-height: 1.35; }
.bubble .meta { font-size: .7rem; opacity: .7; margin-top: .2rem; }

/* Composer — fixed bottom */
.composer {
  position: fixed; left: 0; right: 0; bottom: 0;
  display: flex; gap: .5rem; padding: .6rem;
  background: var(--panel); border-top: 1px solid var(--border);
  padding-bottom: calc(.6rem + env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--border); border-radius: 18px;
  padding: .55rem .85rem; font: inherit; outline: none; background: #fff;
  max-height: 160px;
}
.composer textarea:focus { border-color: var(--accent); }
.composer button {
  background: var(--accent); color: #fff; border: none; border-radius: 18px;
  padding: 0 1.1rem; font-weight: 600;
}

/* Login */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1.5rem; }
.auth-card { background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 1.5rem; width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: .9rem; }
.auth-card h1 { margin: 0; font-size: 1.3rem; }
.auth-card label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--fg-dim); }
.auth-card input { padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.auth-card button { background: var(--accent); color: #fff; border: none; padding: .65rem; border-radius: 8px; font-weight: 600; }
.auth-error { color: var(--danger); font-size: .9rem; }

/* New-thread modal */
dialog.modal { border: none; border-radius: 12px; padding: 1.25rem; width: 90%; max-width: 420px; }
dialog.modal::backdrop { background: rgba(0,0,0,0.5); }
.new-form { display: flex; flex-direction: column; gap: .8rem; }
.new-form h2 { margin: 0; font-size: 1.1rem; }
.new-form label { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; color: var(--fg-dim); }
.new-form input, .new-form select, .new-form textarea { padding: .55rem .7rem; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; }
.modal-actions button { padding: .55rem 1rem; border-radius: 8px; border: 1px solid var(--border); background: #fff; }
.modal-actions button[type="submit"] { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }

/* Hub tile grid */
.hub { max-width: 720px; margin: 0 auto; padding: 1rem; }
.hub-title { font-size: 1.1rem; font-weight: 700; color: var(--fg-dim); text-transform: uppercase; letter-spacing: .08em; margin: .5rem .25rem 1rem; }
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; }
.tile { display: flex; gap: .75rem; align-items: center; padding: 1rem; background: var(--panel); border: 1px solid var(--border); border-radius: 14px; color: var(--fg); transition: transform .12s ease, border-color .12s ease, box-shadow .12s ease; }
.tile:hover { border-color: var(--accent); box-shadow: 0 4px 12px rgba(37,99,235,.08); transform: translateY(-1px); }
.tile-icon { font-size: 2rem; line-height: 1; width: 48px; height: 48px; display: flex; align-items: center; justify-content: center; background: #eef2ff; border-radius: 12px; flex-shrink: 0; }
.tile-body { min-width: 0; }
.tile-name { font-weight: 700; font-size: 1rem; color: var(--fg); }
.tile-name .ext { font-size: .75rem; color: var(--fg-dim); margin-left: .3rem; }
.tile-sub { font-size: .82rem; color: var(--fg-dim); margin-top: .2rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
@media (max-width: 480px) { .tiles { grid-template-columns: 1fr; } }

/* Tool pages (caller verify, future) */
.tool { max-width: 720px; margin: 0 auto; padding: 1rem 1rem 3rem; display: flex; flex-direction: column; gap: 1rem; }
.tool .card { background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 1rem 1.1rem; }
.tool .card h2 { font-size: 1rem; font-weight: 700; margin: 0 0 .25rem; }
.tool .card .hint { color: var(--fg-dim); font-size: .88rem; margin: 0 0 .8rem; }
.tool-form { display: flex; flex-direction: column; gap: .7rem; }
.tool-form label { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; color: var(--fg-dim); font-weight: 600; }
.tool-form input, .tool-form select { font: inherit; padding: .6rem .7rem; border: 1px solid var(--border); border-radius: 10px; background: #fff; color: var(--fg); }
.tool-form input:focus, .tool-form select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: var(--accent); }
.btn { display: inline-flex; align-items: center; justify-content: center; padding: .7rem 1rem; border: 0; border-radius: 10px; font-weight: 600; cursor: pointer; transition: background .12s ease; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.alert { padding: .8rem 1rem; border-radius: 10px; font-size: .92rem; }
.alert-success { background: #dcfce7; color: #14532d; border: 1px solid #86efac; }
.alert-danger  { background: #fee2e2; color: #7f1d1d; border: 1px solid #fca5a5; }

/* Audit log table */
.audit { width: 100%; border-collapse: collapse; font-size: .86rem; }
.audit th { text-align: left; padding: .45rem .4rem; color: var(--fg-dim); font-size: .72rem; text-transform: uppercase; letter-spacing: .08em; border-bottom: 1px solid var(--border); }
.audit td { padding: .5rem .4rem; border-bottom: 1px solid var(--border); }
.audit-row.audit-accepted td:first-child { border-left: 3px solid #16a34a; }
.audit-row.audit-rejected td:first-child { border-left: 3px solid var(--danger); }
.audit-row.audit-error td:first-child { border-left: 3px solid #f59e0b; }
.chip { display: inline-block; padding: .15rem .5rem; border-radius: 999px; font-size: .72rem; font-weight: 700; }
.chip-accepted { background: #dcfce7; color: #14532d; }
.chip-rejected { background: #fee2e2; color: #7f1d1d; }
.chip-sent     { background: #fef9c3; color: #713f12; }
.chip-error    { background: #ffedd5; color: #7c2d12; }
