/* ============================================================
   대한전선 SG 지사 트래커 — main.css
   orra.sg 토큰 시스템 이식 + KPMG 블루 팔레트 + SPA 셸(사이드바/탑바/탭바)
   외부 폰트/CDN 의존 없음. 라이트/다크 3방식.
   구성: 토큰 → 리셋 → 레이아웃 → 컴포넌트 → 폼 → 유틸 → 반응형
   ============================================================ */

/* ── 1. 토큰 (라이트 기본) ────────────────────────────────── */
:root {
  --bg:            #f5f6f8;
  --surface:       #ffffff;
  --surface-2:     #eef1f5;
  --border:        #e0e4ea;
  --border-strong: #c7cdd6;

  --text:          #14181f;
  --text-muted:    #5c636e;
  --text-faint:    #868d98;

  /* KPMG 블루 (브랜드 accent) — 상태색과 분리 */
  --accent:        #00338d;   /* KPMG Blue */
  --accent-hover:  #002a73;
  --accent-soft:   #e5ebf5;   /* Blue 8% 틴트 */
  --on-accent:     #ffffff;
  --link:          #0091da;   /* Light blue — 본문 링크 */

  /* 상태색 (관제 심각도 전용, 브랜드와 미충돌) */
  --ok:            #1a7f52;  --ok-soft:     #e3f4ec;
  --warn:          #9a6300;  --warn-soft:   #fdf1dc;
  --danger:        #b3261e;  --danger-soft: #fce9e7;
  --info:          #0091da;  --info-soft:   #e2f2fb;

  /* 카테고리 tone (사이드바 아이콘·카드 악센트 전용) */
  --cat-fin:   #00338d;
  --cat-sales: #005eb8;
  --cat-ops:   #00a3a1;
  --cat-hr:    #483698;
  --cat-gov:   #470a68;
  --cat-hq:    #c724b1;

  --radius:        11px;
  --radius-sm:     7px;
  --shadow:        0 1px 2px rgba(16,22,34,.06), 0 4px 14px rgba(16,22,34,.05);

  --sidebar-w:     248px;
  --topbar-h:      56px;
  --tabbar-h:      58px;

  --font: ui-sans-serif, -apple-system, "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "D2Coding", monospace;
}

/* 다크 — 시스템 (사용자가 light 명시 안 한 경우) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:            #12151a;
    --surface:       #1a1e25;
    --surface-2:     #232830;
    --border:        #2d333d;
    --border-strong: #3d444f;

    --text:          #e8ebef;
    --text-muted:    #a3aab5;
    --text-faint:    #7c838e;

    --accent:        #4c8de0;   /* 다크 배경 대비 위해 밝게 */
    --accent-hover:  #6aa3e8;
    --accent-soft:   #16233b;
    --on-accent:     #0b0f16;
    --link:          #46b4e8;

    --ok:      #62c795;  --ok-soft:     #16301f;
    --warn:    #e0b055;  --warn-soft:   #33270f;
    --danger:  #f08a83;  --danger-soft: #3a1a18;
    --info:    #46b4e8;  --info-soft:   #10273a;

    --cat-fin:#4c8de0; --cat-sales:#3e9be0; --cat-ops:#33c2c0;
    --cat-hr:#8e7bd8;  --cat-gov:#a277c8;   --cat-hq:#e05fc8;

    --shadow: 0 1px 2px rgba(0,0,0,.32), 0 4px 14px rgba(0,0,0,.26);
  }
}

/* 다크 — 명시 토글 */
:root[data-theme="dark"] {
  --bg:            #12151a;
  --surface:       #1a1e25;
  --surface-2:     #232830;
  --border:        #2d333d;
  --border-strong: #3d444f;

  --text:          #e8ebef;
  --text-muted:    #a3aab5;
  --text-faint:    #7c838e;

  --accent:        #4c8de0;
  --accent-hover:  #6aa3e8;
  --accent-soft:   #16233b;
  --on-accent:     #0b0f16;
  --link:          #46b4e8;

  --ok:      #62c795;  --ok-soft:     #16301f;
  --warn:    #e0b055;  --warn-soft:   #33270f;
  --danger:  #f08a83;  --danger-soft: #3a1a18;
  --info:    #46b4e8;  --info-soft:   #10273a;

  --cat-fin:#4c8de0; --cat-sales:#3e9be0; --cat-ops:#33c2c0;
  --cat-hr:#8e7bd8;  --cat-gov:#a277c8;   --cat-hq:#e05fc8;

  --shadow: 0 1px 2px rgba(0,0,0,.32), 0 4px 14px rgba(0,0,0,.26);
}

