/* ==========================================================================
   Terrana Group Directory (Teams personal tab)
   Heritage palette: Navy #0B1F3A, Metallic Gold #D4AF37, Steel #AABBD2, Paper #F7F8FA
   Theme classes on <body>: theme-light | theme-dark | theme-contrast
   ========================================================================== */

:root {
  --navy-950: #04101f;
  --navy-900: #0b1f3a;
  --navy-800: #143156;
  --navy-700: #1d4270;
  --blue-500: #3a608f;
  --gold-400: #d4af37;
  --gold-500: #9a7b1f;
  --gold-100: #f6efd2;
  --steel: #aabbd2;
  --silver: #bbc2c8;
  --paper: #f7f8fa;

  --font-display: "Playfair Display", Georgia, "Times New Roman", serif;
  --font-brand: "Montserrat", "Segoe UI", sans-serif;
  --font-body: "Segoe UI", -apple-system, "Inter", Roboto, Arial, sans-serif;

  --radius: 12px;
  --radius-sm: 8px;
  --node-w: 200px;
  --avatar: 48px;

  /* presence */
  --p-available: #6bb700;
  --p-busy: #c4314b;
  --p-away: #ffaa44;
  --p-offline: #8a8886;
}

/* Light (Teams "default") */
body.theme-light {
  --bg: var(--paper);
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --ink: #1c2530;
  --muted: #5c6672;
  --line: #e3e7ec;
  --line-strong: #c9d1db;
  --heading: var(--navy-900);
  --accent: var(--gold-500);
  --accent-fill: var(--gold-400);
  --accent-wash: var(--gold-100);
  --focus: var(--gold-400);
  --topbar-bg: var(--navy-900);
  --topbar-ink: var(--paper);
  --topbar-muted: var(--steel);
  --shadow: 0 1px 3px rgba(11, 26, 48, .08), 0 6px 18px rgba(11, 26, 48, .08);
  --shadow-lg: 0 24px 60px rgba(11, 26, 48, .22);
  --avatar-ink: var(--paper);
}

/* Dark */
body.theme-dark {
  --bg: var(--navy-950);
  --surface: var(--navy-900);
  --surface-2: var(--navy-800);
  --ink: var(--paper);
  --muted: var(--steel);
  --line: rgba(255, 255, 255, .10);
  --line-strong: rgba(255, 255, 255, .22);
  --heading: var(--paper);
  --accent: var(--gold-400);
  --accent-fill: var(--gold-400);
  --accent-wash: rgba(212, 175, 55, .14);
  --focus: var(--gold-400);
  --topbar-bg: #020a16;
  --topbar-ink: var(--paper);
  --topbar-muted: var(--steel);
  --shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  --shadow-lg: 0 24px 60px rgba(0, 0, 0, .6);
  --avatar-ink: var(--paper);
}

/* High contrast (Teams "contrast") */
body.theme-contrast {
  --bg: #000;
  --surface: #000;
  --surface-2: #000;
  --ink: #fff;
  --muted: #fff;
  --line: #fff;
  --line-strong: #fff;
  --heading: #fff;
  --accent: #ffff00;
  --accent-fill: #ffff00;
  --accent-wash: #000;
  --focus: #1aebff;
  --topbar-bg: #000;
  --topbar-ink: #fff;
  --topbar-muted: #fff;
  --shadow: none;
  --shadow-lg: none;
  --avatar-ink: #000;
}

