:root {
  --bg: #f5faf0;
  --card: #eef3fc;
  --surface: rgba(255, 255, 255, 0.78);
  --surface-strong: #ffffff;
  --action: #7b8ef0;
  --green: #5bbd3a;
  --lavender: #c0a8d8;
  --pink: #e890a8;
  --text: #2d3a5c;
  --muted: #6b7a9a;
  --line: rgba(45, 58, 92, 0.14);
  --soft-line: rgba(255, 255, 255, 0.72);
  --shadow: 0 14px 32px rgba(45, 58, 92, 0.08);
  --button-shadow: 0 8px 18px rgba(45, 58, 92, 0.08);
  color-scheme: light;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at top left, rgba(192, 168, 216, 0.18), transparent 34%),
    linear-gradient(180deg, rgba(238, 243, 252, 0.74), rgba(245, 250, 240, 0.98) 42%),
    var(--bg);
  color: var(--text);
  font-family: "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", system-ui, sans-serif;
  font-size: 16px;
  font-weight: 400;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.app-shell {
  width: 100%;
  max-width: 430px;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 12px 48px;
}

/* ── Topbar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 4px 18px;
}

h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 780;
  line-height: 1.05;
  letter-spacing: 0;
  flex-shrink: 0;
}

h2 {
  margin: 0;
  font-size: 19px;
  font-weight: 720;
  line-height: 1.25;
  letter-spacing: 0;
}

/* ── Date navigation ── */
.date-nav {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.date-nav-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: rgba(123, 142, 240, 0.1);
  color: var(--action);
  font-size: 20px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0;
}

.date-nav-btn:active {
  background: rgba(123, 142, 240, 0.22);
}

.date-chip {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 7px 10px;
  background: rgba(123, 142, 240, 0.13);
  color: var(--action);
  border-radius: 8px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
  min-width: 110px;
  white-space: nowrap;
  box-shadow: inset 0 0 0 1px rgba(123, 142, 240, 0.1);
  -webkit-tap-highlight-color: transparent;
}

/* Overlays native date picker on iOS/Android */
.date-chip-input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.001;
  border: none !important;
  border-radius: inherit;
  box-shadow: none !important;
  min-height: 0 !important;
  padding: 0 !important;
  background: transparent !important;
  color: transparent !important;
  cursor: pointer;
  z-index: 1;
  font-size: 16px;
}

/* ── Panels ── */
.records-panel,
.form-panel {
  background: rgba(238, 243, 252, 0.94);
  border: 1px solid var(--soft-line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 18px 16px;
  margin-bottom: 12px;
  min-width: 0;
  width: 100%;
  overflow: hidden;   /* 確保面板本身不超出 app-shell */
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.subtle {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
}

/* ── Add CTA ── */
.add-cta {
  width: 100%;
  min-height: 58px;
  background: var(--action);
  color: white;
  font-size: 18px;
  font-weight: 760;
  border-radius: 12px;
  box-shadow: 0 12px 28px rgba(123, 142, 240, 0.28);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  letter-spacing: 0.01em;
}

.add-cta:active {
  box-shadow: 0 6px 14px rgba(123, 142, 240, 0.22);
  transform: translateY(1px);
}

/* ── Form steps ── */
.care-form {
  display: grid;
  gap: 0;
  min-width: 0;
  width: 100%;
}

.form-step {
  padding: 18px 0;
  border-top: 1px solid var(--line);
  min-width: 0;   /* 防止 grid item 撐破 track */
  width: 100%;
  overflow: hidden;
}

.form-step:first-child {
  padding-top: 2px;
  border-top: none;
  overflow: visible; /* 讓 tab scroll container 不被截 */
}

.step-title {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 720;
  line-height: 1.3;
  color: var(--text);
  letter-spacing: 0;
}

/* ── Care item picker ── */
.group-title {
  color: var(--muted);
  font-size: 11px;
  font-weight: 720;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 9px;
}

.item-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 14px;
  width: 100%;
  min-width: 0;
}

.care-groups {
  display: grid;
  gap: 16px;
}

.care-group {
  display: grid;
  gap: 9px;
}

.care-item {
  min-height: 46px;
  border: 1px solid rgba(45, 58, 92, 0.1);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.76);
  color: var(--text);
  font-size: 15px;
  font-weight: 640;
  line-height: 1.25;
  padding: 9px 8px;
  box-shadow: var(--button-shadow);
  width: 100%;
  min-width: 0;
  overflow-wrap: anywhere;
}

