/* 마우스 관리 시스템 공통 스타일 */

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-alt: #f9fafb;
  --border: #e2e5ea;
  --border-strong: #cbd2db;
  --text: #1c2430;
  --text-muted: #6b7684;
  --text-faint: #9aa3af;
  --accent: #2f6f5e;
  --accent-soft: #e6f2ee;
  --accent-strong: #245647;
  --danger: #b3261e;
  --danger-soft: #fce8e6;
  --warn: #8a5a00;
  --warn-soft: #fdf3dd;
  --info: #1b5e9c;
  --info-soft: #e5eff9;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(20, 30, 45, .06), 0 4px 12px rgba(20, 30, 45, .05);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%; /* 사파리에서 가로 보기로 돌릴 때 글자가 커지지 않게 */
  text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  /* -apple-system 을 앞에 두어 macOS·iOS 에서 시스템 글꼴을 쓰고,
     한글은 Apple SD Gothic Neo 로 떨어지게 한다. */
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo",
    "Malgun Gothic", "맑은 고딕", "Segoe UI", system-ui, sans-serif;
  font-size: 14px;
  line-height: 1.55;
  /* 아이폰 노치·홈 인디케이터 영역 */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

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

/* --- 상단바 ------------------------------------------------------------ */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  align-items: center;
  gap: 24px;
  min-height: 56px;
  flex-wrap: wrap;
}

.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 700; }
.brand:hover { text-decoration: none; }
.brand-mark { font-size: 20px; }
.brand-name { white-space: nowrap; }

.nav { display: flex; gap: 2px; flex: 1; flex-wrap: wrap; }
/* 좁은 화면에서는 줄바꿈 대신 가로 스크롤 (모바일 사파리에서 부드럽게 넘어가도록) */
.nav::-webkit-scrollbar { display: none; }

.nav-link {
  padding: 7px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-muted);
  font-weight: 500;
  white-space: nowrap;
}
.nav-link:hover { background: var(--surface-alt); color: var(--text); text-decoration: none; }
.nav-link.is-active { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }

.user-box { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.user-name { font-weight: 600; }
.user-role { color: var(--text-faint); }
.inline { display: inline; }

/* --- 레이아웃 ---------------------------------------------------------- */
.page { max-width: 1400px; margin: 0 auto; padding: 24px 20px 64px; }
.page-centered { max-width: 420px; padding-top: 10vh; }

.page-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}
.page-title { margin: 0; font-size: 22px; font-weight: 700; }
.page-subtitle { margin: 4px 0 0; color: var(--text-muted); font-size: 13px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 20px;
  overflow: hidden;
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-alt);
  flex-wrap: wrap;
}
.card-title { margin: 0; font-size: 15px; font-weight: 650; }
.card-body { padding: 16px; }
.card-body.tight { padding: 0; }

.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }

/* --- 통계 타일 --------------------------------------------------------- */
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat-label { color: var(--text-muted); font-size: 12px; font-weight: 600; letter-spacing: .02em; }
.stat-value { font-size: 26px; font-weight: 700; line-height: 1.2; margin-top: 4px; }
.stat-value small { font-size: 13px; font-weight: 500; color: var(--text-muted); margin-left: 4px; }
.stat-sub { color: var(--text-faint); font-size: 12px; margin-top: 2px; }

