/* ══════════════════════════════════════════════════════════════════════
   인애드 인트라넷 — Track B (StyleGallery 레이아웃)  /v2
   현행 UI와 완전 분리된 실험 버전. 데이터는 기존 /api·D1 그대로 사용.
   방법론: github.com/changeroa/StyleGallery
   · cascade layers · 레이아웃 값 토큰 · logical properties
   · container queries · 명시적 스크롤 소유권
   이 파일이 "디자인 커스터마이징"의 주 편집 대상입니다.
   ══════════════════════════════════════════════════════════════════════ */
@layer tokens, base, shell, sidebar, topbar, components, views, states, blueprint, responsive;

/* ───────────────────────────────  TOKENS  ─────────────────────────── */
@layer tokens {
  :root {
    --sidebar-w: 260px;
    --sidebar-w-min: 76px;
    --header-h: 60px;
    --content-max: 1440px;
    --gutter: 28px;
    --stack-gap: 22px;
    --card-gap: 16px;
    --radius: 14px;
    --radius-sm: 10px;

    --navy-1: #153088;
    --navy-2: #142a6e;
    --brand: #3563e9;
    --brand-strong: #2748c8;
    --brand-soft: #eaf0ff;

    --ground: #f5f7fc;
    --surface: #ffffff;
    --surface-2: #fbfcfe;
    --line: #e7ecf4;
    --line-strong: #d5dced;
    --ink: #1a2130;
    --muted: #5c6577;
    --faint: #8a93a6;

    --ok: #12b76a;   --ok-bg: #e6f7ef;
    --warn: #f79009; --warn-bg: #fdf1df;
    --bad: #f04438;  --bad-bg: #fde8e6;
    --info: #7c5cfc; --info-bg: #efeafe;

    --bp-line: #00b8d4;
    --bp-ink: #04222a;

    --sans: 'Pretendard', -apple-system, BlinkMacSystemFont, 'Apple SD Gothic Neo',
            'Malgun Gothic', 'Noto Sans KR', system-ui, sans-serif;
    --mono: ui-monospace, 'SF Mono', 'JetBrains Mono', 'Menlo', monospace;

    --shadow-sm: 0 1px 2px rgba(20,34,80,.05), 0 1px 3px rgba(20,34,80,.06);
    --shadow-md: 0 4px 16px rgba(20,34,80,.08);
    --shadow-lg: 0 12px 40px rgba(20,34,80,.16);

    /* 파이프라인 단계 색 */
    --st-lead: #8a93a6; --st-contact: #3563e9; --st-needs: #7c5cfc;
    --st-proposal: #f79009; --st-meeting: #06b6d4; --st-negotiation: #12b76a;
  }
  @media (prefers-color-scheme: dark) {
    :root {
      --ground:#0e131b; --surface:#161d28; --surface-2:#1b2430; --line:#26303f;
      --line-strong:#33404f; --ink:#eaeef6; --muted:#9aa6b9; --faint:#6b7688;
      --brand-soft:#1c2b52; --ok-bg:#0f2a20; --warn-bg:#2c2410; --bad-bg:#2c1614; --info-bg:#1e1a34;
      --shadow-sm:0 1px 2px rgba(0,0,0,.3); --shadow-md:0 6px 20px rgba(0,0,0,.4); --shadow-lg:0 16px 48px rgba(0,0,0,.55);
    }
  }
  :root[data-theme="light"] {
    --ground:#f5f7fc; --surface:#fff; --surface-2:#fbfcfe; --line:#e7ecf4; --line-strong:#d5dced;
    --ink:#1a2130; --muted:#5c6577; --faint:#8a93a6; --brand-soft:#eaf0ff;
    --ok-bg:#e6f7ef; --warn-bg:#fdf1df; --bad-bg:#fde8e6; --info-bg:#efeafe;
  }
  :root[data-theme="dark"] {
    --ground:#0e131b; --surface:#161d28; --surface-2:#1b2430; --line:#26303f; --line-strong:#33404f;
    --ink:#eaeef6; --muted:#9aa6b9; --faint:#6b7688; --brand-soft:#1c2b52;
    --ok-bg:#0f2a20; --warn-bg:#2c2410; --bad-bg:#2c1614; --info-bg:#1e1a34;
  }
}