/* Reset ------------------------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-body);
  font-size: 14px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
img { max-width: 100%; display: block; }
button { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { font-family: var(--font-display); font-weight: 600; color: var(--heading); margin: 0; line-height: 1.2; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

/* Top bar ---------------------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: var(--topbar-bg);
  color: var(--topbar-ink);
  border-bottom: 1px solid rgba(212, 175, 55, .35);
  flex: 0 0 auto;
  min-height: 52px;
}
body.theme-contrast .topbar { border-bottom-color: #fff; }
.brand { display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.brand-mark { height: 30px; width: auto; }
.brand-mark-light { display: none; }
.brand-text { font-family: var(--font-brand); font-weight: 600; letter-spacing: .13em; font-size: .86rem; color: #c9cfd6; }
.brand-text span { color: var(--gold-400); }
body.theme-contrast .brand-text, body.theme-contrast .brand-text span { color: #fff; }
.brand-sep { width: 1px; height: 22px; background: rgba(255, 255, 255, .2); margin: 0 2px; }
.brand-app { font-family: var(--font-display); font-size: 1.05rem; color: var(--topbar-ink); }

.views { display: flex; gap: 2px; background: rgba(255, 255, 255, .08); padding: 3px; border-radius: 999px; }
.view-btn {
  border: 0; background: transparent; color: var(--topbar-muted);
  padding: 5px 14px; border-radius: 999px; cursor: pointer; font-weight: 600; font-size: .82rem;
}
.view-btn.is-active { background: var(--gold-400); color: var(--navy-950); }
body.theme-contrast .views { background: #000; border: 1px solid #fff; }
body.theme-contrast .view-btn.is-active { background: #ffff00; color: #000; }

.topbar-tools { margin-left: auto; display: flex; align-items: center; gap: 8px; min-width: 0; }
.search {
  display: flex; align-items: center; gap: 8px;
  background: rgba(255, 255, 255, .10); border: 1px solid rgba(255, 255, 255, .14);
  border-radius: 999px; padding: 0 12px; height: 34px; width: min(340px, 40vw);
}
.search:focus-within { border-color: var(--gold-400); }
.search svg { width: 16px; height: 16px; fill: var(--topbar-muted); flex: 0 0 auto; }
.search input {
  border: 0; background: transparent; color: var(--topbar-ink); width: 100%; outline: none; font: inherit;
}
.search input::placeholder { color: var(--topbar-muted); opacity: .9; }
.search input::-webkit-search-cancel-button { filter: invert(1); }
body.theme-contrast .search { background: #000; border-color: #fff; }

.icon-btn {
  width: 34px; height: 34px; border-radius: 50%; border: 1px solid transparent; background: transparent;
  display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: inherit;
}
.icon-btn svg { width: 18px; height: 18px; fill: currentColor; }
.topbar .icon-btn { color: var(--topbar-muted); }
.topbar .icon-btn:hover { background: rgba(255, 255, 255, .12); color: var(--topbar-ink); }
body.theme-contrast .icon-btn { border-color: #fff; }

/* Status line ------------------------------------------------------------ */
.status {
  flex: 0 0 auto; font-size: .78rem; color: var(--muted); padding: 4px 16px; min-height: 22px;
  border-bottom: 1px solid var(--line); background: var(--surface);
}
.status:empty { display: none; }
.status.is-error { color: var(--p-busy); }

/* Main ------------------------------------------------------------------- */
.main { flex: 1 1 auto; min-height: 0; position: relative; display: flex; }
.view { display: none; flex: 1 1 auto; min-width: 0; min-height: 0; flex-direction: column; }
.view.is-active { display: flex; }

