:root {
  --primary: #1976d2;
  --primary-dark: #115293;
  --bg: #f5f7fa;
  --surface: #fff;
  --border: #e0e4eb;
  --text: #2c3e50;
  --text-light: #6b7280;
  --success: #16a34a;
  --danger: #dc2626;
  --warning: #d97706;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-hover: 0 4px 12px rgba(0,0,0,0.1);
  --radius: 8px;
}

* { box-sizing: border-box; }
html, body {
  margin: 0; padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Microsoft JhengHei", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

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

/* Layout */
.app {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}
.sidebar {
  background: #1e293b;
  color: #cbd5e1;
  padding: 16px 0;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.sidebar .logo {
  padding: 0 20px 16px;
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  border-bottom: 1px solid #334155;
}
.sidebar .logo small {
  display: block; font-size: 11px; color: #94a3b8; font-weight: 400; margin-top: 2px;
}
.sidebar nav { padding: 12px 0; }
.sidebar nav a {
  display: flex; align-items: center;
  gap: 8px;
  padding: 10px 20px;
  color: #cbd5e1;
  border-left: 3px solid transparent;
  font-size: 14px;
}
.sidebar nav a:hover { background: #334155; color: #fff; text-decoration: none; }
.sidebar nav a.active {
  background: #334155;
  color: #fff;
  border-left-color: var(--primary);
}
.sidebar .footer {
  position: absolute; bottom: 0; left: 0; right: 0;
  padding: 12px 20px;
  border-top: 1px solid #334155;
  font-size: 12px;
  color: #94a3b8;
}
.sidebar .footer button {
  background: transparent; border: 1px solid #475569; color: #cbd5e1;
  padding: 4px 10px; border-radius: 4px; cursor: pointer; font-size: 12px;
}
.sidebar .footer button:hover { background: #334155; }

.main { padding: 24px 32px; }
.page-header {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 20px;
}
.page-header h1 { margin: 0; font-size: 22px; font-weight: 600; }
.page-header .subtitle { color: var(--text-light); font-size: 13px; margin-top: 4px; }
.page-header .actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* Cards */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 16px;
}
.card-title {
  font-size: 14px; font-weight: 600;
  margin: 0 0 12px;
  color: var(--text);
  display: flex; justify-content: space-between; align-items: center;
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
}
.stat-card .label {
  font-size: 12px; color: var(--text-light);
  margin-bottom: 6px;
}
.stat-card .value {
  font-size: 26px; font-weight: 700; color: var(--text);
}
.stat-card .delta {
  font-size: 12px; margin-top: 4px;
}
.stat-card .delta.up { color: var(--danger); }
.stat-card .delta.down { color: var(--success); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px; cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;   /* 按鈕文字永遠一行，不被 flex 擠成逐字直排 */
  flex-shrink: 0;        /* 在 .actions flex 列裡不被壓縮 */
}
.btn:hover { background: #f9fafb; }
.btn-primary {
  background: var(--primary); border-color: var(--primary); color: #fff;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger {
  background: var(--danger); border-color: var(--danger); color: #fff;
}
.btn-danger:hover { opacity: 0.9; }
.btn-sm { padding: 3px 8px; font-size: 12px; }

/* Form */
.form-group { margin-bottom: 12px; }
.form-row {
  display: flex; gap: 10px; margin-bottom: 12px;
  flex-wrap: wrap;
}
.form-row > * { flex: 1; min-width: 100px; }
label {
  display: block; font-size: 12px; color: var(--text-light);
  margin-bottom: 4px; font-weight: 500;
}
input[type="text"], input[type="number"], input[type="password"],
input[type="date"], input[type="month"], select, textarea {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 13px;
  font-family: inherit;
  background: #fff;
}
input:focus, select:focus, textarea:focus {
  outline: none; border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(25, 118, 210, 0.15);
}

/* Table */
.table-wrap {
  overflow: auto;
  max-height: 70vh;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  background: var(--surface);
}
table {
  width: 100%; border-collapse: collapse;
  font-size: 13px;
}
thead th {
  background: #f8fafc;
  border-bottom: 1px solid var(--border);
  padding: 10px 12px;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 2;
}
thead th.freeze-col {
  left: 0; z-index: 3;
}
tbody td {
  padding: 8px 12px;
  border-bottom: 1px solid #f1f5f9;
  white-space: nowrap;
}
tbody td.freeze-col {
  position: sticky; left: 0; background: var(--surface);
  z-index: 1;
}
tbody tr:hover td { background: #f9fafb; }
tbody tr:hover td.freeze-col { background: #f3f4f6; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.text-right { text-align: right; }
.text-center { text-align: center; }

/* Tags */
.tag {
  display: inline-block; padding: 2px 8px;
  border-radius: 12px;
  font-size: 11px;
  background: #e5e7eb; color: #374151;
}
.tag-blue { background: #dbeafe; color: #1e40af; }
.tag-green { background: #dcfce7; color: #166534; }
.tag-amber { background: #fef3c7; color: #92400e; }
.tag-red { background: #fee2e2; color: #991b1b; }

/* Modal */
.modal-mask {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal {
  background: #fff;
  border-radius: var(--radius);
  max-width: 520px;
  width: 90%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1);
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-header .close {
  background: none; border: none; font-size: 22px;
  cursor: pointer; color: var(--text-light);
}
.modal-body { padding: 20px; }
.modal-footer {
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 8px;
}

/* Toast */
.toast {
  position: fixed; top: 20px; right: 20px;
  background: #1f2937; color: #fff;
  padding: 10px 16px; border-radius: 6px;
  font-size: 13px;
  z-index: 200;
  box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1);
  animation: slideIn 0.2s ease;
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
@keyframes slideIn {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Login page */
.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.login-card {
  background: #fff;
  padding: 32px;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0,0,0,0.15);
  width: 90%;
  max-width: 380px;
}
.login-card h1 {
  margin: 0 0 4px;
  font-size: 22px;
  text-align: center;
}
.login-card .subtitle {
  text-align: center;
  color: var(--text-light);
  margin-bottom: 24px;
  font-size: 13px;
}
.login-card .err {
  color: var(--danger); font-size: 13px;
  margin-top: 8px; text-align: center;
}

/* Responsive */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: relative; height: auto;
  }
  .sidebar .footer { position: relative; }
  .main { padding: 16px; }
  .stat-grid { grid-template-columns: 1fr 1fr; }
}

/* Empty state */
.empty {
  text-align: center;
  padding: 60px 20px;
  color: var(--text-light);
}

/* Viewer role: hide admin-only controls (edit/delete buttons, save buttons, etc.) */
body.role-viewer .admin-only { display: none !important; }
body.role-viewer .admin-only-col { display: none !important; }
/* Inputs that should look readonly for viewer */
body.role-viewer .admin-only-input {
  pointer-events: none;
  background: transparent !important;
  border-color: transparent !important;
  color: var(--text);
}

/* Inline edit */
.inline-edit {
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
}
.inline-edit:hover { background: #f3f4f6; }
