:root {
  --ink: #172026;
  --muted: #647174;
  --line: #d8dfdc;
  --paper: #f3f7f2;
  --panel: #fff;
  --teal: #155e63;
  --teal-2: #0f4549;
  --gold: #c9922e;
  --rose: #bd5260;
  --blue: #3867a8;
  --red-soft: #fff2f3;
  --shadow: 0 14px 34px rgba(19, 35, 38, .11);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Noto Sans TC", "Segoe UI", sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { width: 100%; max-width: 100%; overflow-x: hidden; }
body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background: linear-gradient(135deg, #eef5f0 0%, #f8f5ec 48%, #edf4f8 100%);
  font-size: 16px;
  line-height: 1.5;
}
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: progress; opacity: .7; }
img { max-width: 100%; height: auto; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.eyebrow { margin: 0; color: var(--gold); font-size: 12px; font-weight: 900; text-transform: uppercase; }

.auth-panel, dialog, .sidebar, .topbar, .order-card, .admin-panel, .history-table, .profile-card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.auth-layout {
  display: grid;
  gap: 16px;
  width: min(100%, 560px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 16px;
  align-content: center;
}
.brand-panel {
  min-height: 230px;
  padding: 22px;
  color: #fff;
  background: linear-gradient(rgba(13,54,58,.56), rgba(13,54,58,.35)), url("https://images.unsplash.com/photo-1511512578047-dfb367046420?auto=format&fit=crop&w=1200&q=80") center/cover;
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  align-content: start;
}
.brand-panel h1 { margin: 8px 0 0; max-width: 460px; font-size: clamp(34px, 10vw, 56px); line-height: 1.05; text-shadow: 0 3px 14px rgba(0,0,0,.38); }
.auth-panel { padding: 16px; }
.tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin-bottom: 16px; }
.auth-form, .dialog-form { display: grid; gap: 14px; }

label { display: grid; gap: 7px; color: var(--muted); font-weight: 800; }
input, textarea, select {
  width: 100%;
  min-width: 0;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  background: #fff;
  color: var(--ink);
  outline: none;
  font-size: 16px;
}
input:disabled, select:disabled, textarea:disabled, .readonly-field {
  color: #6f7b82;
  background: #e5eaec !important;
  border-color: #d4dcdf;
  opacity: 1;
  -webkit-text-fill-color: #6f7b82;
}
input[data-readonly-field="true"],
input[data-readonly-field="true"]:disabled {
  color: #5f6d73 !important;
  background: #e1e7ea !important;
  border-color: #c8d2d6 !important;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.72);
  cursor: not-allowed;
  opacity: 1;
  -webkit-text-fill-color: #5f6d73;
}
textarea { resize: vertical; }
.check-row { display: flex; align-items: center; gap: 10px; min-height: 44px; color: var(--ink); }
.check-row input { width: 22px; min-height: 22px; accent-color: var(--teal); }

.tab, .ghost, .nav-btn, .icon-btn, .text-btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 8px;
}
.tab, .ghost, .nav-btn, .primary {
  min-height: 44px;
  padding: 0 14px;
}
.tab.is-active, .nav-btn.is-active { border-color: var(--teal); background: #e8f3f1; color: var(--teal-2); font-weight: 900; }
.primary {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: var(--teal);
  font-weight: 900;
}
.primary.blue { background: var(--blue); }
.danger { color: var(--rose); border-color: #efc4cb; background: #fff5f6; }
.icon-btn { width: 44px; min-height: 44px; padding: 0; }
.text-btn { min-height: 0; padding: 0; border: 0; background: transparent; color: var(--teal); font-weight: 900; text-decoration: underline; }

.app-layout { min-height: 100vh; padding: 12px; display: grid; gap: 12px; }
.sidebar { padding: 14px; display: grid; gap: 14px; align-content: start; }
.sidebar-head { display: flex; align-items: start; justify-content: space-between; gap: 12px; }
.sidebar h2 { margin: 4px 0 0; font-size: 22px; }
#appNav { display: none; gap: 8px; grid-template-columns: 1fr; }
#appNav.is-open { display: grid; }
.nav-btn, .ghost { display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.user-box { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.user-box span { font-weight: 900; }

.content { display: grid; gap: 12px; min-width: 0; }
.topbar { position: sticky; top: 0; z-index: 5; padding: 14px; display: grid; gap: 12px; }
.topbar h2 { margin: 0; font-size: 24px; }
.top-actions, .actions, .dialog-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.view-grid { display: grid; gap: 12px; }

.order-card { display: grid; gap: 12px; padding: 14px; overflow: hidden; }
.card-head { display: flex; justify-content: space-between; align-items: start; gap: 12px; }
.card-head h3 { margin: 6px 0 0; font-size: 20px; word-break: break-word; }
.status { display: inline-flex; min-height: 28px; align-items: center; padding: 0 10px; border-radius: 999px; font-weight: 900; font-size: 13px; }
.status.open { color: #24598f; background: #e8f1fb; }
.status.settling { color: #9c3543; background: var(--red-soft); }
.preview-btn { padding: 0; border: 0; background: transparent; }
.preview { width: 100%; max-height: 260px; object-fit: contain; border-radius: 8px; border: 1px solid var(--line); background: #f8fafb; }
.meta { display: grid; gap: 6px; color: var(--muted); }
.money-row { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.metric { min-width: 0; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: #f9fbfb; }
.metric span { display: block; color: var(--muted); font-size: 13px; }
.metric strong { display: block; font-size: 20px; word-break: break-word; }
.countdown { color: var(--rose); }
.claim-list { display: grid; gap: 8px; padding: 10px; border-radius: 8px; background: #f7faf8; border: 1px solid var(--line); }
.claim-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.claim-row small { display: block; color: var(--muted); }
.selected-list { display: flex; flex-wrap: wrap; gap: 8px; }
.tag { display: inline-flex; align-items: center; gap: 6px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; background: #f7faf8; font-weight: 800; }
.tag button { min-height: 24px; padding: 0 6px; border: 0; border-radius: 6px; color: #fff; background: var(--rose); }
.rarity-blue { color: #1e5fa6; font-weight: 900; }
.rarity-red { color: #b32937; font-weight: 900; }
.rarity-purple { color: #7342b8; font-weight: 900; }

dialog { width: min(720px, calc(100% - 24px)); max-height: calc(100dvh - 24px); padding: 0; }
dialog::backdrop { background: rgba(12, 22, 25, .5); }
.dialog-form, .image-preview-wrap { padding: 16px; }
.dialog-form header, .image-preview-wrap header { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.dialog-form h3, .image-preview-wrap h3 { margin: 0; }
.image-dialog { width: min(960px, calc(100% - 24px)); }
#imagePreview { width: 100%; max-height: 76vh; object-fit: contain; border-radius: 8px; background: #111; }

.history-table, .admin-panel, .profile-card { padding: 14px; overflow-x: auto; }
.filter-bar {
  display: grid;
  gap: 10px;
  grid-column: 1 / -1;
  margin-bottom: 10px;
}
.filter-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.filter-custom {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 180px));
  gap: 8px;
  align-items: end;
}
.filter-custom label {
  min-width: 0;
}
.filter-custom input {
  min-height: 44px;
}
.history-tools { display: flex; gap: 8px; margin-bottom: 10px; }
.history-row { display: grid; grid-template-columns: minmax(140px, 1.4fr) repeat(5, minmax(90px, 1fr)); gap: 8px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); min-width: 720px; }
.history-head { color: var(--muted); font-size: 13px; font-weight: 900; }
.attendance-history-list { display: grid; gap: 8px; }
.attendance-history-item { display: flex; justify-content: space-between; padding: 10px; border: 1px solid var(--line); border-radius: 8px; }
.attendance-history-item.is-paid strong { color: #c73545; }
.profile-data { display: grid; gap: 10px; }
.profile-data div, .member-row, .audit-row, .pending-row { display: grid; gap: 8px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: #f9fbfb; margin-bottom: 8px; }
.profile-data span { color: var(--muted); font-size: 13px; }
.member-row {
  padding: 8px;
  background: #f6faf8;
  border-color: #d7e2df;
}
.member-summary {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr) auto auto;
  gap: 12px;
  align-items: center;
  width: 100%;
  min-height: 64px;
  padding: 10px;
  border: 1px solid #e4ece9;
  border-radius: 8px;
  color: var(--ink);
  background: linear-gradient(180deg, #fff 0%, #f8fbfa 100%);
  text-align: left;
  box-shadow: 0 5px 14px rgba(19, 35, 38, .05);
  transition: border-color .16s ease, box-shadow .16s ease, transform .16s ease;
}
.member-summary:hover {
  border-color: #bfd5d0;
  box-shadow: 0 10px 20px rgba(19, 35, 38, .08);
  transform: translateY(-1px);
}
.member-avatar {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(135deg, var(--teal), #2d7f75);
  font-weight: 1000;
  box-shadow: inset 0 -10px 18px rgba(0,0,0,.12);
}
.member-title {
  display: grid;
  gap: 2px;
  min-width: 0;
}
.member-title strong {
  overflow: hidden;
  color: var(--ink);
  font-size: 16px;
  font-weight: 1000;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.member-title small {
  overflow: hidden;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.role-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 52px;
  min-height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 900;
}
.role-badge.is-admin {
  color: #24598f;
  background: #e6f0fb;
  border: 1px solid #c7dcf4;
}
.role-badge.is-member {
  color: var(--teal-2);
  background: #e7f3f0;
  border: 1px solid #c9e1dc;
}
.member-chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 0 10px;
  border-radius: 8px;
  color: #415157;
  background: #eef3f2;
  font-size: 13px;
  font-weight: 900;
  white-space: nowrap;
}
.member-detail {
  display: grid;
  gap: 10px;
  margin-top: 2px;
  padding: 14px;
  border: 1px solid #e1e9e6;
  border-radius: 8px;
  background: #fff;
}
.member-edit-row { grid-template-columns: 1fr; }
.member-status {
  align-content: start;
  min-height: 48px;
  padding: 10px 12px;
  border-radius: 8px;
  background: #f6faf9;
  border: 1px solid #e2ebe8;
}
.member-status span { display: block; color: var(--muted); font-size: 13px; font-weight: 800; }
.member-status strong { display: block; margin-top: 2px; }
.member-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.pager {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 12px;
}
.pager span { color: var(--muted); font-weight: 900; }

#toastHost { position: fixed; right: 16px; bottom: 16px; z-index: 1000; display: grid; gap: 8px; }
.toast { padding: 12px 14px; border-radius: 8px; color: #fff; background: #162024; box-shadow: var(--shadow); max-width: min(340px, calc(100vw - 32px)); }

@media (min-width: 900px) {
  .auth-layout { grid-template-columns: 1.05fr .95fr; width: min(980px, 100%); }
  .app-layout { grid-template-columns: 280px 1fr; align-items: start; }
  .sidebar { position: sticky; top: 12px; min-height: calc(100vh - 24px); }
  #appNav { display: grid; }
  .nav-toggle { display: none; }
  .topbar { grid-template-columns: 1fr auto; align-items: center; }
  .view-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .member-edit-row { grid-template-columns: repeat(3, minmax(0, 1fr)); align-items: end; }
  .member-actions { align-self: end; }
}

@media (max-width: 560px) {
  .money-row { grid-template-columns: 1fr; }
  .card-head { display: grid; }
  .claim-row { display: grid; }
  .top-actions > * { flex: 1 1 auto; }
  .filter-custom { grid-template-columns: 1fr; }
  .member-summary {
    grid-template-columns: 42px minmax(0, 1fr);
  }
  .role-badge, .member-chevron {
    grid-column: 2;
    justify-self: start;
  }
  .member-detail {
    padding: 12px;
  }
}