/* ── 2. 리셋 ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
h1,h2,h3,h4,p { margin: 0; }
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--accent); color: var(--on-accent);
  padding: 8px 14px; border-radius: 0 0 var(--radius-sm) 0;
}
.skip-link:focus { left: 0; }

/* ── 3. 레이아웃 ──────────────────────────────────────────── */
.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  display: flex; align-items: center; gap: 10px;
  height: var(--topbar-h);
  padding: 0 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.brand-mark {
  display: grid; place-items: center;
  width: 28px; height: 28px;
  border-radius: var(--radius-sm);
  background: var(--accent); color: var(--on-accent);
  font-weight: 800; font-size: 14px;
}
.brand-name { font-weight: 680; letter-spacing: .01em; line-height: 1.1; }
.brand-name small { display: block; font-size: 11px; font-weight: 500; color: var(--text-muted); }

.nav { flex: 1; padding: 12px 10px; }
.nav-group { margin-bottom: 6px; }
.nav-label {
  display: flex; align-items: center; gap: 7px;
  padding: 9px 8px 4px;
  font-size: 11px; font-weight: 700; letter-spacing: .05em;
  text-transform: uppercase; color: var(--text-faint);
}
.nav-label .dot { width: 8px; height: 8px; border-radius: 2px; background: var(--cat, var(--accent)); }
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 7px 10px 7px 22px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-weight: 500;
  cursor: pointer;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.nav-item[aria-current="page"] { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.nav-item .en { margin-left: auto; font-size: 10px; color: var(--text-faint); }

.sidebar-foot {
  padding: 10px; border-top: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 4px; flex: 0 0 auto;
}

.main-col { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.topbar-title { font-size: 15px; font-weight: 680; }
.topbar-title small { font-weight: 500; color: var(--text-muted); margin-left: 8px; font-size: 12px; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.content {
  padding: 20px;
  display: flex; flex-direction: column; gap: 16px;
  max-width: 1320px; width: 100%;
}
.content:focus { outline: none; }

/* ── 4. 컴포넌트 ──────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card-head {
  display: flex; align-items: center; gap: 12px;
  padding: 13px 16px;
  border-bottom: 1px solid var(--border);
}
.card-title { font-size: 14px; font-weight: 680; }
.card-tools { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.card-body { padding: 16px; }
.card-foot { padding: 10px 16px; border-top: 1px solid var(--border); background: var(--surface-2); }

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  border-left: 3px solid var(--stat-accent, var(--accent));
}
.stat-label { font-size: 12px; font-weight: 650; color: var(--text-muted); }
.stat-value { font-size: 25px; font-weight: 720; line-height: 1.2; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.stat-sub { font-size: 12px; }

/* 표 */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; white-space: nowrap; }
.table th, .table td { padding: 9px 16px; text-align: left; border-bottom: 1px solid var(--border); }
.table thead th { background: var(--surface-2); font-size: 12px; font-weight: 650; color: var(--text-muted); position: sticky; top: 0; }
.table th[data-sort] { cursor: pointer; user-select: none; }
.table th[data-sort]:hover { color: var(--text); }
.table th[data-sort]::after { content: "↕"; margin-left: 6px; opacity: .3; font-size: 10px; }
.table th[aria-sort="ascending"]::after { content: "↑"; opacity: 1; }
.table th[aria-sort="descending"]::after { content: "↓"; opacity: 1; }
.table tbody tr:hover { background: var(--surface-2); }
.table tbody tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; }
.empty-row td { padding: 24px 16px; text-align: center; }

/* 카드 리스트 (모바일 표 대체) */
.card-list { display: none; flex-direction: column; }
.record-card {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.record-card:last-child { border-bottom: 0; }
.record-card:hover { background: var(--surface-2); }
.record-card .rc-main { min-width: 0; flex: 1; }
.record-card .rc-title { font-weight: 640; }
.record-card .rc-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.record-card .rc-chevron { color: var(--text-faint); }

/* 배지 */
.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 650; background: var(--surface-2); color: var(--text-muted); }
.badge-ok { background: var(--ok-soft); color: var(--ok); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-danger { background: var(--danger-soft); color: var(--danger); }
.badge-info { background: var(--info-soft); color: var(--info); }

/* 리스크 피드 (관제) */
.risk-list { display: flex; flex-direction: column; }
.risk-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; border-bottom: 1px solid var(--border); cursor: pointer;
}
.risk-item:last-child { border-bottom: 0; }
.risk-item:hover { background: var(--surface-2); }
.risk-sev { width: 8px; height: 8px; border-radius: 999px; flex: 0 0 auto; }
.risk-sev.danger { background: var(--danger); }
.risk-sev.warn { background: var(--warn); }
.risk-sev.info { background: var(--info); }
.risk-main { flex: 1; min-width: 0; }
.risk-tag { font-size: 11px; font-weight: 650; color: var(--text-faint); }
.risk-msg { font-weight: 550; }

/* 버튼 / 입력 */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 13px; min-height: 36px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font-weight: 550; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn-sm { padding: 4px 9px; font-size: 12px; min-height: 30px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }

.input, .select, .textarea {
  width: 100%;
  padding: 8px 10px; min-height: 38px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text); font: inherit;
}
.input::placeholder { color: var(--text-faint); }
.input-sm { padding: 4px 8px; min-height: 30px; font-size: 12px; width: auto; min-width: 160px; }