/* --- 표 ---------------------------------------------------------------- */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; /* iOS 관성 스크롤 */
  overscroll-behavior-x: contain;
}

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data th, table.data td {
  padding: 9px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  white-space: nowrap;
}
table.data th {
  background: var(--surface-alt);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  position: sticky;
  top: 0;
}
table.data tbody tr:hover { background: var(--surface-alt); }
table.data td.wrap, table.data th.wrap { white-space: normal; min-width: 180px; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data tfoot td { font-weight: 600; background: var(--surface-alt); }

.mono { font-family: "Consolas", "D2Coding", ui-monospace, monospace; }

.empty {
  padding: 36px 16px;
  text-align: center;
  color: var(--text-faint);
}

/* --- 배지 -------------------------------------------------------------- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  background: var(--surface-alt);
  color: var(--text-muted);
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-alive { background: var(--accent-soft); color: var(--accent-strong); border-color: #cfe5dd; }
.badge-danger { background: var(--danger-soft); color: var(--danger); border-color: #f6d3d0; }
.badge-warn { background: var(--warn-soft); color: var(--warn); border-color: #f0e0b8; }
.badge-info { background: var(--info-soft); color: var(--info); border-color: #cfe0f0; }
.badge-male { background: #e8eefc; color: #26429c; border-color: #d2ddf6; }
.badge-female { background: #fcebf2; color: #9c2657; border-color: #f6d4e2; }

/* --- 버튼 -------------------------------------------------------------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  line-height: 1.4;
}
.btn:hover { background: var(--surface-alt); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
.btn-danger { background: var(--surface); border-color: #e4b6b3; color: var(--danger); }
.btn-danger:hover { background: var(--danger-soft); }
.btn-ghost { border-color: transparent; background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface-alt); color: var(--text); }
.btn-sm { padding: 4px 10px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* --- 폼 ---------------------------------------------------------------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 14px 16px; }
.form-grid .span-2 { grid-column: span 2; }
.form-grid .span-all { grid-column: 1 / -1; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > label { font-size: 12.5px; font-weight: 600; color: var(--text-muted); }
.field .hint { font-size: 11.5px; color: var(--text-faint); }
.required::after { content: " *"; color: var(--danger); }

input[type="text"], input[type="password"], input[type="date"], input[type="number"],
input[type="search"], select, textarea {
  width: 100%;
  padding: 7px 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 13.5px;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent-soft);
  border-color: var(--accent);
}
textarea { min-height: 72px; resize: vertical; }

.check-row { display: flex; align-items: center; gap: 7px; font-size: 13px; }
.check-row > input { flex: 0 0 auto; }
/* 설명이 길어 줄바꿈되면 체크박스를 첫 줄에 맞춘다 */
.check-row:has(> span) { align-items: flex-start; }
.check-row:has(> span) > input { margin-top: 3px; }
.check-grid { display: flex; flex-wrap: wrap; gap: 10px 16px; }
.check-grid label { display: flex; align-items: center; gap: 6px; font-size: 13px; }

.form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface-alt);
  flex-wrap: wrap;
}

/* --- 필터 바 ----------------------------------------------------------- */
.filter-bar {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  align-items: flex-end;
}
.filter-bar .field { min-width: 130px; flex: 0 1 auto; }
.filter-bar .field.grow { flex: 1 1 200px; }