/* Toolbars */
.org-toolbar, .roster-toolbar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 16px; border-bottom: 1px solid var(--line); background: var(--surface); flex: 0 0 auto;
}
.org-toolbar-group { display: inline-flex; align-items: center; gap: 4px; }
.org-toolbar-group + .org-toolbar-group { margin-left: 8px; padding-left: 12px; border-left: 1px solid var(--line); }
.tool-btn {
  height: 30px; min-width: 30px; padding: 0 8px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); cursor: pointer; font-weight: 600;
}
.tool-btn:hover { border-color: var(--accent); color: var(--accent); }
.tool-btn-text { font-weight: 500; padding: 0 12px; font-size: .82rem; }
.tool-label { min-width: 46px; text-align: center; font-size: .8rem; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Org chart canvas */
.org-canvas {
  flex: 1 1 auto; overflow: auto; position: relative; cursor: grab;
  background-image: radial-gradient(circle, var(--line-strong) 1px, transparent 1px);
  background-size: 24px 24px;
}
body.theme-contrast .org-canvas { background-image: none; }
.org-canvas.is-panning { cursor: grabbing; user-select: none; }
.org-stage { display: inline-block; min-width: 100%; padding: 28px 32px 60px; }
.org-empty { padding: 48px; text-align: center; color: var(--muted); }

.tree ul { display: flex; padding: 28px 0 0; margin: 0; list-style: none; position: relative; justify-content: center; }
.tree ul.root { padding-top: 0; gap: 40px; align-items: flex-start; }
.tree li { position: relative; display: flex; flex-direction: column; align-items: center; padding: 28px 10px 0; }
.tree ul.root > li { padding-top: 0; }
/* connectors: each li draws the half-width horizontal bar above it plus a vertical stub down to its card */
.tree li::before, .tree li::after {
  content: ""; position: absolute; top: 0; right: 50%; width: 50%; height: 28px;
  border-top: 2px solid var(--line-strong);
}
.tree li::after { right: auto; left: 50%; border-left: 2px solid var(--line-strong); }
.tree li:only-child::before { display: none; }
.tree li:only-child::after { border-top: 0; width: 0; }
.tree li:first-child::before, .tree li:last-child::after { border: 0 none; }
.tree li:last-child::before { border-right: 2px solid var(--line-strong); border-radius: 0 8px 0 0; }
.tree li:first-child::after { border-radius: 8px 0 0 0; }
/* vertical line from a parent card down to its children row */
.tree ul:not(.root)::before {
  content: ""; position: absolute; top: 0; left: 50%; width: 0; height: 28px; border-left: 2px solid var(--line-strong);
}
.tree ul.root > li::before, .tree ul.root > li::after { display: none; }
.tree li.is-collapsed > ul { display: none; }

/* Node card */
.node {
  width: var(--node-w); background: var(--surface); border: 1px solid var(--line);
  border-top: 3px solid var(--accent-fill); border-radius: var(--radius); padding: 14px 12px 12px;
  box-shadow: var(--shadow); text-align: center; cursor: pointer; position: relative;
  transition: transform .12s ease, box-shadow .12s ease, opacity .15s ease;
}
.node:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.node.is-selected { border-color: var(--accent-fill); box-shadow: 0 0 0 3px var(--accent-wash), var(--shadow-lg); }
.node.is-dim { opacity: .28; }
.node.is-hit { border-color: var(--accent-fill); }
.node.is-me .node-name::after { content: "you"; font-size: .62rem; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--accent); margin-left: 6px; }
.node .avatar { margin: 0 auto 8px; }
.node-name { font-weight: 700; color: var(--heading); font-size: .92rem; line-height: 1.25; }
.node-title { color: var(--ink); font-size: .8rem; margin-top: 2px; }
.node-dept { color: var(--muted); font-size: .72rem; margin-top: 4px; text-transform: uppercase; letter-spacing: .06em; }
.node-toggle {
  position: absolute; left: 50%; bottom: -14px; transform: translateX(-50%);
  min-width: 28px; height: 28px; padding: 0 8px; border-radius: 999px; border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--muted); font-size: .72rem; font-weight: 700; cursor: pointer; box-shadow: var(--shadow);
}
.node-toggle:hover { color: var(--accent); border-color: var(--accent); }
.is-collapsed > .node .node-toggle { background: var(--accent-fill); color: var(--navy-950); border-color: var(--accent-fill); }
body.theme-contrast .is-collapsed > .node .node-toggle { color: #000; }

/* Avatar + presence */
.avatar {
  position: relative; width: var(--avatar); height: var(--avatar); border-radius: 50%; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: var(--avatar-ink);
  background: var(--navy-800); overflow: visible; font-size: calc(var(--avatar) * .36); letter-spacing: .02em;
}
.avatar img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.avatar.c1 { background: #0b1f3a; } .avatar.c2 { background: #143156; } .avatar.c3 { background: #1d4270; }
.avatar.c4 { background: #3a608f; } .avatar.c5 { background: #9a7b1f; } .avatar.c6 { background: #5c6672; }
body.theme-contrast .avatar { background: #fff; color: #000; border: 1px solid #fff; }
.avatar.lg { --avatar: 88px; }
.avatar.sm { --avatar: 36px; }
.presence {
  position: absolute; right: -1px; bottom: -1px; width: 30%; height: 30%; border-radius: 50%;
  background: var(--p-offline); border: 2px solid var(--surface); display: none;
}
.presence.is-set { display: block; }
.presence.available { background: var(--p-available); }
.presence.busy, .presence.dnd { background: var(--p-busy); }
.presence.away { background: var(--p-away); }
.presence.offline { background: var(--surface); box-shadow: inset 0 0 0 2px var(--p-offline); }
.presence.dnd::after { content: ""; position: absolute; left: 25%; right: 25%; top: 45%; height: 12%; background: #fff; border-radius: 2px; }

/* Roster ----------------------------------------------------------------- */
.select select {
  height: 30px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--ink); padding: 0 8px; font: inherit; font-size: .82rem;
}
.check { display: inline-flex; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); cursor: pointer; }
.check input { accent-color: var(--gold-400); }
.roster-count { margin-left: auto; font-size: .78rem; color: var(--muted); }
.roster-list { flex: 1 1 auto; overflow: auto; padding: 12px 16px 40px; }
.roster-group { margin-bottom: 20px; }
.roster-group-h {
  display: flex; align-items: baseline; gap: 8px; margin: 6px 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--line);
}
.roster-group-h h3 { font-size: 1.05rem; }
.roster-group-h span { color: var(--muted); font-size: .78rem; }
.roster-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 10px; }
.card {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px;
  box-shadow: var(--shadow); cursor: pointer; text-align: left; min-width: 0;
}
.card:hover { border-color: var(--accent-fill); }
.card.is-selected { border-color: var(--accent-fill); box-shadow: 0 0 0 3px var(--accent-wash), var(--shadow); }
.card-main { min-width: 0; }
.card-name { font-weight: 700; color: var(--heading); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-title { font-size: .82rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-dept { font-size: .72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-top: 2px; }
.card-actions { display: flex; flex-direction: column; gap: 2px; }
.card-actions .icon-btn { width: 28px; height: 28px; color: var(--muted); }
.card-actions .icon-btn:hover { color: var(--accent); background: var(--accent-wash); }
.card-actions .icon-btn svg { width: 15px; height: 15px; }
.roster-empty { color: var(--muted); padding: 40px; text-align: center; }
mark { background: var(--accent-wash); color: inherit; border-radius: 2px; padding: 0 1px; }
body.theme-contrast mark { background: #ffff00; color: #000; }

/* State view (loading / sign in / errors) --------------------------------- */
.state-view { align-items: center; justify-content: center; padding: 24px; }
.state-card {
  max-width: 420px; text-align: center; background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; padding: 32px 28px; box-shadow: var(--shadow);
}
.state-mark { height: 56px; width: auto; margin: 0 auto 14px; }
.state-mark-dark { display: none; }
body.theme-dark .state-mark-light, body.theme-contrast .state-mark-light { display: none; }
body.theme-dark .state-mark-dark, body.theme-contrast .state-mark-dark { display: block; }
.state-card h2 { font-size: 1.4rem; margin-bottom: 8px; }
.state-card p { color: var(--muted); margin: 0 0 16px; }
.state-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.btn {
  height: 36px; padding: 0 18px; border-radius: 999px; border: 1px solid var(--accent-fill);
  background: var(--accent-fill); color: var(--navy-950); font-weight: 700; cursor: pointer;
}
.btn.secondary { background: transparent; color: var(--ink); border-color: var(--line-strong); }
.btn:hover { filter: brightness(1.05); }
body.theme-contrast .btn { color: #000; }
.spinner {
  width: 26px; height: 26px; border-radius: 50%; margin: 0 auto 10px;
  border: 3px solid var(--line); border-top-color: var(--accent-fill); animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Drawer ----------------------------------------------------------------- */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(4, 16, 31, .35); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(400px, 100vw); z-index: 21;
  background: var(--surface); border-left: 1px solid var(--line); box-shadow: var(--shadow-lg);
  overflow: auto; animation: slide .18s ease-out;
}
@keyframes slide { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer-close { position: absolute; top: 10px; right: 10px; color: var(--muted); z-index: 2; }
.drawer-close:hover { background: var(--surface-2); color: var(--ink); }
.drawer-body { padding: 0 0 24px; }
.drawer-hero {
  padding: 28px 24px 18px; text-align: center; border-bottom: 1px solid var(--line);
  background: linear-gradient(180deg, var(--accent-wash), transparent);
}
.drawer-hero .avatar { margin: 0 auto 12px; }
.drawer-hero h2 { font-size: 1.35rem; }
.drawer-hero .d-title { margin-top: 4px; }
.drawer-hero .d-dept { color: var(--muted); font-size: .76rem; text-transform: uppercase; letter-spacing: .08em; margin-top: 4px; }
.drawer-hero .d-presence { margin-top: 6px; font-size: .78rem; color: var(--muted); }
.drawer-actions { display: flex; gap: 8px; justify-content: center; padding: 14px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.action {
  display: inline-flex; flex-direction: column; align-items: center; gap: 4px; min-width: 64px;
  padding: 8px 6px; border-radius: var(--radius-sm); border: 1px solid var(--line); background: var(--surface);
  color: var(--ink); cursor: pointer; font-size: .72rem;
}
.action svg { width: 20px; height: 20px; fill: var(--accent); }
.action:hover { border-color: var(--accent-fill); background: var(--accent-wash); }
.drawer-section { padding: 14px 24px 0; }
.drawer-section h4 {
  margin: 0 0 8px; font-size: .7rem; letter-spacing: .1em; text-transform: uppercase; color: var(--muted); font-weight: 700;
}
.kv { display: grid; grid-template-columns: 92px 1fr; gap: 6px 10px; font-size: .86rem; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.kv a { color: var(--accent); text-decoration: none; }
.kv a:hover { text-decoration: underline; }
.person-row {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
  padding: 6px 8px; border-radius: var(--radius-sm); border: 1px solid transparent; background: transparent; cursor: pointer;
}
.person-row:hover { background: var(--surface-2); border-color: var(--line); }
.person-row .avatar { --avatar: 32px; font-size: .7rem; }
.person-row b { display: block; color: var(--heading); font-size: .86rem; }
.person-row span { display: block; color: var(--muted); font-size: .74rem; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--navy-900); color: var(--paper); padding: 8px 14px; border-radius: 999px; font-size: .8rem;
  box-shadow: var(--shadow-lg); z-index: 30; opacity: 0; transition: opacity .15s; pointer-events: none;
}
.toast.is-on { opacity: 1; }

/* Responsive ------------------------------------------------------------- */
@media (max-width: 860px) {
  .brand-text, .brand-sep { display: none; }
  .search { width: min(240px, 44vw); }
}
@media (max-width: 600px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  .topbar-tools { margin-left: 0; width: 100%; }
  .search { width: 100%; }
  .brand-app { display: none; }
  .drawer { width: 100vw; }
  :root { --node-w: 168px; }
}
