:root {
  --primary: #0d9488;
  --primary-dark: #0f766e;
  --primary-light: #e6f5f3;
  --accent: #f59e0b;
  --bg: #f6f7f9;
  --card: #ffffff;
  --text: #1f2733;
  --muted: #6b7480;
  --border: #e5e7eb;
  --danger: #dc2626;
  --danger-bg: #fdeaea;
  --ok: #16a34a;
  --ok-bg: #e7f6ec;
  --warn: #b7791f;
  --warn-bg: #fdf3e0;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "PingFang TC", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.55;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary-dark); text-decoration: none; }

/* ---- 版面 ---- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; background: #1e293b; color: #cbd5e1; flex-shrink: 0;
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sidebar .brand {
  padding: 18px 16px 14px; color: #fff; font-size: 17px; font-weight: 700;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}
.sidebar .brand small { display: block; font-size: 11px; font-weight: 400; color: #94a3b8; margin-top: 2px; }
.nav { flex: 1; padding: 8px 0; }
.nav a { display: block; padding: 10px 18px; color: #cbd5e1; font-size: 14px; border-left: 3px solid transparent; }
.nav a:hover { background: rgba(255,255,255,0.05); }
.nav a.active { background: rgba(255,255,255,0.08); color: #fff; border-left-color: var(--primary); }
.nav .nav-group { padding: 12px 18px 4px; font-size: 11px; color: #7c8797; letter-spacing: 1px; }
.nav .nav-badge {
  float: right; background: var(--danger); color: #fff; border-radius: 10px;
  font-size: 11px; padding: 0 6px; line-height: 17px; font-weight: 700;
}
.sidebar .user-box { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,0.1); font-size: 13px; }
.sidebar .user-box .name { color: #fff; font-weight: 600; }
.sidebar .user-box button {
  margin-top: 8px; width: 100%; padding: 6px; background: transparent; color: #cbd5e1;
  border: 1px solid rgba(255,255,255,0.28); border-radius: 6px; cursor: pointer; font-size: 13px;
}
.sidebar .user-box button:hover { background: rgba(255,255,255,0.07); }

.main { flex: 1; min-width: 0; padding: 20px 24px 60px; }
.page-title { font-size: 21px; font-weight: 700; margin-bottom: 4px; }
.page-sub { color: var(--muted); font-size: 13px; margin-bottom: 18px; }

/* 手機版頂欄 */
.topbar { display: none; }
@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar { position: fixed; left: -240px; z-index: 60; transition: left 0.2s; height: 100vh; }
  .sidebar.open { left: 0; box-shadow: 4px 0 20px rgba(0,0,0,0.3); }
  .topbar {
    display: flex; align-items: center; gap: 12px; background: #1e293b; color: #fff;
    padding: 12px 16px; position: sticky; top: 0; z-index: 50;
  }
  .topbar .menu-btn { background: none; border: 1px solid rgba(255,255,255,0.4); color: #fff; border-radius: 6px; padding: 4px 10px; cursor: pointer; }
  .main { padding: 16px 14px 60px; }
  .backdrop { display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 55; }
  .backdrop.show { display: block; }
}

/* ---- 卡片、統計 ---- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; margin-bottom: 16px; }
.card h3 { font-size: 15px; margin-bottom: 12px; }
.card h3 .sub { font-weight: 400; color: var(--muted); font-size: 12.5px; margin-left: 6px; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; cursor: default; }
.stat .num { font-size: 26px; font-weight: 700; color: var(--primary-dark); }
.stat .num.warn { color: var(--warn); }
.stat .num.danger { color: var(--danger); }
.stat .num.ok { color: var(--ok); }
.stat .label { font-size: 12px; color: var(--muted); margin-top: 2px; }
.stat.clickable { cursor: pointer; }
.stat.clickable:hover { outline: 2px solid var(--primary-light); }

/* ---- 表格 ---- */
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; background: var(--card); border-radius: var(--radius); }
table.list th, table.list td { padding: 9px 10px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
table.list td.wrap { white-space: normal; min-width: 160px; }
table.list th { background: #f1f3f5; color: var(--muted); font-weight: 600; font-size: 12.5px; position: sticky; top: 0; }
table.list tr:hover td { background: #f7f9fb; }
table.list tr.row-danger td { background: var(--danger-bg); }
table.list tr.row-warn td { background: var(--warn-bg); }

/* ---- 按鈕、標籤 ---- */
.btn {
  display: inline-block; padding: 7px 14px; border-radius: 7px; border: 1px solid transparent;
  background: var(--primary); color: #fff; cursor: pointer; font-size: 14px; line-height: 1.4;
}
.btn:hover { background: var(--primary-dark); }
.btn.secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn.secondary:hover { background: #f1f3f5; }
.btn.danger { background: var(--danger); }
.btn.warn { background: var(--accent); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.tiny { padding: 2px 8px; font-size: 12px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.tag { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 12px; background: #eef1f4; color: var(--muted); }
.tag.ok { background: var(--ok-bg); color: var(--ok); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.danger { background: var(--danger-bg); color: var(--danger); }
.tag.primary { background: var(--primary-light); color: var(--primary-dark); }

/* ---- 工具列 ---- */
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.toolbar .spacer { flex: 1; }
.toolbar input, .toolbar select {
  padding: 7px 10px; border: 1px solid var(--border); border-radius: 7px; background: #fff; font-size: 14px;
}

/* ---- 表單 ---- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.form-row { display: flex; flex-direction: column; gap: 4px; }
.form-row.full { grid-column: 1 / -1; }
.form-row label { font-size: 12.5px; color: var(--muted); }
.form-row input, .form-row select, .form-row textarea {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px; background: #fff; width: 100%;
}
.form-row textarea { min-height: 72px; resize: vertical; }
.form-row input:focus, .form-row select:focus, .form-row textarea:focus { outline: 2px solid var(--primary-light); border-color: var(--primary); }
.form-check { display: flex; align-items: center; gap: 7px; font-size: 14px; padding-top: 6px; }
.form-check input { width: auto; }

/* ---- Modal ---- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(15,23,42,0.5); z-index: 100;
  display: flex; align-items: flex-start; justify-content: center; padding: 5vh 14px; overflow-y: auto;
}
.modal { background: #fff; border-radius: 12px; width: 640px; max-width: 100%; box-shadow: 0 10px 40px rgba(0,0,0,0.25); }
.modal.wide { width: 900px; }
.modal-head { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 16px; flex: 1; }
.modal-head .close { background: none; border: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 18px; max-height: 74vh; overflow-y: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }

/* ---- Toast ---- */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: #1f2733; color: #fff; padding: 10px 20px; border-radius: 8px; font-size: 14px;
  opacity: 0; transition: opacity 0.25s; pointer-events: none; max-width: 90vw;
}
#toast.show { opacity: 1; }
#toast.error { background: var(--danger); }

/* ---- 登入頁 ---- */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; background: linear-gradient(150deg, #0d9488, #1e293b); padding: 16px; }
.login-card { background: #fff; border-radius: 14px; padding: 34px 30px; width: 380px; max-width: 100%; box-shadow: 0 16px 50px rgba(0,0,0,0.28); }
.login-card h1 { font-size: 22px; margin-bottom: 2px; color: var(--primary-dark); }
.login-card .sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.login-card .form-row { margin-bottom: 14px; }
.login-card .btn { width: 100%; padding: 10px; font-size: 15px; margin-top: 6px; }
.login-err { color: var(--danger); font-size: 13px; min-height: 18px; margin-top: 8px; }

/* ---- 寵物卡片（候診、住院看板） ---- */
.pet-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.pet-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 13px 14px;
  border-left: 4px solid var(--border);
}
.pet-card.in { border-left-color: var(--ok); }
.pet-card.waiting { border-left-color: var(--accent); }
.pet-card.progress { border-left-color: var(--primary); }
.pet-card.done { border-left-color: #94a3b8; }
.pet-card.empty-cage { border-left-color: var(--border); background: #fafbfc; border: 1px dashed var(--border); }
.pet-card .pet-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.pet-avatar {
  width: 44px; height: 44px; border-radius: 50%; background: var(--primary-light); color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 18px;
  background-size: cover; background-position: center; flex-shrink: 0;
}
.pet-card .pet-name { font-weight: 700; }
.pet-card .pet-meta { font-size: 12px; color: var(--muted); }
.pet-card .pet-status { font-size: 12.5px; color: var(--muted); margin-bottom: 8px; min-height: 32px; }
.pet-card .pet-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.queue-no {
  background: var(--primary); color: #fff; border-radius: 7px; padding: 1px 8px;
  font-size: 13px; font-weight: 700; margin-left: auto;
}

/* 警示標記（過敏、性格、慢性病）— 看診前必看 */
.alert-chips { display: flex; flex-wrap: wrap; gap: 5px; margin: 6px 0; }
.alert-chip { font-size: 11.5px; padding: 1px 8px; border-radius: 12px; background: var(--danger-bg); color: var(--danger); font-weight: 600; }
.alert-chip.info { background: var(--warn-bg); color: var(--warn); }

/* ---- 時間軸（照護紀錄、病歷） ---- */
.timeline { list-style: none; }
.timeline li { display: flex; gap: 12px; padding: 9px 0; border-bottom: 1px dashed var(--border); }
.timeline .tl-time { color: var(--muted); font-size: 13px; width: 92px; flex-shrink: 0; padding-top: 1px; }
.timeline .tl-type { font-weight: 600; font-size: 13.5px; color: var(--primary-dark); width: 66px; flex-shrink: 0; }
.timeline .tl-body { flex: 1; font-size: 14px; min-width: 0; }
.timeline .tl-body img { max-width: 180px; border-radius: 8px; display: block; margin-top: 4px; }
.timeline .tl-del { color: var(--danger); font-size: 12px; cursor: pointer; }

/* ---- SOAP 病歷 ---- */
.soap-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
@media (max-width: 760px) { .soap-grid { grid-template-columns: 1fr; } }
.soap-box { border: 1px solid var(--border); border-radius: 9px; padding: 10px 12px; background: #fff; }
.soap-box .soap-label { font-size: 11.5px; font-weight: 700; color: var(--primary-dark); letter-spacing: 1px; margin-bottom: 5px; }
.soap-box textarea { width: 100%; border: none; outline: none; resize: vertical; min-height: 92px; font-size: 14px; }
.vitals { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; margin-bottom: 14px; }
.vital-box { border: 1px solid var(--border); border-radius: 9px; padding: 8px 10px; }
.vital-box label { font-size: 11.5px; color: var(--muted); display: block; }
.vital-box input { width: 100%; border: none; outline: none; font-size: 16px; font-weight: 600; padding: 2px 0; }

/* ---- 召回中心 ---- */
.recall-tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.recall-tabs button {
  border: 1px solid var(--border); background: #fff; border-radius: 20px; padding: 5px 14px;
  cursor: pointer; font-size: 13.5px; color: var(--muted);
}
.recall-tabs button.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.due-badge { font-weight: 700; }
.due-badge.overdue { color: var(--danger); }
.due-badge.due { color: var(--warn); }
.due-badge.soon { color: var(--muted); }

/* ---- 籠位看板 ---- */
.cage-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.cage { border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: #fff; min-height: 104px; }
.cage.occupied { border-color: var(--primary); box-shadow: inset 0 0 0 1px var(--primary-light); cursor: pointer; }
.cage.occupied.boarding { border-color: var(--accent); }
.cage .cage-name { font-size: 12px; color: var(--muted); font-weight: 700; }
.cage .cage-pet { font-weight: 700; margin-top: 4px; }
.cage .cage-note { font-size: 12px; color: var(--muted); }
.cage .cage-free { color: var(--muted); font-size: 13px; padding-top: 14px; text-align: center; }

/* ---- 明細表（處方、帳單） ---- */
.item-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.item-table th, .item-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); text-align: left; }
.item-table th { color: var(--muted); font-size: 12px; font-weight: 600; }
.item-table td input, .item-table td select { padding: 5px 7px; border: 1px solid var(--border); border-radius: 6px; width: 100%; }
.item-table .num { text-align: right; }
.item-total { display: flex; justify-content: flex-end; gap: 18px; padding: 10px 8px 0; font-size: 15px; }
.item-total b { font-size: 18px; color: var(--primary-dark); }

.empty { text-align: center; color: var(--muted); padding: 36px 0; font-size: 14px; }
.section-tabs { display: flex; gap: 4px; margin-bottom: 16px; border-bottom: 2px solid var(--border); overflow-x: auto; }
.section-tabs button {
  background: none; border: none; padding: 9px 16px; cursor: pointer; font-size: 14.5px; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -2px; white-space: nowrap;
}
.section-tabs button.active { color: var(--primary-dark); border-bottom-color: var(--primary); font-weight: 600; }

.detail-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px 18px; font-size: 14px; }
.detail-grid .dg-label { color: var(--muted); font-size: 12px; }

/* ---- 主內容頂部工具列（全域搜尋＋通知鈴） ---- */
.main-head { display: flex; align-items: center; gap: 10px; margin-bottom: 16px; }
.search-box { position: relative; flex: 1; max-width: 400px; }
.search-box input { width: 100%; padding: 8px 12px 8px 32px; border: 1px solid var(--border); border-radius: 9px; background: #fff; font-size: 14px; }
.search-box input:focus { outline: 2px solid var(--primary-light); border-color: var(--primary); }
.search-box .ico { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 14px; }
.search-drop {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; background: #fff;
  border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 8px 26px rgba(15,23,42,.14);
  z-index: 70; max-height: 340px; overflow-y: auto;
}
.search-drop .grp { padding: 6px 12px; font-size: 11.5px; color: var(--muted); background: #f6f7f9; font-weight: 700; }
.search-drop .item { padding: 9px 12px; cursor: pointer; font-size: 14px; border-bottom: 1px solid var(--border); }
.search-drop .item:last-child { border-bottom: 0; }
.search-drop .item:hover { background: var(--primary-light); }
.search-drop .item small { color: var(--muted); font-size: 12px; }
.notif-btn { position: relative; background: #fff; border: 1px solid var(--border); border-radius: 9px; padding: 7px 12px; cursor: pointer; font-size: 15px; line-height: 1; }
.notif-btn:hover { background: #f1f3f5; }
.notif-btn .dot {
  position: absolute; top: -5px; right: -5px; min-width: 18px; height: 18px;
  background: var(--danger); color: #fff; border-radius: 9px; font-size: 11px; line-height: 18px;
  text-align: center; padding: 0 4px; font-weight: 700;
}
.notif-item { padding: 10px 2px; border-bottom: 1px dashed var(--border); font-size: 14px; cursor: pointer; }
.notif-item:last-child { border-bottom: 0; }
.notif-item.unread { background: var(--primary-light); border-radius: 6px; padding-left: 8px; padding-right: 8px; }
.notif-item .t { font-weight: 700; }
.notif-item .b { color: var(--muted); font-size: 13px; }
.notif-item .d { color: var(--muted); font-size: 11.5px; margin-top: 2px; }

/* ---- 體重趨勢迷你圖 ---- */
.spark { display: flex; align-items: flex-end; gap: 3px; height: 54px; }
.spark i { flex: 1; background: var(--primary-light); border-radius: 2px 2px 0 0; min-height: 3px; position: relative; }
.spark i:last-child { background: var(--primary); }

/* ---- 財務報表 ---- */
.fin-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.fin-card { background: #fff; border: 1px solid var(--border); border-radius: 11px; padding: 14px 16px; }
.fin-card .n { font-size: 22px; font-weight: 800; color: var(--primary-dark); }
.fin-card .n.ok { color: var(--ok); } .fin-card .n.warn { color: var(--warn); } .fin-card .n.danger { color: var(--danger); }
.fin-card .l { font-size: 12px; color: var(--muted); margin-top: 2px; }
.bar { height: 9px; background: #eef1f4; border-radius: 5px; overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--ok); }

@media print {
  .sidebar, .topbar, .toolbar, .btn, .main-head, .section-tabs { display: none !important; }
  .main { padding: 0; }
  .modal-mask { position: static; background: none; padding: 0; }
  .modal { box-shadow: none; width: 100%; }
  .modal-head .close, .modal-foot { display: none !important; }
}

/* ================= 飼主端（手機版） ================= */
.fam-wrap { max-width: 640px; margin: 0 auto; padding-bottom: 76px; background: var(--bg); min-height: 100vh; }
.fam-head {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark)); color: #fff;
  padding: 18px 18px 22px; position: sticky; top: 0; z-index: 20;
}
.fam-head .t { font-size: 18px; font-weight: 700; }
.fam-head .s { font-size: 12.5px; opacity: 0.9; margin-top: 2px; }
.fam-body { padding: 14px; }
.fam-nav {
  position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--border);
  display: flex; z-index: 30; max-width: 640px; margin: 0 auto; padding-bottom: env(safe-area-inset-bottom);
}
.fam-nav button {
  flex: 1; background: none; border: none; padding: 9px 0 8px; cursor: pointer; color: var(--muted);
  font-size: 11.5px; position: relative;
}
.fam-nav button .ico { display: block; font-size: 19px; line-height: 1.25; }
.fam-nav button.active { color: var(--primary-dark); font-weight: 600; }
.fam-nav .nav-badge {
  position: absolute; top: 4px; right: 50%; margin-right: -20px; min-width: 8px; height: 8px;
  background: #dc2626; border-radius: 8px;
}
.fam-nav .nav-badge.num {
  min-width: 15px; height: 15px; font-size: 10px; color: #fff; line-height: 15px;
  text-align: center; padding: 0 3px; margin-right: -22px;
}
.fam-card { background: #fff; border-radius: 12px; box-shadow: var(--shadow); padding: 14px 16px; margin-bottom: 12px; }
.fam-card h4 { font-size: 14.5px; margin-bottom: 10px; }
.fam-pet {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: #fff;
  border-radius: 12px; box-shadow: var(--shadow); margin-bottom: 10px; cursor: pointer;
}
.fam-pet .pet-avatar { width: 52px; height: 52px; font-size: 21px; }
.fam-pet .n { font-weight: 700; font-size: 15.5px; }
.fam-pet .m { font-size: 12.5px; color: var(--muted); }
.fam-remind {
  display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--warn-bg); margin-bottom: 8px; font-size: 13.5px;
}
.fam-remind.overdue { background: var(--danger-bg); }
.fam-remind .d { margin-left: auto; font-weight: 700; white-space: nowrap; }
.fam-remind.overdue .d { color: var(--danger); }
.fam-remind .d { color: var(--warn); }
.fam-visit { border-left: 3px solid var(--primary-light); padding: 2px 0 10px 12px; margin-left: 4px; }
.fam-visit .dt { font-size: 12.5px; color: var(--muted); }
.fam-visit .dx { font-weight: 700; margin: 2px 0 4px; }
.fam-empty { text-align: center; color: var(--muted); padding: 40px 12px; font-size: 14px; }
