:root {
  --primary: #2563eb;
  --primary-light: #dbeafe;
  --success: #16a34a;
  --danger: #dc2626;
  --warn: #d97706;
  --bg: #f8fafc;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.1), 0 1px 2px rgba(0,0,0,.06);
  --nav-h: 60px;
  --notice-header-bg: #1e3a5f;
  --evening: #7c3aed;
}
* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 16px; }

/* Layout */
#app { display: flex; flex-direction: column; height: 100%; }
#header { background: var(--primary); color: #fff; padding: 12px 16px 10px; display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 100; }
#unsaved-banner { display: none; position: sticky; top: 52px; z-index: 99; background: #f59e0b; color: #fff; font-size: 14px; font-weight: 700; text-align: center; padding: 10px 16px; letter-spacing: 0.3px; box-shadow: 0 3px 10px rgba(245,158,11,.5); }
#unsaved-banner.visible { display: block; }
#header h1 { font-size: 18px; font-weight: 700; }
#header .subtitle { font-size: 13px; opacity: .8; }
#content { flex: 1; overflow-y: auto; padding: 12px 12px calc(var(--nav-h) + 68px + env(safe-area-inset-bottom)); }
#bottom-nav { position: fixed; bottom: 0; left: 0; right: 0; height: calc(var(--nav-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom); background: var(--card); border-top: 1px solid var(--border); display: flex; z-index: 100; }
.save-footer { position: fixed; bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); left: 0; right: 0; z-index: 90; display: flex; gap: 8px; padding: 8px 12px; background: var(--bg); border-top: 1px solid var(--border); }
.save-footer .btn { flex: 1; }
.save-footer .admin-content { flex: 0 0 auto; width: auto; padding: 14px 16px; }
.nav-btn { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border: none; background: none; cursor: pointer; color: var(--muted); font-size: 11px; transition: color .15s; padding: 4px; }
.nav-btn.active { color: var(--primary); }
.nav-btn svg { width: 22px; height: 22px; }

