:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e6;
  --text: #1c2129;
  --muted: #5f6b7a;
  --accent: rgb(30, 185, 213);   /* brand blue */
  --accent-text: #1e2230;        /* dark text reads better than white on the bright brand blue */
  --accent-ink: rgb(13, 120, 140); /* brand blue, darkened just enough for small text on white */
  --danger: #c93c37;
  --ok: #1a7f37;
  --ok-bg: #e6f4ea;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --surface-2: #1f242c;
    --border: #2c333d;
    --text: #e6e9ee;
    --muted: #97a1ae;
    --accent: rgb(30, 185, 213);
    --accent-ink: rgb(30, 185, 213);
    --danger: #ff6b66;
    --ok: #4ac26b;
    --ok-bg: #12261a;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; font-weight: 600; }

/* ---------- shared bits ---------- */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}
.topbar .brand { font-weight: 650; font-size: 15px; display: flex; align-items: center; gap: 8px; color: var(--text); }
.topbar .spacer { flex: 1; }
.logo { width: 32px; height: 32px; display: block; }

button, .btn {
  font: inherit; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 12px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
button:hover, .btn:hover { background: var(--surface-2); text-decoration: none; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button.link { border: 0; background: none; padding: 2px 4px; color: var(--muted); }
button.link:hover { color: var(--text); background: none; }
button.link.danger:hover { color: var(--danger); }
button:disabled { opacity: .55; cursor: default; }

input[type=text], input[type=password], input[type=email], select, textarea {
  font: inherit; width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--text);
}
input[type=text]:focus, input[type=password]:focus, input[type=email]:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.muted { color: var(--muted); }
.error { color: var(--danger); }
.badge {
  display: inline-block; font-size: 12px; font-weight: 600;
  padding: 1px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.badge.open { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent-ink); }
.badge.resolved { background: var(--ok-bg); color: var(--ok); }

.badge.warn { background: color-mix(in srgb, #d97706 18%, transparent); color: #b45309; }
@media (prefers-color-scheme: dark) { .badge.warn { color: #f5a524; } }

/* Revision + status pills */
.rev-line { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; }
.badge.rev { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); font-variant-numeric: tabular-nums; }
.badge.status-work { background: color-mix(in srgb, #d97706 16%, transparent); color: #9a5b06; }
.badge.status-approval { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--accent-ink); }
.badge.status-approved { background: var(--ok-bg); color: var(--ok); }
@media (prefers-color-scheme: dark) { .badge.status-work { color: #f5a524; } }

#user-area { display: flex; align-items: center; gap: 10px; }
.who { display: flex; align-items: center; gap: 6px; font-weight: 600; }

/* ---------- login page ---------- */
body.auth-page { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.auth-card { width: 100%; max-width: 380px; padding: 28px; display: grid; gap: 14px; text-align: center; }
.auth-card form { display: grid; gap: 12px; text-align: left; }
.auth-card form p { margin: 0 0 4px; text-align: center; }
.auth-card button { justify-content: center; padding: 9px; }
.auth-logo { width: 72px; height: 72px; margin: 0 auto; }
.auth-card h1 { font-size: 20px; }

/* ---------- admin page ---------- */
.admin { max-width: 1100px; margin: 0 auto; padding: 24px 20px 60px; display: grid; gap: 24px; }
.admin section { padding: 20px; display: grid; gap: 14px; }
.admin .section-head { display: flex; align-items: center; gap: 12px; }
.admin .section-head .spacer { flex: 1; }
.new-account { display: grid; grid-template-columns: 1.3fr 1fr 1.2fr auto; gap: 10px; align-items: end; }
/* Company form: two rows of three (name, email, username / password, logo, button). */
.new-account[data-role=Company] { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.new-account[data-role=Company] button[type=submit] { justify-self: start; }
.pw-field { display: flex; gap: 6px; }
.email-cell { white-space: nowrap; }
.email-cell a { color: var(--text); }
.email-cell .link { font-size: 12px; }
.link.warn-link { color: #b45309; font-weight: 600; }
@media (prefers-color-scheme: dark) { .link.warn-link { color: #f5a524; } }
.logo-pick { display: flex; align-items: center; gap: 8px; white-space: nowrap; }

/* Company logo: shown contained in a rounded tile; falls back to initials. */
.company-logo {
  width: 34px; height: 34px; flex: none; border-radius: 8px;
  border: 1px solid var(--border); background: #fff;
  display: inline-grid; place-items: center; overflow: hidden;
  font-size: 12px; font-weight: 700; color: var(--muted);
}
.company-logo img { width: 100%; height: 100%; object-fit: contain; padding: 2px; }
.company-logo.lg { width: 44px; height: 44px; }
.company-logo:empty { background: var(--surface-2); border-style: dashed; }
.company-cell { display: flex; align-items: center; gap: 10px; }

/* Viewer header: left (Client Portal / Company / Project / Model) | centre (client logo) | right (actions).
   The equal side columns keep the logo truly centred on the page.
   If the buttons need more room on a narrower screen, the right column grows instead of overlapping
   the logo (the logo then sits slightly left of centre). */
.viewer-topbar { display: grid; grid-template-columns: minmax(0, 1fr) auto 1fr; gap: 16px; }
.topbar-left { display: flex; align-items: center; gap: 12px; min-width: 0; overflow: hidden; }
.topbar-left .brand { flex: none; }
.topbar-right { display: flex; align-items: center; justify-content: flex-end; gap: 12px; white-space: nowrap; }
.topbar-right #model-company { max-width: 210px; text-overflow: ellipsis; }
/* Keep the viewer header lean so the logo can stay centred (these are on the home page anyway). */
.viewer-topbar #user-area .btn, .viewer-topbar .who .badge { display: none; }

/* Breadcrumb: when space runs out, company and project shorten with "…" before the model name does. */
.crumbs { display: flex; align-items: center; gap: 10px; min-width: 0; overflow: hidden; }
.crumbs .sep { color: var(--muted); flex: none; }
.crumbs .crumb-company, .crumbs .project { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 0 3 auto; min-width: 3em; }
.crumbs .project { color: var(--muted); }
.crumbs strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 0 0 auto; max-width: 100%; }

/* Centre: the client's logo image only, at its own shape (wide logos stay wide). */
.viewer-topbar .company-banner { grid-column: 2; }
.company-banner { display: flex; align-items: center; min-width: 0; max-width: min(320px, 22vw); border-radius: 8px; }
.banner-logo { height: 36px; width: auto; max-width: 100%; object-fit: contain; display: block; }
/* Logos sit straight on the header. Only when a logo would vanish against it (a white logo in
   light mode, a dark logo in dark mode) does it get a contrasting backing. logo-light / logo-dark
   are set by detectLogoTone() in common.js. */
.company-banner.logo-light { background: #1e2230; padding: 3px 10px; }
@media (prefers-color-scheme: dark) {
  .company-banner.logo-light { background: none; padding: 0; }
  .company-banner.logo-dark { background: #fff; padding: 3px 10px; }
}

/* Same idea for the small logo tiles (admin page): white tile by default, dark tile for light logos. */
.company-logo.logo-light { background: #1e2230; border-color: #1e2230; }
/* Wide logos get a wider tile rather than shrinking into a square. */
.company-logo:has(img) { width: auto; min-width: 34px; max-width: 130px; padding: 4px 6px; }
.company-logo:has(img) img { width: auto; max-width: 100%; height: 100%; padding: 0; }
/* Medium screens: logo gets its own centred strip under the header so nothing overlaps. */
@media (max-width: 1180px) {
  .viewer-topbar { grid-template-columns: minmax(0, 1fr) auto; row-gap: 8px; }
  .viewer-topbar .company-banner { grid-column: 1 / -1; grid-row: 2; justify-self: center; max-width: 90vw; }
}
.model .project { font-size: 12px; font-weight: 600; color: var(--accent-ink); text-transform: uppercase; letter-spacing: .03em; }
.pw-field input { min-width: 0; }
table.accounts { width: 100%; border-collapse: collapse; }
table.accounts th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--border); }
table.accounts td { padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.accounts tr:last-child td { border-bottom: 0; }
table.accounts tr.disabled td:not(:last-child) { opacity: .55; }
table.accounts .actions { text-align: right; white-space: nowrap; }
.notice {
  border: 1px solid var(--accent); border-radius: var(--radius); padding: 12px 14px;
  background: color-mix(in srgb, var(--accent) 8%, var(--surface));
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
}
.notice code { font-size: 15px; padding: 2px 8px; border-radius: 5px; background: var(--surface); border: 1px solid var(--border); user-select: all; }
.notice .spacer { flex: 1; }

/* ---------- library page ---------- */
.library { max-width: 1100px; margin: 0 auto; padding: 24px 20px 60px; display: grid; gap: 24px; }
.upload { padding: 20px; display: grid; gap: 14px; }
.upload .row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.drop {
  border: 2px dashed var(--border); border-radius: var(--radius);
  padding: 22px; text-align: center; color: var(--muted); cursor: pointer;
}
.drop.over, .drop:hover { border-color: var(--accent); color: var(--text); }
.drop strong { color: var(--text); }
.upload-actions { display: flex; align-items: center; gap: 12px; }
progress { width: 200px; }

.models { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.model {
  position: relative;
  padding: 16px; display: grid; gap: 8px; color: var(--text); align-content: start;
  transition: border-color .15s;
}
.model:hover, .model:focus-within { border-color: var(--accent); }
.model h3 { font-size: 15px; display: flex; align-items: center; gap: 8px; }
/* The model name link covers the whole card, so clicking anywhere opens it. */
.model-link { color: inherit; }
.model-link:hover { text-decoration: none; }
.model-link::after { content: ""; position: absolute; inset: 0; border-radius: var(--radius); }
.model-link:focus-visible { outline: none; }
.model-link:focus-visible::after { outline: 2px solid var(--accent); outline-offset: 2px; }
.model-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 20px; }
.model .project.none { color: var(--muted); font-weight: 500; text-transform: none; letter-spacing: 0; font-style: italic; }
/* Edit sits above the stretched link so it can be clicked. */
.edit-model { position: relative; z-index: 1; font-size: 13px; padding: 2px 8px; border-radius: 6px; }
.edit-model:hover { background: var(--surface-2); color: var(--text); }

/* Admin: drop a file on a card to upload a new revision */
.drop-overlay {
  position: absolute; inset: 0; z-index: 2; border-radius: var(--radius);
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 4px; text-align: center; padding: 12px;
  background: color-mix(in srgb, var(--accent) 14%, var(--surface));
  border: 2px dashed var(--accent);
  pointer-events: none;   /* keeps drag events going to the card */
}
.drop-overlay strong { font-size: 15px; color: var(--accent-ink); }
.drop-overlay span { font-size: 12px; color: var(--muted); }
.model.drop-over .drop-overlay { display: flex; }
.model.uploading { opacity: .85; pointer-events: none; }
.card-uploading { font-size: 12px; color: var(--accent-ink); font-weight: 600; }
.card-progress { width: 100%; height: 6px; }
.model.just-updated { animation: card-flash 1.6s ease-out; }
@keyframes card-flash { from { box-shadow: 0 0 0 3px var(--accent); } to { box-shadow: 0 0 0 0 transparent; } }
@media (prefers-reduced-motion: reduce) { .model.just-updated { animation: none; border-color: var(--accent); } }
.name-warning { margin-top: 8px; font-size: 13px; color: #9a5b06; }
@media (prefers-color-scheme: dark) { .name-warning { color: #f5a524; } }

/* Edit dialog */
.edit-dialog { width: min(560px, calc(100vw - 32px)); padding: 0; color: var(--text); }
.edit-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.edit-dialog form { padding: 20px; display: grid; gap: 14px; }
.edit-dialog h2 { font-size: 17px; }
.edit-dialog .row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.edit-dialog textarea { resize: vertical; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
@media (max-width: 560px) { .edit-dialog .row { grid-template-columns: 1fr; } }
.model .desc { color: var(--muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.model .meta { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12px; color: var(--muted); }
.empty { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- viewer page ---------- */
body.viewer { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.viewer-main { flex: 1; display: flex; min-height: 0; }
.stage { flex: 1; display: flex; flex-direction: column; background: var(--surface-2); min-width: 0; }
.frame-wrap { flex: 1; position: relative; min-height: 0; }
.stage iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #fff; }
.stage .loading { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }

/* Revision bar above the model */
.rev-bar {
  display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px;
  padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.rev-bar label { margin: 0; font-size: 13px; color: var(--muted); }
.rev-bar select { width: auto; padding: 5px 8px; }
.rev-bar .spacer { flex: 1; }
.rev-status { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.rev-actions { display: flex; align-items: center; gap: 8px; }
.rev-actions button { padding: 5px 12px; }
.approval-notice { font-size: 12px; color: var(--muted); max-width: 420px; }
.rev-actions .btn { padding: 5px 12px; font-size: 13px; }
.older-note { font-size: 13px; color: #9a5b06; }
@media (prefers-color-scheme: dark) { .older-note { color: #f5a524; } }
.rev-bar progress { width: 120px; }
.this-rev { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 500; color: var(--muted); margin: 0; cursor: pointer; }
.comment-head .badge.rev { font-size: 11px; padding: 0 6px; }

.panel {
  width: 380px; flex: none;
  display: flex; flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  min-height: 0;
}
body.panel-hidden .panel { display: none; }
.panel-head { padding: 12px 14px; border-bottom: 1px solid var(--border); display: grid; gap: 10px; }
.panel-head .title { display: flex; align-items: center; gap: 8px; }
.tabs { display: flex; gap: 4px; }
.tabs button { padding: 4px 10px; font-size: 13px; border-color: transparent; }
.tabs button.active { background: var(--surface-2); border-color: var(--border); font-weight: 600; }

.threads { flex: 1; overflow-y: auto; padding: 10px 14px; display: grid; gap: 10px; align-content: start; }
.thread { border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px; display: grid; gap: 8px; }
.thread.resolved { opacity: .75; }
.comment { display: grid; gap: 4px; }
.comment + .comment, .replies { border-top: 1px solid var(--border); padding-top: 8px; }
.replies { display: grid; gap: 8px; padding-left: 10px; }
.comment-head { display: flex; align-items: baseline; gap: 8px; }
.avatar {
  width: 22px; height: 22px; border-radius: 50%; flex: none;
  display: grid; place-items: center;
  font-size: 11px; font-weight: 700; color: #fff;
  align-self: center;
}
.author { font-weight: 600; }
.time { font-size: 12px; color: var(--muted); }
.body { white-space: pre-wrap; word-wrap: break-word; }
.shot { max-width: 100%; max-height: 180px; border-radius: 6px; border: 1px solid var(--border); cursor: zoom-in; display: block; }
.thread-actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.resolved-note { font-size: 12px; color: var(--ok); }

.composer { border-top: 1px solid var(--border); padding: 12px 14px; display: grid; gap: 8px; }
.composer textarea { min-height: 70px; resize: vertical; }
.composer .row { display: flex; align-items: center; gap: 8px; }
.composer .row .spacer { flex: 1; }
.replying { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.preview { position: relative; display: inline-block; }
.preview img { max-height: 90px; border-radius: 6px; border: 1px solid var(--border); display: block; }
.preview button { position: absolute; top: 4px; right: 4px; padding: 0 6px; font-size: 12px; }
.hint { font-size: 12px; color: var(--muted); }

.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.8); display: grid; place-items: center; z-index: 10; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; border-radius: 6px; }

@media (max-width: 760px) {
  .upload .row, .new-account, .new-account[data-role=Company] { grid-template-columns: 1fr; }
  .crumbs .project, .crumbs .sep.p { display: none; }
  .viewer-topbar { grid-template-columns: 1fr auto; }
  .viewer-topbar .company-banner { grid-column: 1 / -1; grid-row: 2; justify-self: center; }
  table.accounts .hide-sm { display: none; }
  .who .badge, #user-area .btn { display: none; }
  .viewer-main { flex-direction: column; }
  .panel { width: auto; height: 50%; border-left: 0; border-top: 1px solid var(--border); }
}