.care-item.selected {
  background: linear-gradient(180deg, #8497f4, var(--action));
  color: white;
  border-color: var(--action);
  box-shadow: 0 9px 18px rgba(123, 142, 240, 0.24);
}

/* ── Picker tabs ── */
.picker-tabs-wrap {
  /* 不加 overflow: hidden，否則 iOS 觸控橫滑會被截斷 */
  margin-bottom: 12px;
  min-width: 0;
  width: 100%;
}

.picker-tabs {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 3px;
  scrollbar-width: none;
  width: 100%;          /* 讓 overflow-x: auto 有邊界可比較 */
  min-width: 0;
  -webkit-overflow-scrolling: touch;
}

.picker-tabs::-webkit-scrollbar {
  display: none;
}

.picker-tab {
  flex: 0 0 auto;       /* 不縮、不長，保持自然寬度 */
  padding: 7px 14px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 14px;
  font-weight: 650;
  cursor: pointer;
  white-space: nowrap;
  min-height: 0;
}

.picker-tab.active {
  background: var(--action);
  color: white;
  border-color: var(--action);
  box-shadow: 0 4px 10px rgba(123, 142, 240, 0.28);
}

/* ── Selected chips ── */
.selected-chips {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 12px;
}

.selected-chips:empty {
  display: none;
}

.selected-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 6px 10px 6px 12px;
  border-radius: 20px;
  background: rgba(123, 142, 240, 0.14);
  color: var(--action);
  font-size: 13px;
  font-weight: 650;
  line-height: 1;
}

.chip-remove {
  width: 17px;
  height: 17px;
  border-radius: 50%;
  background: rgba(123, 142, 240, 0.22);
  color: var(--action);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0;
  min-height: 0;
  flex-shrink: 0;
}

.chips-clear-btn {
  margin-left: 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  min-height: 0;
  padding: 5px 8px;
  background: transparent;
}

.picker-item-grid {
  margin-bottom: 0;
}

/* ── Time fields ── */
.time-fields {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 10px;
}

.time-fields label:first-child {
  grid-column: 1 / -1;
}

label {
  display: grid;
  gap: 7px;
  min-width: 0;
}

label span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
  line-height: 1.2;
}

input,
select,
textarea {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 52px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-strong);
  color: var(--text);
  padding: 12px 13px;
  outline: none;
  font-size: 16px;
  font-weight: 500;
  line-height: 1.35;
  box-shadow: 0 1px 0 rgba(45, 58, 92, 0.03);
}

input[type="date"],
input[type="time"] {
  min-width: 0;
  max-width: 100%;
  appearance: none;
  -webkit-appearance: none;
  font-size: 15px;
  font-weight: 550;
}

input::-webkit-date-and-time-value {
  text-align: left;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--action);
  box-shadow: 0 0 0 3px rgba(123, 142, 240, 0.2);
}

textarea {
  resize: vertical;
  min-height: 100px;
  line-height: 1.55;
}

/* ── Time meta (night tag + duration hint) ── */
.time-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 22px;
}

.night-tag {
  display: inline-block;
  padding: 4px 9px;
  border-radius: 6px;
  background: rgba(192, 168, 216, 0.24);
  color: #604879;
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
}

.night-tag.period-dawn {
  background: rgba(37, 43, 82, 0.16);
  color: #34385f;
}

.night-tag.period-day {
  background: rgba(91, 189, 58, 0.13);
  color: var(--text);
}

.night-tag.period-evening {
  background: rgba(226, 166, 86, 0.22);
  color: #76542a;
}

.night-tag.period-night {
  background: #1a1e3a;
  color: #f0ecff;
}

.duration-hint {
  color: var(--muted);
  font-size: 13px;
  font-weight: 500;
}

