:root {
  --bg: #fff8f0;
  --bg-soft: #fdeede;
  --card: #ffffff;
  --ink: #4a3524;
  --ink-soft: #8a715c;
  --line: #f0dcc6;
  --primary: #e8743b;
  --primary-dark: #cf5a24;
  --amber: #f6a623;
  --accent: #c0492f;
  --shadow: 0 8px 26px rgba(180, 110, 60, 0.14);
  --radius: 16px;

  /* 工作項目色票 */
  --t-visit: #e8743b;   /* 面訪 */
  --t-call: #f6a623;    /* 電訪 */
  --t-sign: #3f8f6b;    /* 簽約 */
  --t-channel: #9c6ade; /* 通路來訪 */
  --t-online: #2f9caf;  /* 線上進件 */
  --t-other: #a08b76;   /* 其他 */
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, var(--bg) 0%, var(--bg-soft) 100%);
  color: var(--ink);
  min-height: 100vh;
}

/* ---- Header ---- */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 28px;
  background: linear-gradient(120deg, var(--primary) 0%, var(--amber) 100%);
  color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-mark {
  width: 46px; height: 46px;
  display: grid; place-items: center;
  background: rgba(255, 255, 255, 0.22);
  border: 2px solid rgba(255, 255, 255, 0.6);
  border-radius: 12px;
  font-size: 24px; font-weight: 800;
}
.brand-text h1 { margin: 0; font-size: 1.25rem; letter-spacing: 1px; }
.brand-text p { margin: 2px 0 0; font-size: 0.85rem; opacity: 0.92; }
.header-actions { display: flex; align-items: center; gap: 10px; }
.admin-badge {
  background: rgba(255, 255, 255, 0.9);
  color: var(--primary-dark);
  font-weight: 700; font-size: 0.8rem;
  padding: 5px 12px; border-radius: 999px;
}

/* ---- Layout ---- */
.layout {
  max-width: 1160px;
  margin: 26px auto;
  padding: 0 20px;
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 22px;
  align-items: start;
}
.calendar-card, .detail-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

/* ---- Todo bar ---- */
.todo-bar {
  grid-column: 1 / -1;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 20px;
  border-left: 6px solid var(--amber);
}
.todo-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.todo-head h3 { margin: 0; font-size: 1.05rem; }
.todo-add { display: flex; gap: 8px; flex: 1; min-width: 220px; }
.todo-add input {
  flex: 1; min-width: 0; font: inherit; color: var(--ink);
  border: 1px solid var(--line); border-radius: 9px; padding: 8px 11px; background: #fffdf9;
}
.todo-add input:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
.todo-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 7px; }
.todo-list .todo-item {
  display: flex; align-items: flex-start; gap: 8px;
  font-size: 0.92rem; line-height: 1.4;
}
.todo-list .todo-item::before { content: '•'; color: var(--amber); font-weight: 700; }
.todo-list .todo-text { flex: 1; word-break: break-word; }
.todo-list .todo-del {
  background: none; border: none; cursor: pointer; color: #c0392b;
  font-size: 0.9rem; line-height: 1; padding: 0 2px;
}
.todo-empty { color: var(--ink-soft); font-size: 0.9rem; }

/* ---- Toolbar ---- */
.cal-toolbar {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 14px;
}
.cal-toolbar h2 { margin: 0; font-size: 1.3rem; min-width: 150px; text-align: center; }
.btn-today { margin-left: auto; }

/* ---- Legend ---- */
.legend { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 14px; }
.legend .lg { display: inline-flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--ink-soft); }
.legend .dot { width: 11px; height: 11px; border-radius: 50%; }

/* ---- Weekday row ---- */
.weekday-row {
  display: grid; grid-template-columns: repeat(7, 1fr);
  text-align: center; font-size: 0.82rem; font-weight: 700;
  color: var(--ink-soft); margin-bottom: 8px;
}
.weekday-row span:first-child, .weekday-row span:last-child { color: var(--accent); }

/* ---- Grid ---- */
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 8px; }
.day {
  height: 98px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px 7px;
  background: #fffdf9;
  cursor: pointer;
  display: flex; flex-direction: column; gap: 4px;
  transition: transform 0.08s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}
