/* ===============================================================
   공용 테마 / 기본 컴포넌트 스타일

   SPA(React)와 정적 HTML 업무 화면이 함께 사용하는 단일 기준 파일이다.
   FastAPI 가 /static/theme.css 로 서빙한다. (main.py 의 public_asset 라우트)

   로드 방법
     정적 페이지: <link rel="stylesheet" href="/static/theme.css">
     SPA        : index.html 에서 동일하게 로드 (개발 중에는 Vite 프록시가 전달)
   =============================================================== */

/* --- 디자인 토큰 ------------------------------------------------ */
:root {
  /* 색 */
  --bg: #f5f6f8;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1c1f24;
  --text-muted: #6b7280;

  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-text: #ffffff;

  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --success: #16a34a;
  --success-bg: #f0fdf4;
  --warn: #b45309;
  --warn-bg: #fffbeb;

  /* 치수 */
  --radius: 8px;
  --radius-sm: 6px;
  --gap: 16px;

  /* 그림자 */
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 2px 8px rgba(0, 0, 0, 0.04);

  /* 글꼴 — 한글 가독성을 위해 Pretendard 계열을 우선 시도한다 */
  --font: 'Pretendard', -apple-system, BlinkMacSystemFont, 'Malgun Gothic',
    '맑은 고딕', system-ui, sans-serif;
  --font-mono: 'D2Coding', 'Consolas', 'Courier New', monospace;
}

/* 다크 모드: OS 설정을 따라간다 */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181d;
    --surface: #1e2127;
    --border: #2d3138;
    --text: #e6e8ea;
    --text-muted: #9aa1ab;

    --primary: #3b82f6;
    --primary-hover: #60a5fa;

    --danger: #f87171;
    --danger-bg: #2a1a1a;
    --success: #4ade80;
    --success-bg: #16251a;
    --warn: #fbbf24;
    --warn-bg: #2a2110;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 2px 8px rgba(0, 0, 0, 0.3);
  }
}

/* --- 기본 리셋 -------------------------------------------------- */
* {
  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.6;
  -webkit-font-smoothing: antialiased;
}

a {
  color: var(--primary);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

/* --- 텍스트 ----------------------------------------------------- */
.text-muted {
  color: var(--text-muted);
}
.text-right {
  text-align: right;
}

/* 금액처럼 자릿수를 맞춰 읽어야 하는 값 */
.num {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

/* --- 레이아웃 --------------------------------------------------- */
.page {
  min-height: 100vh;
}

/* 화면 정중앙 배치 (로그인/회원가입) */
.page-center {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.content {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: var(--gap);
}

/* --- 상단바 ----------------------------------------------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 15px;
}

/* --- 카드 ------------------------------------------------------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 16px;
  font-size: 15px;
  font-weight: 600;
}

/* --- 버튼 ------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn:hover:not(:disabled) {
  background: var(--bg);
  text-decoration: none;
}
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--primary-text);
}
.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-muted);
}
.btn-ghost:hover:not(:disabled) {
  background: var(--bg);
  color: var(--text);
}

.btn-block {
  width: 100%;
}

/* --- 폼 --------------------------------------------------------- */
.field {
  display: block;
  margin-bottom: 14px;
}

.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-muted);
}

input[type='text'],
input[type='password'],
input[type='email'],
input[type='number'],
input[type='date'],
input[type='tel'],
select,
textarea {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

input:disabled,
select:disabled,
textarea:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* 아이콘이 들어간 입력란 */
.input-wrap {
  position: relative;
}
.input-wrap .input-icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
.input-wrap input {
  padding-left: 34px;
}

/* 입력란 오른쪽 안쪽 버튼 (비밀번호 보기/숨기기) */
.input-wrap input.has-trailing-btn {
  padding-right: 38px;
}
.input-wrap .input-btn {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.input-wrap .input-btn:hover {
  color: var(--text);
  background: var(--bg);
}
.input-wrap .input-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

/* 체크박스 한 줄 (자동 로그인 등) */
.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 14px;
  cursor: pointer;
  user-select: none;
}
.check-row input[type='checkbox'] {
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--primary);
  cursor: pointer;
}
.check-help {
  margin: 0 0 14px 24px;
  font-size: 12px;
  color: var(--text-muted);
}

/* --- 알림 ------------------------------------------------------- */
.alert {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 14px;
}

.alert-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
}

.alert-warn {
  background: var(--warn-bg);
  color: var(--warn);
  border: 1px solid var(--warn);
}

/* 표 안에서 쓰는 작은 버튼 */
.btn-sm {
  padding: 3px 9px;
  font-size: 12px;
  min-height: 0;
}

.alert-ok {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid var(--success);
}

/* --- 태그(칩) --------------------------------------------------- */
.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.chip {
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  font-size: 12px;
  font-family: var(--font-mono);
  color: var(--text-muted);
}