/* Tabs within pages */
.view { display: none; animation: fadeIn .15s; }
.view.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/* Cards */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); margin-bottom: 12px; overflow: hidden; }
.card-header { padding: 12px 16px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.card-header h2 { font-size: 15px; font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 8px; }
.card-body { padding: 12px 16px; }

/* Date input with inline weekday */
.date-input-wrap { display: flex; align-items: center; border: 1px solid var(--border); border-radius: 8px; background: var(--card); transition: border-color .15s; overflow: hidden; }
.date-input-wrap:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.date-input-wrap input[type=date] { flex: 1; border: none; background: transparent; box-shadow: none; padding: 12px 6px 12px 12px; }
.date-input-wrap input[type=date]:focus { outline: none; box-shadow: none; border-color: transparent; }
#entry-weekday { font-size: 15px; font-weight: 600; color: var(--text); padding-right: 12px; white-space: nowrap; }

/* Form Elements */
.form-row { margin-bottom: 12px; }
.form-row label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
input[type=text], input[type=number], input[type=date], select, textarea {
  width: 100%; padding: 12px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 16px; color: var(--text); background: var(--card); appearance: none; -webkit-appearance: none;
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
input[type=number] { -moz-appearance: textfield; }
input[type=number]::-webkit-outer-spin-button, input[type=number]::-webkit-inner-spin-button { display: none; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 12px 20px; border-radius: 8px; font-size: 15px; font-weight: 600; cursor: pointer; border: none; transition: all .15s; width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:active { background: #1d4ed8; transform: scale(.98); }
.btn-success { background: var(--success); color: #fff; }
.btn-success:active { background: #15803d; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-outline { background: transparent; color: var(--primary); border: 1.5px solid var(--primary); }
.btn-ghost { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-sm { padding: 6px 12px; font-size: 13px; width: auto; border-radius: 6px; }
.btn-icon { width: 36px; height: 36px; padding: 0; border-radius: 8px; }

/* Input row for expenses */
.expense-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.expense-row:last-child { border-bottom: none; }
.expense-info { flex: 1; min-width: 0; }
.expense-info .item-name { font-size: 14px; font-weight: 600; }
.expense-info .item-note { font-size: 12px; color: var(--muted); }
.expense-amount { font-size: 15px; font-weight: 700; color: var(--text); white-space: nowrap; }
.expense-actions { display: flex; gap: 4px; }

/* Add expense form */
.add-form { display: flex; flex-direction: column; gap: 8px; padding-top: 8px; }
.add-form-row { display: flex; gap: 8px; }
.add-form-row > * { flex: 1; }

/* Revenue inputs side by side */
.rev-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Summary box */
.summary-box { background: var(--primary-light); border-radius: 8px; padding: 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.summary-item .label { font-size: 12px; color: var(--primary); font-weight: 600; }
.summary-item .value { font-size: 18px; font-weight: 800; color: var(--primary); }

/* Badge */
.badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.badge-blue { background: var(--primary-light); color: var(--primary); }
.badge-green { background: #dcfce7; color: var(--success); }
.badge-red { background: #fee2e2; color: var(--danger); }
.badge-purple { background: #ede9fe; color: var(--evening); }

/* ── Role-based visibility ── */
/* morning-content: 早班專屬，晚班admin與user看不到 */
#app.role-evening .morning-content,
#app.role-user    .morning-content { display: none !important; }
/* evening-content: 晚班專屬，早班admin與user看不到 */
#app.role-morning .evening-content,
#app.role-user    .evening-content { display: none !important; }
/* staff-content: 非user可見（admin/morning/evening） */
#app.role-user .staff-content { display: none !important; }
/* admin-content: 僅admin可見 */
#app.role-morning .admin-content,
#app.role-evening .admin-content,
#app.role-user    .admin-content  { display: none !important; }

/* History list */
.history-item { padding: 12px 16px; display: flex; align-items: center; cursor: pointer; border-bottom: 1px solid var(--border); transition: background .1s; }
.history-item:active { background: var(--bg); }
.history-item .date-col { flex: 1; }
.history-item .date-main { font-size: 15px; font-weight: 700; }
.history-item .date-sub { font-size: 12px; color: var(--muted); }
.history-item .amount-col { text-align: right; }
.history-item .amount-main { font-size: 15px; font-weight: 700; color: var(--primary); }
.history-item .amount-sub { font-size: 12px; color: var(--muted); }
.chevron { color: var(--muted); margin-left: 8px; }

/* Month selector */
.month-selector { display: flex; align-items: center; gap: 10px; justify-content: center; padding: 8px 0; }
.month-selector button, .month-nav-btn { width: 40px; height: 40px; border-radius: 50%; border: 1.5px solid var(--border); background: var(--card); font-size: 18px; cursor: pointer; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.month-selector .month-label { font-size: 18px; font-weight: 800; color: var(--text); min-width: 120px; text-align: center; }

/* Report table */
.report-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.report-table th { background: #f1f5f9; padding: 8px; text-align: left; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); }
.report-table td { padding: 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.report-table tr:last-child td { border-bottom: none; }
.report-table .total-row td { font-weight: 700; background: #f8fafc; }
.text-right { text-align: right; }
.text-mono { font-variant-numeric: tabular-nums; }

/* Net profit display */
.profit-calc { }
.profit-row { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.profit-row:last-child { border-bottom: none; }
.profit-row .label { color: var(--muted); }
.profit-row .minus { color: var(--danger); }
.profit-row .result { color: var(--success); font-size: 20px; font-weight: 900; }

/* Toast */
#toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%) translateY(20px); background: #1e293b; color: #fff; padding: 10px 20px; border-radius: 8px; font-size: 14px; font-weight: 600; opacity: 0; transition: all .25s; pointer-events: none; z-index: 999; white-space: normal; max-width: min(320px, 90vw); text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* Modal */
.modal-overlay, .bottom-sheet-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 200; display: flex; align-items: flex-end; animation: fadeIn .15s; }
.bottom-sheet { background: var(--card); border-radius: 16px 16px 0 0; padding: 20px; width: 100%; max-height: 85vh; overflow-y: auto; animation: slideUp .2s; }
.modal-sheet { background: var(--card); border-radius: 16px 16px 0 0; padding: 20px; width: 100%; max-height: 85vh; overflow-y: auto; animation: slideUp .2s; }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal-handle { width: 40px; height: 4px; background: var(--border); border-radius: 2px; margin: 0 auto 16px; }
.modal-title { font-size: 17px; font-weight: 800; margin-bottom: 16px; }

/* Print styles */
@media print {
  /* ── 通用 ── */
  #bottom-nav, #header, #unsaved-banner, .no-print { display: none !important; }
  #content { padding: 0; margin: 0; }
  body { font-size: 11px; background: #fff; margin: 0; }
  * { -webkit-print-color-adjust: exact !important; print-color-adjust: exact !important; }
  .card { box-shadow: none; border: 1px solid #ddd; break-inside: avoid; border-radius: 6px; }

  /* ══ 列印月報（預設）══ */
  .view { display: none !important; }
  #view-monthly { display: block !important; padding: 0; }

  .print-only { display: block !important; }
  #monthly-print-title {
    font-size: 15px; font-weight: 800; text-align: center;
    padding: 8px 0 12px; letter-spacing: 1px;
    border-bottom: 2px solid #333; margin-bottom: 12px;
  }
  #monthly-content {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    align-items: start;
  }
  #print-card-shared          { grid-column: 1; grid-row: 1 / 4; }
  #print-card-rev             { grid-column: 2; grid-row: 1; }
  #print-card-morning-profit  { grid-column: 2; grid-row: 2; }
  #print-card-evening-profit  { grid-column: 2; grid-row: 3; }
  #print-card-daily { display: none !important; } /* 列印版不含每日明細 */
  /* 列印時強制顯示早晚班淨利卡片，避免 role CSS 的 display:none 破壞版面 */
  #print-card-morning-profit,
  #print-card-evening-profit { display: block !important; }
  #monthly-content .morning-content:not(#print-card-morning-profit),
  #monthly-content .evening-content:not(#print-card-evening-profit) { display: none !important; }
  .card-header { padding: 6px 10px; }
  .card-header h2 { font-size: 12px; }
  .card-body { padding: 6px 10px; }
  .report-table td, .report-table th { padding: 3px 6px; font-size: 10.5px; }
  .profit-row { padding: 3px 0; font-size: 10.5px; }
  .summary-box { gap: 4px; padding: 6px; }
  .summary-item .value { font-size: 16px; }
  .result { font-size: 18px !important; }

  /* ══ 列印通知單 ══ */
  body.printing-notice #view-monthly,
  body.printing-notice #view-input,
  body.printing-notice #view-history,
  body.printing-notice #view-settings { display: none !important; }
  body.printing-notice #view-notice { display: block !important; padding: 0; }
  body.printing-notice .seg-control,
  body.printing-notice #notice-monthly-selector,
  body.printing-notice #notice-year-selector { display: none !important; }
  body.printing-notice #notice-content { display: block !important; }
}

/* Divider */
.divider { height: 1px; background: var(--border); margin: 12px 0; }

/* Empty state */
.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty svg { width: 48px; height: 48px; margin: 0 auto 12px; display: block; opacity: .4; }
.empty p { font-size: 15px; }

/* Section title */
.section-title { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; margin: 16px 0 8px; }

/* Inline edit */
.inline-edit { display: flex; align-items: center; gap: 6px; }
.inline-edit input { width: 90px; padding: 4px 8px; font-size: 14px; border: 1.5px solid var(--primary); border-radius: 6px; }

/* search bar */
.search-bar { position: relative; margin-bottom: 12px; }
.search-bar input { padding-left: 38px; }
.search-bar svg { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--muted); width: 18px; height: 18px; }

/* Notice card styles */
.notice-header { background: var(--notice-header-bg); color: #fff; padding: 16px; border-radius: var(--radius) var(--radius) 0 0; }
.notice-header h3 { font-size: 16px; font-weight: 800; margin-bottom: 4px; }
.notice-header .meta { font-size: 13px; opacity: .8; }
.notice-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.notice-table th { background: #f1f5f9; padding: 8px 6px; text-align: center; font-size: 11px; border: 1px solid var(--border); }
.notice-table td { padding: 8px 6px; text-align: right; border: 1px solid var(--border); }
.notice-table td:first-child { text-align: left; }
.notice-table .total-row td { font-weight: 800; background: #fef9c3; }

/* Segmented control */
.seg-control { display: flex; background: var(--bg); border-radius: 8px; padding: 3px; margin-bottom: 12px; }
.seg-btn { flex: 1; padding: 8px; text-align: center; font-size: 14px; font-weight: 600; border-radius: 6px; cursor: pointer; border: none; background: none; color: var(--muted); transition: all .15s; }
.seg-btn.active { background: var(--card); color: var(--primary); box-shadow: var(--shadow); }

/* Loading */
.loading { text-align: center; padding: 20px; color: var(--muted); font-size: 14px; }

/* Number highlight */
.num-large { font-size: 28px; font-weight: 900; font-variant-numeric: tabular-nums; }
.color-morning { color: #2563eb; }
.color-evening { color: var(--evening); }
.color-expense { color: #dc2626; }
.color-profit { color: #16a34a; }

select { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%2364748b' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14L2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E"); background-repeat: no-repeat; background-position: right 12px center; padding-right: 36px; }

/* Settings reorder */
.settings-item { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--card); }
.settings-item:last-child { border-bottom: none; }
.settings-item .item-label { flex: 1; font-size: 15px; font-weight: 600; }
.settings-item .item-index { font-size: 12px; color: var(--muted); min-width: 20px; }
.item-reorder { display: flex; flex-direction: row; gap: 4px; flex-shrink: 0; }
.reorder-btn { background: none; border: 1px solid var(--border); border-radius: 6px; width: 32px; height: 32px; cursor: pointer; font-size: 14px; color: var(--muted); display: flex; align-items: center; justify-content: center; padding: 0; }
.reorder-btn:disabled { opacity: .25; cursor: default; }
.reorder-btn:not(:disabled):active { background: var(--primary-light); }