/* --- 플래시 ------------------------------------------------------------ */
.flash-stack { display: flex; flex-direction: column; gap: 8px; margin-bottom: 16px; }
.flash {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-size: 13.5px;
  font-weight: 500;
}
.flash-success { background: var(--accent-soft); border-color: #cfe5dd; color: var(--accent-strong); }
.flash-error { background: var(--danger-soft); border-color: #f6d3d0; color: var(--danger); }
.flash-warning { background: var(--warn-soft); border-color: #f0e0b8; color: var(--warn); }
.flash-info { background: var(--info-soft); border-color: #cfe0f0; color: var(--info); }

/* --- 페이지네이션 ------------------------------------------------------ */
.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-muted);
  flex-wrap: wrap;
}
.pager-links { display: flex; gap: 4px; flex-wrap: wrap; }
.pager-links a, .pager-links span {
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
}
.pager-links .is-current { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }

/* --- 상세 정보 목록 ---------------------------------------------------- */
.detail-list { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px 20px; }
.detail-item { display: flex; flex-direction: column; gap: 2px; }
.detail-label { font-size: 12px; color: var(--text-muted); font-weight: 600; }
.detail-value { font-size: 14px; }

/* --- 케이지 그리드 ----------------------------------------------------- */
.cage-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 12px; }
.cage-tile {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.cage-tile:hover { border-color: var(--accent); text-decoration: none; }
.cage-tile.is-over { border-color: #e4b6b3; background: var(--danger-soft); }
.cage-tile.is-empty { background: var(--surface-alt); color: var(--text-faint); }
.cage-code { font-weight: 700; font-size: 15px; }
.cage-meta { font-size: 12px; color: var(--text-muted); }
.cage-occupants { display: flex; flex-wrap: wrap; gap: 4px; }

/* --- 타임라인 ---------------------------------------------------------- */
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li {
  position: relative;
  padding: 0 0 14px 20px;
  border-left: 2px solid var(--border);
}
.timeline li:last-child { border-left-color: transparent; padding-bottom: 0; }
.timeline li::before {
  content: "";
  position: absolute;
  left: -5px;
  top: 5px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border-strong);
}
.timeline li.is-terminal::before { background: var(--danger); }
.timeline-date { font-size: 12px; color: var(--text-muted); font-weight: 600; }
.timeline-body { font-size: 13.5px; }

/* --- 알림 목록 --------------------------------------------------------- */
.alert-list { list-style: none; margin: 0; padding: 0; }
.alert-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 11px 16px;
  border-bottom: 1px solid var(--border);
}
.alert-item:last-child { border-bottom: none; }
.alert-body { flex: 1; }
.alert-title { font-weight: 600; font-size: 13.5px; }
.alert-detail { font-size: 12.5px; color: var(--text-muted); }

/* --- 막대 차트(순수 CSS) ----------------------------------------------- */
.bar-chart { display: flex; flex-direction: column; gap: 8px; }
.bar-row { display: grid; grid-template-columns: 86px 1fr 46px; align-items: center; gap: 10px; font-size: 12.5px; }
.bar-track { background: var(--surface-alt); border-radius: 999px; height: 16px; overflow: hidden; border: 1px solid var(--border); }
.bar-fill { height: 100%; background: var(--accent); border-radius: 999px; min-width: 2px; }
.bar-fill.alt { background: var(--info); }
.bar-value { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-muted); }

/* --- 로그인 ------------------------------------------------------------ */
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 28px; }
.login-title { margin: 0 0 4px; font-size: 20px; text-align: center; }
.login-sub { margin: 0 0 20px; text-align: center; color: var(--text-muted); font-size: 13px; }
.login-card .field { margin-bottom: 14px; }
.login-card .btn { width: 100%; justify-content: center; }

/* --- 펼침 폼(details) -------------------------------------------------- */
details { display: inline-block; vertical-align: top; position: relative; }
details > summary { list-style: none; cursor: pointer; }
details > summary::-webkit-details-marker { display: none; }
details[open] > summary { box-shadow: inset 0 0 0 1px var(--accent); }
/* 표 안(.table-wrap 는 overflow 가 걸려 있음)에서도 잘리지 않도록 제자리에서 펼친다. */
details[open] > form {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px;
}
details[open] > form label { display: flex; flex-direction: column; gap: 3px; }

.detail-list .span-all { grid-column: 1 / -1; }

/* --- 케이지 종류 탭 ---------------------------------------------------- */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; margin-bottom: 16px; }
.tab {
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 13px;
}
.tab:hover { text-decoration: none; color: var(--text); }
.tab.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tab .count { font-weight: 500; opacity: .8; margin-left: 4px; }