/* --- 정의 목록 -------------------------------------------------- */
.info-list {
  display: grid;
  grid-template-columns: minmax(80px, auto) 1fr;
  gap: 8px 16px;
  margin: 0;
}

.info-list dt {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 13px;
}

.info-list dd {
  margin: 0;
  word-break: break-all;
}

/* --- 표 --------------------------------------------------------- */
.table-wrap {
  overflow-x: auto;
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

table.data th,
table.data td {
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  white-space: nowrap;
}

table.data th {
  background: var(--bg);
  font-weight: 600;
  color: var(--text-muted);
}

table.data tbody tr:hover {
  background: var(--bg);
}

/* --- 내비게이션 (nav-menu.js 가 그린다) -------------------------- */
.nav {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}

.nav a {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-size: 13px;
  white-space: nowrap;
}

.nav a:hover {
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
}

.nav a.active {
  background: var(--primary);
  color: var(--primary-text);
}

.nav-spacer {
  flex: 1;
}

/* --- 로딩 스피너 ------------------------------------------------ */
.spin {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* 애니메이션을 줄이도록 설정한 사용자 존중 */
@media (prefers-reduced-motion: reduce) {
  .spin {
    animation: none;
  }
}

/* ===============================================================
   관리 화면용 추가 컴포넌트
   (users.html, audit.html 이 공유한다)
   =============================================================== */

/* --- 페이지 제목 ------------------------------------------------ */
.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.page-head h1 {
  margin: 0 0 2px;
  font-size: 20px;
  font-weight: 600;
}

.page-head p {
  margin: 0;
  font-size: 13px;
}

/* --- 검색 툴바 -------------------------------------------------- */
.toolbar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  padding: 14px 16px;
  margin-bottom: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.toolbar .field {
  margin-bottom: 0;
}

/* 툴바 안의 입력칸은 내용에 맞춰 좁게 */
.toolbar input[type='text'],
.toolbar input[type='date'],
.toolbar select {
  width: auto;
  min-width: 140px;
}

.toolbar-spacer {
  flex: 1;
}

/* --- 배지 ------------------------------------------------------- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  border: 1px solid transparent;
}

.badge-role-superadmin {
  background: #ede9fe;
  color: #6d28d9;
  border-color: #ddd6fe;
}
.badge-role-admin {
  background: #dbeafe;
  color: #1d4ed8;
  border-color: #bfdbfe;
}
.badge-role-viewer_mgt {
  background: #d1fae5;
  color: #047857;
  border-color: #a7f3d0;
}
.badge-role-viewer_admin {
  background: #f1f5f9;
  color: #475569;
  border-color: #e2e8f0;
}

.badge-on {
  background: var(--success-bg);
  color: var(--success);
  border-color: var(--success);
}
.badge-off {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: var(--danger);
}

@media (prefers-color-scheme: dark) {
  .badge-role-superadmin { background: #2e1065; color: #c4b5fd; border-color: #4c1d95; }
  .badge-role-admin      { background: #172554; color: #93c5fd; border-color: #1e3a8a; }
  .badge-role-viewer_mgt { background: #052e16; color: #86efac; border-color: #14532d; }
  .badge-role-viewer_admin { background: #1e2127; color: #9aa1ab; border-color: #2d3138; }
}

/* --- 모달 ------------------------------------------------------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 1000;
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  width: 100%;
  max-width: 560px;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  line-height: 0;
  border-radius: var(--radius-sm);
}
.modal-close:hover {
  background: var(--bg);
  color: var(--text);
}

/* --- 권한 체크박스 목록 ----------------------------------------- */
.perm-group {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 10px;
}

.perm-group-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-bottom: 8px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 14px;
}

.perm-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 5px 0;
}

.perm-item input[type='checkbox'] {
  width: auto;
  margin: 3px 0 0;
  flex-shrink: 0;
}

.perm-item-text {
  flex: 1;
  min-width: 0;
}

.perm-item-label {
  font-size: 13px;
}

.perm-item-key {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 6px;
}

.perm-item-desc {
  font-size: 12px;
  color: var(--text-muted);
}

/* 상위키가 켜져 하위가 자동 포함된 상태 */
.perm-item.covered {
  opacity: 0.55;
}

/* --- 페이지네이션 ----------------------------------------------- */
.pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
  font-size: 13px;
}

/* --- 통계 타일 -------------------------------------------------- */
.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat {
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.stat-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.stat-value {
  font-size: 22px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.stat-value.warn {
  color: var(--danger);
}

/* --- 빈 상태 ---------------------------------------------------- */
.empty {
  padding: 36px 16px;
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
}

/* --- 상세 펼침 (감사로그 detail) -------------------------------- */
details.detail-cell summary {
  cursor: pointer;
  color: var(--primary);
  font-size: 12px;
}

details.detail-cell pre {
  margin: 6px 0 0;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-all;
  max-width: 460px;
}
