:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1c2026;
  --muted: #687080;
  --accent: #1a66d2;
  --accent-soft: #e6effc;
  --danger: #c4322b;
  --warn-bg: #fff6e0;
  --warn-text: #7a5300;
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f24;
    --surface-2: #252930;
    --border: #2f343c;
    --text: #e6e8eb;
    --muted: #949ba7;
    --accent: #6ea2f5;
    --accent-soft: #1f2d45;
    --danger: #f0726b;
    --warn-bg: #3a2f14;
    --warn-text: #f3d48a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.center { text-align: center; padding: 8px; }
.num, .files th.num { text-align: right; }

button, input { font: inherit; color: inherit; }
button {
  cursor: pointer; border: 1px solid var(--border); background: var(--surface);
  padding: 6px 12px; border-radius: 6px;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { border-color: transparent; background: transparent; }
button.ghost:hover { background: var(--surface-2); }
button:disabled { opacity: .6; cursor: default; }
input[type=email], input[type=search], input[type=password] {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface); outline: none;
}
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(460px, 100%); background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 28px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card label { display: block; font-weight: 600; margin: 18px 0 6px; }
button.link { border: none; background: none; padding: 6px 0 0; color: var(--muted); text-decoration: underline; }
button.link:hover { color: var(--danger); background: none; }
button.block { display: block; width: 100%; margin-top: 20px; padding: 10px; font-weight: 600; }
.login-card label.drop {
  display: flex; align-items: center; gap: 12px; margin: 0; font-weight: 400; cursor: pointer;
  padding: 12px; border: 1px dashed var(--border); border-radius: 8px; background: var(--bg);
}
.drop:hover, .drop.over { border-color: var(--accent); background: var(--accent-soft); }
.login-card label.drop.ready { border-style: solid; }
.drop input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.drop:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.drop-icon {
  flex: none; width: 36px; height: 36px; border-radius: 8px; display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent); font: 600 13px ui-monospace, monospace;
}
.drop-text { display: flex; flex-direction: column; min-width: 0; }
.drop-text span { overflow-wrap: anywhere; }
#login-status { margin-top: 14px; }
.note { margin: 14px 0 0; padding: 8px 10px; border-radius: 6px; background: var(--warn-bg); color: var(--warn-text); }
.status-line { display: flex; gap: 8px; margin: 6px 0; font-size: 13px; }
.status-line .dot { flex: none; width: 8px; height: 8px; border-radius: 50%; margin-top: 6px; background: #3a9a5b; }
.status-line.bad .dot { background: var(--danger); }
.setup { margin-top: 18px; font-size: 13px; }
.setup summary { cursor: pointer; color: var(--muted); }
.setup code { word-break: break-all; background: var(--surface-2); padding: 1px 4px; border-radius: 4px; }

/* App shell */
.app { height: 100%; height: 100dvh; display: flex; flex-direction: column; }
.show-sm { display: none; }
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 8px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; }
.tabs { display: flex; gap: 4px; }
.tab { border: none; background: none; padding: 8px 14px; border-radius: 6px; color: var(--muted); }
.tab.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.user { margin-left: auto; display: flex; align-items: center; gap: 10px; min-width: 0; }
.user img { width: 32px; height: 32px; border-radius: 50%; }
.user-text { min-width: 0; }
.user-text div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.banner { background: var(--warn-bg); color: var(--warn-text); padding: 8px 16px; font-size: 13px; }
.pane { flex: 1; min-height: 0; display: flex; }
.sentinel { height: 1px; }

/* Chat */
.spaces {
  width: 300px; flex: none; border-right: 1px solid var(--border); background: var(--surface);
  display: flex; flex-direction: column; overflow-y: auto;
}
.spaces input { margin: 10px; width: calc(100% - 20px); }
.list { list-style: none; margin: 0; padding: 0 6px; }
.list li {
  padding: 8px 10px; border-radius: 6px; cursor: pointer; display: flex; gap: 10px; align-items: center;
}
.list li.active { background: var(--accent-soft); }
.list .meta { min-width: 0; flex: 1; }
.list .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 500; }
.avatar {
  flex: none; width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 600; font-size: 13px; color: #fff;
}
.avatar.square { border-radius: 8px; }

.conversation { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.empty { margin: auto; color: var(--muted); }
#conv { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.conv-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.conv-title { flex: 1; min-width: 0; }
.conv-title > div:first-child { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-actions { display: flex; align-items: center; gap: 4px; }
.back { display: none; }
.messages { flex: 1; overflow-y: auto; padding: 8px 16px 16px; overflow-anchor: none; }
.day { text-align: center; margin: 16px 0 8px; }
.day span { background: var(--surface-2); color: var(--muted); font-size: 12px; padding: 2px 10px; border-radius: 10px; }
.msg { display: flex; gap: 10px; padding: 2px 8px; border-radius: 6px; }
.msg.first { margin-top: 10px; }
.msg .gutter { width: 32px; flex: none; }
.msg .body { min-width: 0; flex: 1; }
.msg .who { font-weight: 600; }
.msg .who .time { font-weight: 400; margin-left: 6px; }
.msg .text { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .text.deleted { font-style: italic; color: var(--muted); }
.msg .reply { color: var(--muted); font-size: 12px; }
.attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--surface); font-size: 13px; text-decoration: none; color: var(--text);
  max-width: 280px;
}
.chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip:hover { border-color: var(--accent); }
.thumb {
  display: block; width: 220px; height: 150px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface-2); object-fit: cover;
}