.day:hover { border-color: var(--primary); box-shadow: 0 4px 12px rgba(232, 116, 59, 0.18); transform: translateY(-1px); }
.day.blank { background: transparent; border: none; cursor: default; }
.day.today { border-color: var(--primary); background: #fff3e8; }
.day.selected { outline: 2px solid var(--primary); outline-offset: 1px; }
.day .dnum { font-size: 0.85rem; font-weight: 700; color: var(--ink-soft); }
.day.today .dnum { color: var(--primary-dark); }
.day .weekend { color: var(--accent); }
.pill {
  font-size: 0.7rem; line-height: 1.3;
  color: #fff; border-radius: 6px;
  padding: 1px 5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.more { font-size: 0.68rem; color: var(--ink-soft); }

/* ---- Detail panel ---- */
.detail-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.detail-head h3 { margin: 0; font-size: 1.05rem; }
.detail-list { display: flex; flex-direction: column; gap: 10px; }
.empty-hint { color: var(--ink-soft); font-size: 0.9rem; }
.event-item {
  border: 1px solid var(--line);
  border-left-width: 5px;
  border-radius: 10px;
  padding: 10px 12px;
  background: #fffdf9;
}
.event-item .ev-top { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.event-item .ev-time { font-weight: 700; font-size: 0.9rem; }
.event-item .ev-type {
  font-size: 0.72rem; color: #fff; padding: 1px 8px; border-radius: 999px;
}
.event-item .ev-edit {
  margin-left: auto; background: none; border: none; cursor: pointer;
  color: var(--primary-dark); font-size: 0.8rem; font-weight: 700;
}
.event-item dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 0.85rem; }
.event-item dt { color: var(--ink-soft); }
.event-item dd { margin: 0; }
.event-item .ev-note { margin-top: 6px; font-size: 0.82rem; color: var(--ink-soft); }

/* ---- Buttons ---- */
.btn {
  font: inherit; cursor: pointer; border-radius: 10px;
  padding: 8px 14px; border: 1px solid transparent;
  transition: background 0.12s ease, opacity 0.12s ease;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-ghost:hover { background: var(--bg-soft); }
.header-actions .btn-ghost { border-color: rgba(255,255,255,0.7); color: #fff; }
.header-actions .btn-ghost:hover { background: rgba(255,255,255,0.18); }
.btn-danger { background: #d64545; color: #fff; }
.btn-danger:hover { background: #b83636; }
.btn-nav { background: var(--bg-soft); font-size: 1.3rem; line-height: 1; padding: 4px 12px; }
.btn-nav:hover { background: var(--line); }

/* ---- Modal ---- */
.modal {
  position: fixed; inset: 0;
  background: rgba(60, 40, 25, 0.42);
  display: grid; place-items: center;
  padding: 16px; z-index: 50;
}
/* hidden 屬性要能真正隱藏彈窗：此規則權重高於 .modal，否則 display:grid 會蓋過 hidden */
.modal[hidden] { display: none; }
.modal-box {
  background: var(--card); border-radius: var(--radius);
  padding: 22px; width: 100%; max-width: 460px;
  box-shadow: 0 18px 48px rgba(80, 50, 25, 0.3);
  /* 內容超過畫面高度時，彈窗自己可上下捲動，避免按不到儲存鈕 */
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}
.modal-sm { max-width: 360px; }
.modal-box h3 { margin: 0 0 14px; }
.modal-box form { display: flex; flex-direction: column; gap: 12px; }
.modal-box label { display: flex; flex-direction: column; gap: 5px; font-size: 0.85rem; color: var(--ink-soft); }
.modal-box input, .modal-box textarea, .modal-box select {
  font: inherit; color: var(--ink);
  border: 1px solid var(--line); border-radius: 9px;
  padding: 9px 11px; background: #fffdf9;
}
.modal-box select { appearance: auto; }
.modal-box input:focus, .modal-box textarea:focus, .modal-box select:focus { outline: 2px solid var(--primary); border-color: var(--primary); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* 時間三欄：上午/下午 + 時 + 分 */
.time-field { display: flex; flex-direction: column; gap: 5px; }
.tf-label { font-size: 0.85rem; color: var(--ink-soft); }
.tf-row { display: flex; align-items: center; gap: 8px; }
.tf-row select { flex: 1; min-width: 0; }
.tf-row .t-period { flex: 1.4; }
.tf-row .t-colon { color: var(--ink-soft); font-weight: 700; }
.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.modal-actions .spacer { flex: 1; }
.form-error { color: #c0392b; font-size: 0.85rem; margin: 0; }
.muted { color: var(--ink-soft); font-size: 0.85rem; margin: 0 0 4px; }

/* ---- Toast ---- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--ink); color: #fff;
  padding: 10px 18px; border-radius: 999px;
  font-size: 0.88rem; box-shadow: var(--shadow); z-index: 90;
}

/* ---- Responsive ---- */
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .day { height: 86px; }
  .brand-text h1 { font-size: 1.05rem; }
}