/* ───────────────────────────────  BASE  ───────────────────────────── */
@layer base {
  *, *::before, *::after { box-sizing: border-box; }
  html, body { block-size: 100%; margin: 0; }
  body {
    font-family: var(--sans); font-size: 14px; line-height: 1.5;
    color: var(--ink); background: var(--ground);
    -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
  }
  h1,h2,h3,h4 { margin: 0; text-wrap: balance; font-weight: 700; letter-spacing: -0.01em; }
  button { font-family: inherit; cursor: pointer; }
  svg { display: block; }
  .icon { inline-size: 18px; block-size: 18px; flex: none; }
  ::selection { background: color-mix(in srgb, var(--brand) 25%, transparent); }
  :focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; border-radius: 4px; }
  .num { font-variant-numeric: tabular-nums; }
  @media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
}

/* ──────────────────────────────  APP SHELL  ───────────────────────── */
@layer shell {
  .app_shell {
    block-size: 100dvh; display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    grid-template-rows: var(--header-h) 1fr;
    grid-template-areas: "aside topbar" "aside main";
    overflow: hidden; transition: grid-template-columns .22s ease;
  }
  .app_shell.is-collapsed { --sidebar-w: var(--sidebar-w-min); }
  .app_shell__aside  { grid-area: aside;  overflow-y: auto; overflow-x: hidden; }
  .app_shell__topbar { grid-area: topbar; }
  .app_shell__main   { grid-area: main;   overflow-y: auto; scroll-behavior: smooth; }
  .page { max-inline-size: var(--content-max); margin-inline: auto; padding: var(--gutter); padding-block-end: 64px; }
  .stack { display: flex; flex-direction: column; gap: var(--stack-gap); }
  .stack--tight { gap: 10px; }
}