/* 폼 */
.form { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.full { grid-column: 1 / -1; }
.field label { font-size: 12px; font-weight: 640; color: var(--text-muted); }
.field .help { font-size: 11px; color: var(--text-faint); }
.field.derived .input { background: var(--surface-2); color: var(--text-muted); }
.form-bar {
  display: flex; gap: 8px; justify-content: flex-end;
  padding: 14px 16px; border-top: 1px solid var(--border); background: var(--surface-2);
}

/* 모듈 서브내비 (형제 모듈 칩) */
.subnav { display: flex; gap: 7px; flex-wrap: wrap; }
.chip {
  padding: 5px 11px; border-radius: 999px; font-size: 12px; font-weight: 550;
  background: var(--surface); border: 1px solid var(--border); color: var(--text-muted); cursor: pointer;
}
.chip:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.chip[aria-current="page"] { background: var(--accent-soft); color: var(--accent); border-color: transparent; font-weight: 650; }

/* 페이지 헤더 (뷰 상단) */
.page-head { display: flex; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.page-head h1 { font-size: 20px; font-weight: 720; letter-spacing: -.01em; }
.page-head .page-sub { color: var(--text-muted); font-size: 13px; margin-top: 2px; }
.page-head .page-actions { margin-left: auto; display: flex; gap: 8px; }

.placeholder {
  padding: 40px 20px; text-align: center; color: var(--text-muted);
  border: 1px dashed var(--border-strong); border-radius: var(--radius); background: var(--surface);
}
.placeholder .ph-icon { font-size: 30px; opacity: .5; }

/* 탭바 (모바일) */
.tabbar { display: none; }

/* 테마 버튼 */
.theme-btn { display: inline-flex; align-items: center; gap: 6px; }

/* ── 5. 유틸 ──────────────────────────────────────────────── */
.muted { color: var(--text-muted); }
.xs { font-size: 12px; }
.mono { font-family: var(--mono); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; font-size: 13px; }
.kv dt { color: var(--text-faint); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.row-gap { display: flex; flex-direction: column; gap: 16px; }

/* ── 6. 반응형 ────────────────────────────────────────────── */
@media (max-width: 860px) {
  .sidebar { display: none; }
  .content { padding: 14px; padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); }
  .form { grid-template-columns: 1fr; }

  /* 표 숨기고 카드 리스트 노출 */
  .table-wrap.responsive { display: none; }
  .card-list.responsive { display: flex; }

  .tabbar {
    display: flex;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: var(--surface); border-top: 1px solid var(--border);
    overflow-x: auto;
  }
  .tab {
    flex: 1 0 auto; min-width: 60px;
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
    padding: 6px 8px; color: var(--text-muted); font-size: 11px; font-weight: 550; cursor: pointer;
    text-decoration: none;
  }
  .tab .tab-icon { font-size: 17px; line-height: 1; }
  .tab[aria-current="page"] { color: var(--accent); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
