:root {
  --bg: #f5f6fb;
  --panel: #ffffff;
  --ink: #2f3349;
  --muted: #8b90a5;
  --line: #ecedf5;
  --primary: #3b82f6;
  --primary-ink: #ffffff;
  --c-blue: #3b82f6;
  --c-green: #22c55e;
  --c-orange: #f59e0b;
  --c-red: #f43f5e;
  --c-purple: #7c5cfc;
  --soft: #eef3ff;
  --shadow: 0 2px 10px rgba(28, 39, 76, 0.06);
  --shadow-hover: 0 6px 18px rgba(28, 39, 76, 0.10);
  --radius: 14px;
}
:root[data-theme="dark"] {
  --bg: #151827;
  --panel: #1d2135;
  --ink: #e7e9f3;
  --muted: #9aa1bd;
  --line: #2a2f49;
  --soft: #232a45;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.35);
  --shadow-hover: 0 6px 18px rgba(0, 0, 0, 0.45);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Segoe UI", "Sarabun", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.55;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
code { background: var(--soft); padding: 1px 6px; border-radius: 6px; font-size: 12.5px; }
pre.notes { background: var(--soft); padding: 12px; border-radius: 10px; white-space: pre-wrap; word-break: break-word; }
.muted { color: var(--muted); }
.right { text-align: right; }
.center { text-align: center; }

/* ---------- Topbar ---------- */
.topbar { background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; box-shadow: var(--shadow); }
.topbar-inner { width: 100%; display: flex; align-items: center; gap: 18px; padding: 12px 22px; }
.brand { font-size: 19px; font-weight: 800; color: var(--primary); display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-mark { font-size: 20px; }
.search { position: relative; }
.search input { width: 190px; transition: width .2s; padding: 9px 14px 9px 36px; border: 1px solid var(--line); border-radius: 30px; background: var(--bg); color: var(--ink); }
.search-ic { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); opacity: .6; font-size: 13px; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.search input:focus { width: 280px; }
/* main menu lives in the top bar */
.topnav { display: flex; align-items: center; gap: 2px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.topnav::-webkit-scrollbar { display: none; }
.topnav a { color: var(--muted); padding: 8px 12px; font-weight: 600; font-size: 14px; border-radius: 10px; white-space: nowrap; }
.topnav a:hover { color: var(--ink); background: var(--soft); text-decoration: none; }
.topnav a.active { color: var(--primary); background: var(--soft); }
.icon-btn { background: transparent; border: none; width: 38px; height: 38px; border-radius: 9px; cursor: pointer; font-size: 17px; color: var(--muted); display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.icon-btn:hover { background: var(--soft); color: var(--ink); text-decoration: none; }
.tb-divider { width: 1px; height: 28px; background: var(--line); margin: 0 4px; }
.profile { display: flex; align-items: center; gap: 10px; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: linear-gradient(135deg, #4d9bff, #2f7bf6); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; }
.profile-meta { display: flex; flex-direction: column; line-height: 1.25; }
.profile-meta strong { font-size: 13.5px; }
.profile-role { font-size: 12px; color: var(--muted); text-transform: capitalize; }

/* ---------- Menubar ---------- */

/* ---------- Content ---------- */
.content { width: 100%; padding: 26px 22px 40px; flex: 1 0 auto; }
.foot { text-align: center; color: var(--muted); padding: 18px; font-size: 12.5px; flex-shrink: 0; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; }
.head-actions { display: flex; gap: 8px; align-items: center; }

/* ---------- Cards ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px 22px; margin-bottom: 20px; box-shadow: var(--shadow); }
.card h2 { font-size: 15px; margin: 0 0 14px; }
.card h3 { font-size: 12.5px; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }

/* Split form: left = input, right = live preview */
.split-form { display: grid; grid-template-columns: 1.45fr 1fr; gap: 20px; align-items: start; }
.preview-card { position: sticky; top: 130px; }
.preview .pv-title { margin: 0 0 10px; font-size: 18px; font-weight: 800; }
.preview .pv-badges { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.preview .pv-sec { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.preview .pv-label { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin-bottom: 6px; }
.preview .pv-empty { color: var(--muted); }
.preview .pv-row { padding: 4px 0; }

/* ---------- Stat cards ---------- */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 18px; margin-bottom: 22px; }
.stat-card { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--primary); border-radius: var(--radius); padding: 18px 20px; color: var(--ink); display: flex; align-items: center; justify-content: space-between; box-shadow: var(--shadow); transition: box-shadow .15s, transform .15s; }
.stat-card:hover { box-shadow: var(--shadow-hover); transform: translateY(-2px); text-decoration: none; }
.stat-label { color: var(--muted); font-size: 13px; }
.stat-num { font-size: 30px; font-weight: 800; margin-top: 2px; }
.stat-ic { width: 54px; height: 54px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 24px; color: #fff; box-shadow: 0 6px 14px rgba(0,0,0,.12); }
.accent-blue { border-left-color: var(--c-blue); } .accent-blue .stat-num { color: var(--c-blue); }
.accent-green { border-left-color: var(--c-green); } .accent-green .stat-num { color: var(--c-green); }
.accent-orange { border-left-color: var(--c-orange); } .accent-orange .stat-num { color: var(--c-orange); }
.accent-purple { border-left-color: var(--c-purple); } .accent-purple .stat-num { color: var(--c-purple); }
.g-blue { background: linear-gradient(135deg, #4d9bff, #2f7bf6); }
.g-green { background: linear-gradient(135deg, #34d399, #10b981); }
.g-orange { background: linear-gradient(135deg, #fbbf5a, #f59e0b); }
.g-purple { background: linear-gradient(135deg, #a78bfa, #7c5cfc); }

/* ---------- Tables ---------- */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table th { color: var(--muted); font-weight: 600; font-size: 12.5px; }
.table tbody tr:hover { background: var(--soft); }
.table.kv th { width: 130px; }
.arrow { color: var(--primary); font-weight: 700; }
.list { list-style: none; padding: 0; margin: 0; }
.list li { padding: 9px 0; border-bottom: 1px solid var(--line); }

/* ---------- Buttons ---------- */
.btn { display: inline-block; background: var(--panel); border: 1px solid var(--line); color: var(--ink); padding: 8px 15px; border-radius: 9px; cursor: pointer; font-size: 13.5px; font-weight: 600; }
.btn:hover { border-color: #c7cfe0; text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-danger { background: transparent; border-color: rgba(244,63,94,.4); color: var(--c-red); }
.btn-danger:hover { background: var(--c-red); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; }
.btn-block { display: block; width: 100%; }
.inline { display: inline; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: 15px; }
.form h2 { font-size: 14px; margin: 8px 0 2px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12.5px; color: var(--muted); font-weight: 600; }
input, select, textarea { font-family: inherit; font-size: 14px; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; background: var(--panel); color: var(--ink); width: 100%; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(59,130,246,.12); }
textarea { resize: vertical; }
.check { display: flex; align-items: center; gap: 8px; }
.check input { width: auto; }
.form-actions { margin-top: 10px; text-align: right; }

/* ---------- Repeatable rows ---------- */
.repeat { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.rrow { display: grid; grid-template-columns: 1fr 1fr auto; gap: 8px; align-items: center; }
.rrow-wide { grid-template-columns: 1.4fr 1fr 1fr 1fr auto; }

/* ---------- Badges / chips ---------- */
.badge { display: inline-block; background: var(--soft); color: #48546b; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 700; }
.badge-soft { background: var(--soft); color: var(--primary); }
.badge-ok { background: rgba(34,197,94,.14); color: var(--c-green); }
.badge-muted { background: var(--soft); color: var(--muted); }
.badge-danger { background: rgba(244,63,94,.14); color: var(--c-red); }
.role-super { background: rgba(124,92,252,.16); color: var(--c-purple); }
.role-dev { background: rgba(59,130,246,.16); color: var(--c-blue); }
.role-view { background: var(--soft); color: var(--muted); }
.status-active { background: rgba(34,197,94,.14); color: var(--c-green); }
.status-dev { background: rgba(245,158,11,.16); color: var(--c-orange); }
.status-inactive, .status-archived, .status-decommissioned { background: var(--soft); color: var(--muted); }
.kind-calls-api { background: rgba(59,130,246,.16); color: var(--c-blue); }
.kind-same-codebase { background: rgba(124,92,252,.16); color: var(--c-purple); }
.kind-shares-server { background: rgba(245,158,11,.16); color: var(--c-orange); }
.kind-shares-infra { background: rgba(34,197,94,.14); color: var(--c-green); }
.kind-other { background: var(--soft); color: var(--muted); }
.chip { display: inline-block; background: var(--soft); color: var(--ink); padding: 3px 11px; border-radius: 20px; font-size: 12.5px; margin: 2px 3px 2px 0; }
.chip-org { background: transparent; border: 1px solid var(--line); color: var(--muted); font-weight: 600; text-decoration: none; }
.chip-org:hover { border-color: var(--primary); color: var(--primary); text-decoration: none; }
.badges-row { margin-bottom: 18px; display: flex; gap: 6px; flex-wrap: wrap; }
.env { display: inline-block; margin: 2px 4px 2px 0; }

/* ---------- Flash ---------- */
.flash { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; font-weight: 600; }
.flash-success { background: rgba(34,197,94,.14); color: var(--c-green); }
.flash-error { background: rgba(244,63,94,.14); color: var(--c-red); }

/* ---------- Filter bar ---------- */
.filter-bar { display: flex; gap: 8px; margin-bottom: 18px; flex-wrap: wrap; }
.filter-bar input { max-width: 260px; }
.filter-bar select { max-width: 200px; }

/* ---------- Auth ---------- */
.center-body { background: radial-gradient(1200px 500px at 50% -10%, #2a3350, #151827); display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-wrap { width: 100%; max-width: 370px; }
.login-card { padding: 30px; }
.login-title { font-size: 20px; margin: 0 0 4px; }

/* ---------- Error ---------- */
.error-code { font-size: 54px; font-weight: 800; color: var(--primary); }

@media (max-width: 900px) {
  .split-form { grid-template-columns: 1fr; }
  .preview-card { position: static; }
}
@media (max-width: 720px) {
  .topbar-inner { flex-wrap: wrap; gap: 10px; }
  .topnav { order: 3; flex-basis: 100%; }
  .search input, .search input:focus { width: 150px; }
  .profile-meta { display: none; }
  .grid-2 { grid-template-columns: 1fr; }
  .rrow, .rrow-wide { grid-template-columns: 1fr; }
}

/* ---------- Sitemap-style Map (partials/pmap.ejs) ---------- */
.pmap-card { padding-bottom: 10px; }
.pmap-head { display:flex; align-items:center; justify-content:space-between; gap:12px; flex-wrap:wrap; margin-bottom:6px; }
.pmap-legend { font-size:12px; color:var(--muted); display:inline-flex; align-items:center; gap:6px; }
.lv { display:inline-block; border-radius:50%; margin-left:8px; }
.lv1 { width:14px; height:14px; background:#14b8a6; }
.lv2 { width:11px; height:11px; background:var(--c-orange); }
.lv3 { width:8px; height:8px; background:var(--c-blue); }
.pmap { overflow-x:auto; padding:6px 2px 8px; }
.pmap-inner { display:flex; align-items:flex-start; position:relative; min-width:min-content; width:100%; }
/* the horizontal rail through the category icons */
.pmap-inner::before { content:''; position:absolute; left:20px; right:40px; top:59px; height:2px; background:var(--muted); opacity:.45; }
.pmap-root { position:relative; z-index:1; margin-top:27px; margin-right:14px; min-width:150px; max-width:200px; background:var(--ink); color:var(--panel);
  border-radius:10px; padding:9px 14px; box-shadow:var(--shadow); }
.pmap-root-name { font-weight:800; font-size:14px; line-height:1.3; word-break:break-word; }
.pmap-root-meta { font-size:11px; opacity:.7; }
.pmap-root-links { display:flex; gap:10px; margin-top:4px; font-size:12px; }
.pmap-root-links a { color:inherit; opacity:.85; text-decoration:underline; }
.pmap-col { flex:1 1 0; min-width:135px; max-width:300px; padding:0 8px; }
.pmap-col h4 { height:32px; line-height:44px; margin:0 0 8px; font-size:13.5px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.pmap-ico { position:relative; z-index:1; width:38px; height:38px; border-radius:50%; display:grid; place-items:center; font-size:17px;
  background:#14b8a6; border:3px solid var(--panel); box-shadow:0 0 0 2px #14b8a6; }
.pmap-l2, .pmap-l3 { list-style:none; margin:0; }
.pmap-l2 { margin-left:18px; border-left:2px solid var(--line); padding:8px 0 2px 18px; }
.pmap-l2 > li { position:relative; padding:5px 0; font-size:13px; line-height:1.35; overflow-wrap:anywhere; }
.pmap-l2 > li::before { content:''; position:absolute; left:-26px; top:7px; width:12px; height:12px; border-radius:50%;
  background:var(--c-orange); border:2px solid var(--panel); box-shadow:0 0 0 1.5px var(--c-orange); }
.pmap-l3 { margin:4px 0 2px 4px; border-left:2px solid var(--line); padding-left:14px; }
.pmap-l3 > li { position:relative; padding:2px 0; font-size:12px; color:var(--muted); }
.pmap-l3 > li::before { content:''; position:absolute; left:-19px; top:7px; width:8px; height:8px; border-radius:50%; background:var(--c-blue); }
.pmap code { font-size:11.5px; word-break:normal; overflow-wrap:anywhere; }
.pmap-more-cb:not(:checked) + .pmap-l2 .pmap-extra { display:none; }
.pmap-more-cb:checked + .pmap-l2 .m-show, .pmap-more-cb:not(:checked) + .pmap-l2 .m-hide { display:none; }
.pmap-l2 > li.pmap-more::before { background:var(--panel); box-shadow:0 0 0 1.5px var(--muted); }
.pmap-more label { cursor:pointer; color:var(--primary); font-size:12.5px; font-weight:600; margin:0; }
.pmap-empty { font-size:12px; margin:6px 0 0; }
.pmap-details { margin-top:4px; }
.pmap-details > summary { cursor:pointer; font-weight:700; padding:10px 4px; color:var(--muted); }
.pmap-details[open] > summary { margin-bottom:10px; }

/* ---------- Dashboard ---------- */
.stat-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.dash-sub { font-size: 12.5px; margin: -6px 0 12px; }
.dash-checks { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; align-items: start; }
.dash-check { border: 1px solid var(--line); border-left: 4px solid var(--muted); border-radius: 10px; background: var(--panel); }
.dash-check.tone-danger { border-left-color: var(--c-red); }
.dash-check.tone-warn { border-left-color: var(--c-orange); }
.dash-check.tone-info { border-left-color: var(--c-blue); }
.dash-check.is-ok { border-left-color: var(--c-green); opacity: .75; }
.dash-check > summary { list-style: none; display: flex; align-items: center; gap: 10px; padding: 12px 14px; cursor: pointer; }
.dash-check > summary::-webkit-details-marker { display: none; }
.dash-check[open] > summary { border-bottom: 1px solid var(--line); }
.dc-ic { font-size: 18px; }
.dc-title { flex: 1; font-weight: 600; font-size: 13.5px; }
.dc-num { font-size: 20px; font-weight: 800; color: var(--ink); }
.dash-check.is-ok .dc-num { color: var(--c-green); }
.dc-list { padding: 10px 14px; display: flex; flex-wrap: wrap; gap: 4px; align-items: center; margin: 0; }
.dc-list .chip { font-size: 12px; }
.hbars { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.hbars li { display: grid; grid-template-columns: minmax(90px, 38%) 1fr 34px; align-items: center; gap: 10px; padding: 3px 4px; border-radius: 8px; }
.hbars li:hover { background: var(--soft); }
.hb-label { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hb-track { height: 12px; }
.hb-bar { height: 100%; min-width: 4px; background: var(--primary); border-radius: 0 4px 4px 0; }
.hb-num { font-size: 13px; font-weight: 700; text-align: right; color: var(--ink); font-variant-numeric: tabular-nums; }
.dash-recent { list-style: none; margin: 0; padding: 0; }
.dash-recent li { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13.5px; }
.dash-recent li:last-child { border-bottom: 0; }
.dr-meta { white-space: nowrap; font-size: 12px; }

/* ---------- Language switch ---------- */
.lang-switch { display: inline-flex; border: 1px solid var(--line); border-radius: 20px; overflow: hidden; background: var(--bg); }
.lang-switch a { padding: 5px 10px; font-size: 12px; font-weight: 700; color: var(--muted); }
.lang-switch a:hover { color: var(--ink); text-decoration: none; }
.lang-switch a.active { background: var(--primary); color: var(--primary-ink); }
.lang-switch-auth { display: flex; width: max-content; margin: 0 auto 12px; background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.15); }
.lang-switch-auth a { color: #cbd2e6; }
.lang-switch a, .topbar-right .btn, .profile-meta strong, .profile-meta span { white-space: nowrap; }
.topnav { flex: 1 1 auto; }
@media (max-width: 1500px) {
  .profile-meta { display: none; }
  .search input { width: 150px; }
  .topbar-inner { gap: 12px; }
  .topnav a { padding: 8px 9px; }
}

a.profile { color: inherit; }
a.profile:hover { text-decoration: none; opacity: .85; }

/* Detail pages: keep long names and relationship tables within the viewport. */
.detail-page { min-width: 0; overflow-wrap: anywhere; }
.detail-page .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.detail-page .card, .detail-page .page-head > * { min-width: 0; max-width: 100%; }
.detail-page .head-actions, .detail-page .pmap-legend { flex-wrap: wrap; }
.detail-page .table.kv { table-layout: fixed; }
.detail-page .badge, .detail-page .chip { max-width: 100%; white-space: normal; }
.detail-table-scroll { max-width: 100%; overflow-x: auto; }
.detail-table-scroll > .table { min-width: 480px; }
.detail-table-scroll:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

@media (max-width: 1000px) {
  .detail-page .grid-2 { grid-template-columns: 1fr; }
  .detail-page .pmap-inner { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; min-width: 0; }
  .detail-page .pmap-inner::before { display: none; }
  .detail-page .pmap-root { grid-column: 1 / -1; margin: 0; min-width: 0; max-width: 100%; }
  .detail-page .pmap-col { min-width: 0; max-width: none; padding: 0; }
  .detail-page .pmap-col h4 { height: auto; line-height: 1.5; white-space: normal; overflow: visible; }
}

@media (max-width: 600px) {
  .detail-page .card { padding: 16px 12px; }
  .detail-page .page-head { align-items: flex-start; }
  .detail-page .page-head h1 { font-size: 20px; }
  .detail-page .head-actions { width: 100%; gap: 6px; }
  .detail-page .pmap-inner { grid-template-columns: minmax(0, 1fr); }
  .detail-page .table.kv tr, .detail-page .table.kv th, .detail-page .table.kv td { display: block; width: 100%; }
  .detail-page .table.kv th { padding-bottom: 2px; border-bottom: 0; }
  .detail-page .table.kv td { padding-top: 2px; }
}