/* ───────────────────────────────  SIDEBAR  ────────────────────────── */
@layer sidebar {
  .side { min-block-size: 100%; display: flex; flex-direction: column;
    background: linear-gradient(180deg, var(--navy-1) 0%, var(--navy-2) 100%); color: #dfe6fb; }
  .side__brand { display: flex; align-items: center; gap: 11px; padding-inline: 20px; block-size: var(--header-h); flex: none; border-block-end: 1px solid rgba(255,255,255,.08); }
  .side__logo { inline-size: 32px; block-size: 32px; border-radius: 9px; flex: none; display: grid; place-items: center; font-weight: 800; font-size: 15px; color: #fff; background: linear-gradient(135deg, #4d74ff, #2c4be0); box-shadow: 0 3px 10px rgba(45,74,224,.5); }
  .side__word { font-weight: 800; font-size: 16px; color: #fff; letter-spacing: -.02em; white-space: nowrap; }
  .side__word small { display: block; font-size: 10px; font-weight: 600; color: #9db0e8; letter-spacing: .06em; }
  .is-collapsed .side__word, .is-collapsed .side__label, .is-collapsed .side__user-meta,
  .is-collapsed .nav__text, .is-collapsed .nav__count { display: none; }
  .is-collapsed .side__brand { justify-content: center; padding-inline: 0; }
  .is-collapsed .nav__link { justify-content: center; padding-inline: 0; }
  .is-collapsed .side__user { justify-content: center; }

  .side__nav { flex: 1 1 auto; padding: 12px 12px 20px; }
  .side__group { margin-block-start: 14px; }
  .side__label { padding-inline: 12px; margin-block-end: 6px; font-size: 10.5px; font-weight: 700; letter-spacing: .09em; text-transform: uppercase; color: #7f92c9; }
  .nav { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
  .nav__link { display: flex; align-items: center; gap: 11px; inline-size: 100%; padding: 9px 12px; border: 0; background: none; border-radius: var(--radius-sm); color: #c3cdf0; text-align: start; font-size: 13.5px; font-weight: 500; transition: background .14s, color .14s; }
  .nav__link .icon { inline-size: 17px; block-size: 17px; opacity: .82; }
  .nav__link:hover { background: rgba(255,255,255,.07); color: #fff; }
  .nav__link.is-active { background: rgba(255,255,255,.14); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 #6f92ff; }
  .nav__count { margin-inline-start: auto; font: 700 10.5px/1 var(--sans); background: rgba(255,255,255,.16); color: #eaf0ff; padding: 3px 7px; border-radius: 99px; }
  .nav__count.is-hot { background: #ff5d6c; color: #fff; }

  .side__user { flex: none; display: flex; align-items: center; gap: 10px; margin: 10px 12px; padding: 10px; border-radius: var(--radius-sm); background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.08); }
  .side__avatar { inline-size: 34px; block-size: 34px; border-radius: 9px; flex: none; display: grid; place-items: center; font-weight: 700; color: #fff; background: linear-gradient(135deg, #5b82f5, #7c5cfc); }
  .side__user-meta { min-inline-size: 0; }
  .side__user-meta b { display: block; font-size: 13px; color: #fff; }
  .side__user-meta span { font-size: 11px; color: #93a3d6; }
}

/* ───────────────────────────────  TOPBAR  ─────────────────────────── */
@layer topbar {
  .topbar { block-size: var(--header-h); display: flex; align-items: center; gap: 14px; padding-inline: 20px; background: color-mix(in srgb, var(--surface) 88%, transparent); backdrop-filter: saturate(1.4) blur(8px); border-block-end: 1px solid var(--line); }
  .icon-btn { inline-size: 38px; block-size: 38px; border-radius: 10px; flex: none; display: grid; place-items: center; border: 1px solid transparent; background: none; color: var(--muted); transition: background .14s, color .14s; }
  .icon-btn:hover { background: var(--brand-soft); color: var(--brand); }
  .topbar__title { display: flex; flex-direction: column; line-height: 1.15; min-inline-size: 0; }
  .topbar__crumb { font-size: 11px; color: var(--faint); font-weight: 600; }
  .topbar__title h1 { font-size: 17px; white-space: nowrap; }
  .topbar__spacer { flex: 1 1 auto; }
  .search { display: flex; align-items: center; gap: 8px; min-inline-size: 0; inline-size: min(320px, 34vw); padding: 8px 12px; background: var(--ground); border: 1px solid var(--line); border-radius: 10px; color: var(--faint); }
  .search input { flex: 1; border: 0; background: none; outline: none; font: inherit; font-size: 13px; color: var(--ink); min-inline-size: 0; }
  .topbar__actions { display: flex; align-items: center; gap: 6px; }
  .dot { position: relative; }
  .dot[data-count]::after { content: attr(data-count); position: absolute; inset-block-start: 4px; inset-inline-end: 4px; min-inline-size: 15px; block-size: 15px; padding-inline: 3px; border-radius: 99px; background: var(--bad); color: #fff; font: 700 9px/15px var(--sans); text-align: center; box-shadow: 0 0 0 2px var(--surface); }

  .bp-toggle { display: flex; align-items: center; gap: 9px; padding: 7px 12px 7px 11px; border: 1px solid var(--line-strong); border-radius: 99px; background: var(--surface); color: var(--muted); font-size: 12.5px; font-weight: 600; transition: .16s; }
  .bp-toggle:hover { border-color: var(--bp-line); color: var(--bp-line); }
  .bp-toggle .sw { inline-size: 34px; block-size: 19px; border-radius: 99px; background: var(--line-strong); position: relative; transition: background .18s; }
  .bp-toggle .sw::after { content:""; position:absolute; inset-block-start:2px; inset-inline-start:2px; inline-size:15px; block-size:15px; border-radius:50%; background:#fff; transition: transform .18s; box-shadow:0 1px 2px rgba(0,0,0,.3); }
  body.bp .bp-toggle { border-color: var(--bp-line); color: var(--bp-line); background: color-mix(in srgb, var(--bp-line) 8%, var(--surface)); }
  body.bp .bp-toggle .sw { background: var(--bp-line); }
  body.bp .bp-toggle .sw::after { transform: translateX(15px); }
}

/* ─────────────────────────────  COMPONENTS  ───────────────────────── */
@layer components {
  .card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-sm); transition: box-shadow .16s, transform .16s, border-color .16s; }
  .card--pad { padding: 20px; }
  .card--hover:hover { box-shadow: var(--shadow-md); border-color: var(--line-strong); }
  .sec-head { display: flex; align-items: center; gap: 10px; margin-block-end: 14px; }
  .sec-head h2 { font-size: 15px; }
  .sec-head .sec-more { margin-inline-start: auto; font-size: 12px; color: var(--brand); font-weight: 600; background: none; border: 0; }

  .badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 9px; border-radius: 99px; font-size: 11.5px; font-weight: 700; line-height: 1.6; }
  .badge::before { content:""; inline-size:6px; block-size:6px; border-radius:50%; background: currentColor; opacity:.9; }
  .b-ok{color:var(--ok);background:var(--ok-bg);} .b-warn{color:var(--warn);background:var(--warn-bg);}
  .b-bad{color:var(--bad);background:var(--bad-bg);} .b-info{color:var(--info);background:var(--info-bg);}
  .b-gray{color:var(--muted);background:var(--ground);}
  .badge--plain::before { display:none; }

  .btn { display: inline-flex; align-items: center; gap: 7px; padding: 9px 15px; border-radius: 10px; font-size: 13px; font-weight: 600; border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink); transition: .14s; }
  .btn:hover { border-color: var(--brand); color: var(--brand); }
  .btn--primary { background: var(--brand); border-color: var(--brand); color: #fff; box-shadow: 0 2px 8px rgba(53,99,233,.3); }
  .btn--primary:hover { background: var(--brand-strong); color: #fff; }
  .btn--sm { padding: 6px 11px; font-size: 12px; }

  .cluster { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
  .spacer { flex: 1 1 auto; }
  .segmented { display: inline-flex; background: var(--ground); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
  .segmented button { border: 0; background: none; padding: 6px 12px; border-radius: 7px; font-size: 12.5px; font-weight: 600; color: var(--muted); }
  .segmented button.is-active { background: var(--surface); color: var(--brand); box-shadow: var(--shadow-sm); }

  .avatar-pill { display:inline-flex; align-items:center; gap:7px; }
  .avatar-xs { inline-size:24px; block-size:24px; border-radius:6px; flex:none; display:grid; place-items:center; font-size:11px; font-weight:700; color:#fff; background:linear-gradient(135deg,#5b82f5,#7c5cfc); }
}

/* ────────────────────────────────  VIEWS  ─────────────────────────── */
@layer views {
  .view { display: none; }
  .view.is-active { display: block; animation: fade .22s ease; }
  @keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

  .kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: var(--card-gap); }
  .kpi { padding: 18px 20px; }
  .kpi__label { display:flex; align-items:center; gap:8px; font-size: 12.5px; color: var(--muted); font-weight: 600; }
  .kpi__ico { inline-size:30px; block-size:30px; border-radius:8px; display:grid; place-items:center; color:#fff; }
  .kpi__val { font-size: 27px; font-weight: 800; letter-spacing: -.02em; margin-block: 10px 4px; }
  .kpi__val small { font-size: 15px; font-weight: 700; color: var(--muted); margin-inline-start: 2px; }
  .kpi__sub { font-size: 12px; font-weight: 600; color: var(--faint); }

  .dash-body { container-type: inline-size; }
  .split { display: grid; grid-template-columns: 1.85fr 1fr; gap: var(--card-gap); align-items: start; }
  @container (max-width: 860px) { .split { grid-template-columns: 1fr; } }

  .feed { list-style: none; margin: 0; padding: 0; }
  .feed li { display: flex; gap: 12px; padding: 13px 0; border-block-end: 1px solid var(--line); align-items: flex-start; }
  .feed li:last-child { border: 0; }
  .feed .t { font-weight: 600; font-size: 13.5px; }
  .feed .m { font-size: 12px; color: var(--faint); margin-block-start: 2px; }
  .pin { color: var(--warn); flex: none; margin-block-start: 2px; }

  .cal { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
  .cal span { aspect-ratio: 1; display: grid; place-items: center; font-size: 11.5px; border-radius: 7px; color: var(--muted); }
  .cal .hd { font-weight: 700; color: var(--faint); font-size: 10.5px; }
  .cal .today { background: var(--brand); color: #fff; font-weight: 700; }
  .cal .ev { background: var(--brand-soft); color: var(--brand); font-weight: 700; cursor: default; }

  .table-wrap { overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); }
  table.grid { inline-size: 100%; min-inline-size: 760px; border-collapse: collapse; font-size: 13px; }
  table.grid th { text-align: start; font-size: 11.5px; font-weight: 700; color: var(--faint); text-transform: uppercase; letter-spacing: .04em; padding: 12px 16px; background: var(--surface-2); border-block-end: 1px solid var(--line); white-space: nowrap; position: sticky; inset-block-start: 0; }
  table.grid td { padding: 13px 16px; border-block-end: 1px solid var(--line); white-space: nowrap; }
  table.grid tbody tr:last-child td { border-block-end: 0; }
  table.grid tbody tr:hover { background: var(--surface-2); }
  .cell-strong { font-weight: 600; }
  .tnum { text-align: end; font-weight: 700; }

  .kanban { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(248px, 1fr); gap: 14px; overflow-x: auto; padding-block-end: 10px; align-items: start; }
  .kanban__col { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); display: flex; flex-direction: column; min-block-size: 340px; }
  .kanban__head { display: flex; align-items: center; gap: 8px; padding: 13px 14px; border-block-end: 1px solid var(--line); }
  .kanban__head b { font-size: 13px; }
  .kanban__head .amt { margin-inline-start: auto; font-size: 11.5px; font-weight: 700; color: var(--muted); }
  .kanban__body { padding: 10px; display: flex; flex-direction: column; gap: 9px; overflow-y: auto; }
  .lead { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 12px; box-shadow: var(--shadow-sm); transition: .14s; }
  .lead:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }
  .lead .co { font-weight: 700; font-size: 13px; }
  .lead .pr { font-size: 12px; color: var(--muted); margin-block: 3px 9px; }
  .lead .ft { display: flex; align-items: center; justify-content: space-between; }
  .lead .val { font-weight: 800; font-size: 13px; color: var(--brand); }
  .stage-dot { inline-size:9px; block-size:9px; border-radius:50%; flex:none; }
}

/* ─────────────────────────  STATES (로딩·빈·오류)  ─────────────────── */
@layer states {
  .skl { position: relative; overflow: hidden; background: var(--surface-2); border-radius: 8px; }
  .skl::after { content:""; position:absolute; inset:0; transform: translateX(-100%); background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--ink) 6%, transparent), transparent); animation: shimmer 1.3s infinite; }
  @keyframes shimmer { to { transform: translateX(100%); } }
  .skl-line { block-size: 12px; margin-block: 6px; }
  .kpi.is-loading .kpi__val { color: transparent; }

  .empty { display: grid; place-items: center; text-align: center; gap: 10px; padding: 46px 20px; color: var(--faint); }
  .empty .big { inline-size: 52px; block-size: 52px; border-radius: 15px; display: grid; place-items: center; background: var(--brand-soft); color: var(--brand); }
  .empty code { font: 12px/1.7 var(--mono); background: var(--ground); border: 1px solid var(--line); padding: 2px 7px; border-radius: 6px; color: var(--muted); }
  .error-note { display:flex; gap:10px; align-items:flex-start; padding:12px 14px; border-radius:var(--radius-sm); background:var(--bad-bg); color:var(--bad); font-size:12.5px; font-weight:600; }
}

/* ──────────────────────────  BLUEPRINT OVERLAY  ───────────────────── */
@layer blueprint {
  body.bp [data-bp] { position: relative; outline: 1.4px dashed color-mix(in srgb, var(--bp-line) 75%, transparent); outline-offset: -1.4px; border-radius: 4px; }
  body.bp [data-bp]::before { content: attr(data-bp); position: absolute; inset-block-start: 0; inset-inline-start: 0; z-index: 60; font: 600 10px/1.35 var(--mono); letter-spacing: .01em; white-space: nowrap; background: var(--bp-line); color: var(--bp-ink); padding: 2px 6px; border-end-end-radius: 7px; pointer-events: none; max-inline-size: 100%; overflow: hidden; text-overflow: ellipsis; }
  body.bp .app_shell__aside { outline: 1.4px dashed var(--bp-line); outline-offset: -3px; }
  .bp-hint { display: none; }
  body.bp .bp-hint { display: flex; align-items: flex-start; gap: 12px; margin-block-end: var(--stack-gap); padding: 14px 16px; border-radius: var(--radius); background: color-mix(in srgb, var(--bp-line) 10%, var(--surface)); border: 1px solid color-mix(in srgb, var(--bp-line) 45%, transparent); color: var(--ink); }
  .bp-hint .icon { color: var(--bp-line); flex: none; }
  .bp-hint b { color: color-mix(in srgb, var(--bp-line) 70%, var(--ink)); }
  .bp-hint code { font: 12px var(--mono); color: var(--muted); }
}

/* ────────────────────────────  RESPONSIVE  ────────────────────────── */
@layer responsive {
  .scrim { display: none; }
  @media (max-width: 1024px) {
    .app_shell { grid-template-columns: 0 1fr; }
    .app_shell__aside { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 90; inline-size: var(--sidebar-w); transform: translateX(-100%); transition: transform .24s ease; box-shadow: var(--shadow-lg); }
    .app_shell.nav-open .app_shell__aside { transform: none; }
    .app_shell.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 80; background: rgba(10,16,30,.5); }
    .menu-btn { display: grid !important; }
  }
  @media (min-width: 1025px) { .menu-btn { display: none; } }
  @media (max-width: 640px) { :root { --gutter: 16px; --stack-gap: 16px; } .search { display: none; } .topbar__crumb { display: none; } }
}