/* ── Full-width fields ── */
.full-field {
  display: grid;
  gap: 7px;
  min-width: 0;
}

.full-field > span {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
  line-height: 1.2;
}

/* ── Image field ── */
.image-field {
  margin-top: 12px;
}

.image-field-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.image-button {
  flex: 1;
  min-height: 46px;
  border-radius: 8px;
  background: rgba(91, 189, 58, 0.12);
  color: #3f7f2b;
  border: 1px dashed rgba(91, 189, 58, 0.4);
  font-size: 15px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.image-button input {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  opacity: 0;
  pointer-events: none;
}

.image-preview,
.record-thumbs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}

.image-preview-item {
  position: relative;
  width: 76px;
  height: 76px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.thumb {
  width: 66px;
  height: 66px;
  object-fit: cover;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.92);
  box-shadow: 0 8px 16px rgba(45, 58, 92, 0.12);
}

.image-remove-button {
  position: absolute;
  top: 0;
  right: 0;
  width: 30px;
  height: 30px;
  border-radius: 999px;
  background: rgba(45, 58, 92, 0.72);
  color: #ffffff;
  box-shadow: 0 5px 12px rgba(45, 58, 92, 0.22);
  font-size: 13px;
  font-weight: 820;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.image-remove-button:active {
  background: rgba(45, 58, 92, 0.9);
  transform: scale(0.96);
}

/* ── Primary / secondary / text buttons ── */
.primary-button {
  width: 100%;
  min-height: 56px;
  background: var(--action);
  color: white;
  font-size: 18px;
  font-weight: 760;
  border-radius: 10px;
  box-shadow: 0 12px 24px rgba(123, 142, 240, 0.28);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 6px;
}

.secondary-button {
  min-height: 44px;
  padding: 0 13px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.62);
  color: #477c35;
  border: 1px solid rgba(91, 189, 58, 0.16);
  font-size: 15px;
  font-weight: 680;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.text-button {
  min-height: 36px;
  padding: 0 10px;
  border-radius: 8px;
  background: transparent;
  color: var(--action);
  font-size: 15px;
  font-weight: 650;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* ── Records list ── */
.records-list {
  display: grid;
  gap: 10px;
}

.record-card {
  display: grid;
  gap: 9px;
  padding: 14px;
  background: rgba(255, 255, 255, 0.76);
  border: 1px solid rgba(45, 58, 92, 0.1);
  border-radius: 10px;
  box-shadow: 0 7px 18px rgba(45, 58, 92, 0.06);
}

.record-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.record-time {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 19px;
  font-weight: 780;
  line-height: 1.1;
  letter-spacing: 0.01em;
}

.record-actions {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.delete-record-button {
  min-height: 28px;
  padding: 4px 7px;
  border-radius: 6px;
  background: transparent;
  color: rgba(107, 122, 154, 0.72);
  font-size: 12px;
  font-weight: 620;
  line-height: 1;
}

.retry-sync-button {
  min-height: 28px;
  padding: 4px 8px;
  border-radius: 6px;
  background: rgba(45, 58, 92, 0.08);
  color: var(--text);
  font-size: 12px;
  font-weight: 680;
  line-height: 1;
  white-space: nowrap;
}

.retry-sync-button:active {
  background: rgba(45, 58, 92, 0.14);
}

.delete-record-button:active {
  background: rgba(45, 58, 92, 0.06);
  color: var(--text);
}

.sync-status {
  padding: 3px 7px;
  border-radius: 6px;
  background: rgba(107, 122, 154, 0.08);
  color: var(--muted);
  font-size: 11px;
  font-weight: 620;
  line-height: 1;
  white-space: nowrap;
}

.sync-status.is-synced {
  background: rgba(91, 189, 58, 0.1);
  color: #477c35;
}

.sync-status.is-syncing {
  background: rgba(92, 150, 208, 0.12);
  color: #386f9d;
}

.sync-status.is-failed {
  background: rgba(232, 144, 168, 0.12);
  color: #a24b62;
}

.sync-status.is-partial {
  background: rgba(219, 166, 74, 0.12);
  color: #94682d;
}

.tag-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  padding: 5px 8px 6px;
  border-radius: 6px;
  background: rgba(91, 189, 58, 0.13);
  color: var(--text);
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
}

.tag.food {
  background: rgba(232, 144, 168, 0.17);
}

.tag.period-dawn {
  background: rgba(37, 43, 82, 0.16);
  color: #34385f;
}

.tag.period-day {
  background: rgba(91, 189, 58, 0.13);
  color: var(--text);
}

.tag.period-evening {
  background: rgba(226, 166, 86, 0.22);
  color: #76542a;
}

.tag.period-night {
  background: #1a1e3a;
  color: #f0ecff;
}

.tag.night {
  background: rgba(192, 168, 216, 0.28);
  color: #604879;
}

.tag.night-on {
  background: #1a1e3a;
  color: #f0ecff;
}

.tag.evidence {
  background: rgba(123, 142, 240, 0.13);
  color: #5363bc;
}

.image-count-tag {
  flex: 0 0 auto;
  background: rgba(91, 189, 58, 0.1);
  color: #5d7d50;
  border-radius: 6px;
  padding: 5px 8px 6px;
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
}

.record-note {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.empty-state {
  padding: 24px 12px;
  color: var(--muted);
  text-align: center;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.6;
}

/* ── Export panel (details element) ── */
details.export-panel {
  background: rgba(238, 243, 252, 0.7);
  border: 1px solid var(--soft-line);
  border-radius: 10px;
  margin-bottom: 12px;
  overflow: hidden;
}

details.export-panel > summary {
  padding: 14px 16px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 650;
  color: var(--muted);
  list-style: none;
  display: flex;
  align-items: center;
  gap: 6px;
  -webkit-tap-highlight-color: transparent;
}

details.export-panel > summary::before {
  content: "›";
  display: inline-block;
  transition: transform 200ms ease;
  font-size: 17px;
  line-height: 1;
}

details.export-panel[open] > summary::before {
  transform: rotate(90deg);
}

details.export-panel > summary::-webkit-details-marker {
  display: none;
}

.export-actions {
  padding: 0 16px 16px;
  display: grid;
  gap: 8px;
}

/* ── Toast ── */
.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  transform: translate(-50%, 18px);
  width: min(92vw, 390px);
  padding: 13px 16px;
  border-radius: 10px;
  background: #2d3a5c;
  color: white;
  font-weight: 700;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 100;
}

.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ── Dark mode ── */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1e3a;
    --card: #242b4d;
    --surface: rgba(255, 255, 255, 0.09);
    --surface-strong: rgba(255, 255, 255, 0.11);
    --text: #f2f5ff;
    --muted: #b8c2df;
    --line: rgba(242, 245, 255, 0.15);
    --soft-line: rgba(255, 255, 255, 0.1);
    color-scheme: dark;
  }

  body {
    background:
      linear-gradient(180deg, rgba(26, 30, 58, 0.96), rgba(36, 43, 77, 0.92)),
      var(--bg);
  }

  .records-panel,
  .form-panel,
  .record-card {
    background: rgba(36, 43, 77, 0.92);
    border-color: rgba(255, 255, 255, 0.1);
  }

  details.export-panel {
    background: rgba(36, 43, 77, 0.7);
    border-color: rgba(255, 255, 255, 0.1);
  }

  input,
  select,
  textarea,
  .care-item,
  .empty-state {
    background: rgba(255, 255, 255, 0.08);
  }

  .secondary-button {
    background: rgba(255, 255, 255, 0.09);
  }
}

/* ── Responsive ── */
@media (min-width: 700px) {
  .app-shell {
    width: min(100%, 760px);
  }

  .item-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .export-actions {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 374px) {
  .app-shell {
    padding-left: 10px;
    padding-right: 10px;
  }

  .records-panel,
  .form-panel {
    padding: 15px 13px;
  }

  .time-fields {
    gap: 8px;
  }

  input[type="date"],
  input[type="time"] {
    font-size: 14px;
    padding-left: 10px;
    padding-right: 10px;
  }

  .item-grid {
    gap: 7px;
  }

  .date-chip {
    min-width: 96px;
    font-size: 12px;
    padding: 6px 8px;
  }
}