/* Drive */
.drive-nav {
  width: 220px; flex: none; padding: 12px 8px; border-right: 1px solid var(--border);
  background: var(--surface); display: flex; flex-direction: column; gap: 2px;
}
.nav-item { text-align: left; border: none; background: none; border-radius: 6px; padding: 8px 12px; }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.quota { margin-top: auto; padding: 8px 12px; }
.drive-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.drive-toolbar {
  display: flex; gap: 12px; align-items: center; padding: 10px 16px;
  border-bottom: 1px solid var(--border); background: var(--surface);
}
.drive-toolbar form { width: min(320px, 45%); margin-left: auto; }
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; min-width: 0; }
.crumbs button { border: none; background: none; padding: 4px 6px; font-weight: 500; }
.crumbs .sep { color: var(--muted); }
.table-wrap { flex: 1; overflow: auto; }
table.files { width: 100%; border-collapse: collapse; }
.files th {
  position: sticky; top: 0; background: var(--bg); text-align: left; font-weight: 500;
  color: var(--muted); font-size: 12px; padding: 8px 12px; border-bottom: 1px solid var(--border);
}
.files td { padding: 8px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.files .name { display: flex; align-items: center; gap: 10px; min-width: 0; }
.files .name img, .files .name svg { width: 18px; height: 18px; flex: none; }
.files .name a, .files .name button {
  color: var(--text); text-decoration: none; border: none; background: none; padding: 0;
  text-align: left; overflow-wrap: anywhere;
}
.file-sub { display: none; }
.files .name a:hover, .files .name button:hover { color: var(--accent); background: none; }
.files td.actions { white-space: nowrap; text-align: right; }
.files td.actions button { padding: 4px 8px; font-size: 12px; }

/* Toasts */
.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 10; max-width: calc(100% - 32px); }
.toast {
  background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 8px;
  font-size: 13px; max-width: 420px; box-shadow: 0 4px 16px rgba(0,0,0,.2);
}
.toast.error { background: var(--danger); color: #fff; }

/* Hover highlights only where there's a real pointer; on touch screens they stick after a tap. */
@media (hover: hover) {
  .msg:hover { background: var(--surface); }
  .files tr:hover td { background: var(--surface); }
  .list li:hover { background: var(--surface-2); }
}

@media (max-width: 760px) {
  /* 16px inputs stop iOS from zooming on focus */
  input[type=email], input[type=search], input[type=password] { font-size: 16px; padding: 10px 12px; }
  .hide-sm { display: none !important; }
  .show-sm { display: inline; }
  .login { place-items: start center; padding: 24px 16px; }
  .login-card { padding: 20px; }

  .topbar { gap: 8px; padding: 6px 12px; padding-top: max(6px, env(safe-area-inset-top)); }
  .brand { display: none; }
  .tab { padding: 10px 16px; }
  .user-text { display: none; }
  #switch-user { font-size: 18px; padding: 6px 10px; }
  .banner { padding: 8px 12px; }

  /* Chat: list and conversation each take the full screen */
  .spaces { width: 100%; border-right: none; }
  .chat.open .spaces { display: none; }
  .chat:not(.open) .conversation { display: none; }
  .list li { padding: 10px; }
  .avatar { width: 36px; height: 36px; }
  .back { display: inline-block; font-size: 20px; padding: 4px 10px; }
  .conv-head { padding: 6px 8px; gap: 4px; }
  .conv-actions button { padding: 8px; font-size: 13px; }
  .messages { padding: 4px 8px calc(12px + env(safe-area-inset-bottom)); }
  .msg { padding: 2px 4px; gap: 8px; }
  .thumb { width: 100%; max-width: 260px; height: 160px; }
  .chip { max-width: 100%; padding: 8px 10px; }

  /* Drive: nav becomes a scrolling pill row, table rows become compact cards */
  .pane.drive { flex-direction: column; }
  .drive-nav {
    width: 100%; flex-direction: row; overflow-x: auto; gap: 6px; padding: 8px 12px;
    border-right: none; border-bottom: 1px solid var(--border); scrollbar-width: none;
  }
  .nav-item { white-space: nowrap; border: 1px solid var(--border); border-radius: 999px; padding: 6px 14px; }
  .nav-item.active { border-color: var(--accent); }
  .quota { display: none; }
  .drive-toolbar { flex-direction: column-reverse; align-items: stretch; gap: 6px; padding: 8px 12px; }
  .drive-toolbar form { width: 100%; margin: 0; }
  .crumbs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  .crumbs button { white-space: nowrap; }
  .files thead { display: none; }
  .files td { padding: 10px 12px; }
  .files td.actions { width: 1%; padding-left: 0; }
  .files td.actions button { padding: 8px 10px; }
  .files .name { align-items: flex-start; }
  .files .name img, .files .name svg { margin-top: 2px; }
  .file-sub { display: block; margin-top: 2px; }

  .toasts { left: 12px; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom)); max-width: none; }
  .toast { max-width: none; }
}