.type-breeding { border-left: 4px solid #b0417a; }
.type-stock { border-left: 4px solid var(--accent); }
.type-experiment { border-left: 4px solid var(--info); }
.type-banking { border-left: 4px solid var(--warn); }
.type-quarantine { border-left: 4px solid var(--text-faint); }

/* --- 기타 -------------------------------------------------------------- */
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.small { font-size: 12.5px; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.stack { display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > select, .row > input:not([type="checkbox"]) { width: auto; max-width: 100%; }
.divider { height: 1px; background: var(--border); margin: 16px 0; }
.tag-list { display: flex; gap: 4px; flex-wrap: wrap; }

/* --- 모바일 · 태블릿 ---------------------------------------------------- */
@media (max-width: 900px) {
  .topbar-inner { align-items: center; padding: 8px 14px; gap: 10px; }
  .brand-name { display: none; }          /* 아이콘만 남겨 자리를 아낀다 */
  .brand-mark { font-size: 24px; }
  .user-box { margin-left: auto; }
  .user-role { display: none; }

  /* 메뉴는 줄바꿈 대신 가로로 넘긴다 */
  .nav {
    order: 3;
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 2px;
  }
  .nav-link { flex: 0 0 auto; }

  .page { padding: 14px 12px 56px; }
  .page-head { gap: 10px; }
  .page-title { font-size: 19px; }
  .page-actions { width: 100%; }
  .page-actions .btn { flex: 1 1 auto; justify-content: center; }

  .card-head { padding: 12px 14px; }
  .card-body { padding: 14px; }
  table.data th, table.data td { padding: 8px 10px; }

  /* 한 줄짜리 조작 영역은 모바일에서 위아래로 쌓는다 */
  .card-head > .row, .card-head > form.row { width: 100%; }
  .row > select, .row > input:not([type="checkbox"]) { flex: 1 1 140px; }
  .filter-bar .field, .filter-bar .field.grow { flex: 1 1 100%; min-width: 0; }
  .filter-bar .row { width: 100%; }
  .filter-bar .row .btn { flex: 1 1 auto; justify-content: center; }

  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2, .form-grid .span-all { grid-column: 1 / -1; }
  .detail-list { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }

  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .grid-4 { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .stat-value { font-size: 22px; }

  .cage-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .bar-row { grid-template-columns: 74px 1fr 40px; }

  /* 손가락으로 누르기 좋은 크기 */
  .btn { padding: 9px 14px; }
  .btn-sm { padding: 7px 11px; }
  .nav-link { padding: 9px 12px; }
  .check-row { min-height: 32px; }
  table.data input[type="checkbox"] { width: 20px; height: 20px; }

  /* 표 안의 좁은 칸에서 펼쳐지면 입력칸이 너무 작아진다.
     모바일에서는 화면 아래에 시트처럼 띄운다. */
  details[open] > form {
    position: fixed;
    left: 10px;
    right: 10px;
    bottom: calc(10px + env(safe-area-inset-bottom));
    z-index: 50;
    width: auto;
    min-width: 0 !important;
    max-height: 72vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 -6px 28px rgba(20, 30, 45, .22);
  }
  details[open] > form::before {
    content: "닫으려면 위 버튼을 다시 누르세요";
    display: block;
    font-size: 11.5px;
    color: var(--text-faint);
    margin-bottom: 8px;
  }

  /* 좁은 화면에서는 덜 중요한 열을 숨긴다 (상세 화면에서 볼 수 있다) */
  .hide-sm { display: none !important; }

  /* 표를 가로로 밀어도 개체 번호는 왼쪽에 붙어 있게 한다 */
  table.data.sticky-id th:nth-child(1),
  table.data.sticky-id td:nth-child(1) {
    position: sticky;
    left: 0;
    z-index: 2;
    width: 34px;
    min-width: 34px;
    padding-left: 8px;
    padding-right: 2px;
    background: var(--surface);
  }
  table.data.sticky-id th:nth-child(2),
  table.data.sticky-id td:nth-child(2) {
    position: sticky;
    left: 34px;
    z-index: 2;
    background: var(--surface);
    box-shadow: 1px 0 0 var(--border);
  }
  table.data.sticky-id thead th:nth-child(1),
  table.data.sticky-id thead th:nth-child(2) {
    z-index: 3;
    background: var(--surface-alt);
  }

  .form-actions { padding: 12px 14px; }
  .form-actions .btn { flex: 1 1 auto; justify-content: center; }
}

/* iOS 사파리는 글자 크기가 16px 미만인 입력칸을 누르면 화면을 확대한다. */
@media (max-width: 900px) {
  input[type="text"], input[type="password"], input[type="date"], input[type="number"],
  input[type="search"], select, textarea {
    font-size: 16px;
    padding: 9px 10px;
  }
}

@media (max-width: 480px) {
  .page-subtitle { font-size: 12.5px; }
  .detail-list { grid-template-columns: 1fr 1fr; }
  .cage-grid { grid-template-columns: 1fr 1fr; }
}
