/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Variables ── */
:root {
  --primary:        #e8825a;
  --primary-dark:   #d06840;
  --green:          #4a9e6b;
  --green-bg:       #d4ede0;
  --red:            #d64c4c;
  --red-bg:         #f8d7d7;
  --gray:           #c0bfbd;
  --gray-bg:        #f0efee;
  --bg:             #faf8f5;
  --surface:        #ffffff;
  --text:           #1a1a1a;
  --text-muted:     #6b6b6b;
  --border:         #e6e2dd;
  --nav-h:          64px;
  --header-h:       54px;
  --radius:         14px;
  --shadow:         0 1px 6px rgba(0,0,0,0.07), 0 2px 12px rgba(0,0,0,0.04);
}

/* ── Base ── */
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--primary); }

/* ── App Shell ── */
#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 860px;
  margin: 0 auto;
  position: relative;
}

/* ── Header ── */
#app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
  flex-shrink: 0;
}
.app-logo { font-size: 1.3rem; }
.app-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--primary);
  letter-spacing: -0.3px;
  flex: 1;
}
.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1.15rem;
  color: var(--text-muted);
  padding: 8px;
  border-radius: 8px;
  line-height: 1;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--gray-bg); color: var(--text); }

/* ── Backup banner ── */
.backup-banner {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  background: #fff8e1;
  border-bottom: 1px solid #ffe082;
  padding: 12px 16px;
  flex-shrink: 0;
}
.backup-banner-icon { font-size: 1.2rem; flex-shrink: 0; margin-top: 1px; }
.backup-banner-text {
  flex: 1;
  font-size: 0.84rem;
  color: #5a4200;
  line-height: 1.5;
}
.backup-banner-text strong { display: block; margin-bottom: 2px; }
.backup-banner-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.backup-btn-do {
  background: #f59e0b;
  color: #fff;
  border: none;
  border-radius: 7px;
  padding: 6px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.backup-btn-do:hover { background: #d97706; }
.backup-btn-dismiss {
  background: none;
  border: none;
  cursor: pointer;
  color: #a07000;
  font-size: 1rem;
  padding: 4px;
  line-height: 1;
}

/* ── Main / Views ── */
#main-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding-bottom: calc(var(--nav-h) + 8px);
}
.view { display: none; padding: 16px; }
.view.active { display: block; }

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}
.view-header h2 {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
}

/* ── Today view ── */
.summary-pill {
  background: var(--primary);
  color: #fff;
  border-radius: 20px;
  padding: 3px 12px;
  font-size: 0.85rem;
  font-weight: 600;
}

.habit-list { display: flex; flex-direction: column; gap: 10px; }

.habit-item {
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  user-select: none;
  transition: transform 0.1s;
  border: 2px solid transparent;
}
.habit-item:active { transform: scale(0.985); }
.habit-item.status-achieved  { border-color: var(--green); }
.habit-item.status-not-achieved { border-color: var(--red); }

.status-dot {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 2px solid var(--gray);
  background: var(--gray-bg);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  font-weight: 700;
  color: #fff;
  transition: background 0.15s, border-color 0.15s;
}
.status-dot.achieved      { background: var(--green); border-color: var(--green); }
.status-dot.not-achieved  { background: var(--red);   border-color: var(--red);   }

.habit-info { flex: 1; min-width: 0; }
.habit-name {
  font-weight: 500;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.habit-freq  { font-size: 0.78rem; color: var(--text-muted); margin-top: 1px; }
.habit-goal-line {
  font-size: 0.78rem;
  color: var(--primary);
  margin-top: 2px;
  font-weight: 500;
}

/* ── Legend ── */
.legend {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  margin-top: 20px;
  padding: 12px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.78rem;
  color: var(--text-muted);
}
.dot {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}
.dot.green { background: var(--green); }
.dot.red   { background: var(--red); }
.dot.gray  { background: var(--gray); }
.dot.empty { background: var(--bg); border: 1px solid var(--border); }

/* ── Month view ── */
.month-nav { justify-content: space-between; }
.nav-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 14px;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--text);
  line-height: 1;
  box-shadow: var(--shadow);
}
.nav-btn:hover { background: var(--gray-bg); }

.month-table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.month-table {
  border-collapse: separate;
  border-spacing: 0;
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  min-width: max-content;
  width: 100%;
}

/* Sticky habit-name column */
.month-table th.habit-col,
.month-table td.habit-col {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface);
  border-right: 2px solid var(--border);
}
.month-table thead th.habit-col {
  background: var(--gray-bg);
  z-index: 3;
}

.month-table thead th {
  background: var(--gray-bg);
  padding: 6px 4px;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  text-align: center;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.month-table thead th.habit-col {
  font-size: 0.75rem;
  text-align: left;
  padding: 8px 12px;
  min-width: 120px;
}
.day-num { display: block; font-size: 0.72rem; color: var(--text); }
.day-name { display: block; font-size: 0.62rem; color: var(--text-muted); }

.month-table tbody td {
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  padding: 0;
  text-align: center;
  vertical-align: middle;
}
.month-table tbody td.habit-col {
  padding: 8px 12px;
  text-align: left;
  border-right: 2px solid var(--border);
}
.month-table tbody tr:last-child td { border-bottom: none; }

.habit-col-name {
  font-size: 0.82rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}
.habit-col-goal {
  font-size: 0.7rem;
  color: var(--primary);
  font-weight: 500;
  margin-top: 1px;
}

/* Today column highlight */
.month-table th.today-col,
.month-table td.today-col {
  background: rgba(232, 130, 90, 0.07);
}
.month-table thead th.today-col {
  background: rgba(232, 130, 90, 0.15);
}

/* Day squares inside table cells */
.day-sq {
  width: 28px;
  height: 28px;
  border-radius: 5px;
  margin: 4px auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: #fff;
  transition: opacity 0.1s, transform 0.08s;
}
.day-sq.clickable { cursor: pointer; }
.day-sq.clickable:hover { opacity: 0.82; transform: scale(1.08); }
.day-sq.achieved      { background: var(--green); }
.day-sq.not-achieved  { background: var(--red); }
.day-sq.unregistered  { background: var(--gray-bg); border: 1.5px solid var(--gray); color: transparent; }
.day-sq.not-planned   { background: transparent; cursor: default; }
.day-sq.future        { background: var(--gray-bg); opacity: 0.35; cursor: default; }

/* ── Habits management ── */
.habits-management-list { display: flex; flex-direction: column; gap: 10px; }

.habit-manage-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: background 0.12s;
}
.habit-manage-item:hover { background: #fdfcfb; }

.freq-badge {
  display: inline-flex;
  background: var(--gray-bg);
  color: var(--text-muted);
  border-radius: 10px;
  padding: 2px 9px;
  font-size: 0.74rem;
  font-weight: 500;
}
.freq-badge.goal-badge { background: #fdeee8; color: var(--primary); }

.manage-chevron { color: var(--text-muted); font-size: 1.1rem; margin-left: auto; flex-shrink: 0; }

/* ── Reorder buttons ── */
.reorder-btns {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}
.reorder-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 5px;
  width: 26px;
  height: 22px;
  cursor: pointer;
  font-size: 0.6rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, color 0.12s;
  padding: 0;
}
.reorder-btn:hover:not(:disabled) { background: var(--gray-bg); color: var(--text); }
.reorder-btn:disabled { opacity: 0.25; cursor: default; }

/* ── Archived habits ── */
.row-archived td { opacity: 0.65; }
.archived-badge {
  display: inline-block;
  background: var(--gray-bg);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 0.65rem;
  font-weight: 500;
  margin-left: 6px;
  vertical-align: middle;
}
.archived-section-header {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin: 20px 0 8px;
  padding: 0 4px;
}
.archived-item {
  opacity: 0.7;
  cursor: default;
}
.archived-item:hover { background: var(--surface); }
.archived-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.archived-badge-pill {
  background: var(--gray-bg);
  color: var(--text-muted);
}
.btn-restore {
  background: none;
  border: 1px solid var(--green);
  color: var(--green);
  border-radius: 7px;
  padding: 4px 10px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.btn-restore:hover { background: var(--green-bg); }
.btn-permdelete {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 7px;
  padding: 4px 10px;
  font-size: 0.78rem;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}
.btn-permdelete:hover { background: var(--red-bg); border-color: var(--red); color: var(--red); }

/* ── Monthly summary ── */
.month-summary-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin-top: 16px;
  box-shadow: var(--shadow);
}
.summary-heading {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 14px;
}
.summary-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.summary-row:last-child { border-bottom: none; padding-bottom: 0; }
.summary-row:first-of-type { padding-top: 0; }
.summary-medal { font-size: 1.6rem; flex-shrink: 0; }
.summary-info  { flex: 1; min-width: 0; }
.summary-habit-name {
  font-weight: 500;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.summary-stats {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 3px;
  flex-wrap: wrap;
}
.summary-count { font-size: 0.8rem; color: var(--text-muted); }
.summary-label {
  font-size: 0.78rem;
  font-weight: 600;
  border-radius: 8px;
  padding: 1px 8px;
}
.summary-label.gold   { background: #fff8e1; color: #b8860b; }
.summary-label.silver { background: #f0f0f0; color: #666; }
.summary-label.bronze { background: #fdf0e6; color: #a0522d; }
.summary-label.cheer  { background: #e8f5e9; color: #2e7d32; }

/* ── Monthly reflection ── */
.reflection-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin-top: 16px;
  box-shadow: var(--shadow);
}
.reflection-heading {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted);
  margin-bottom: 10px;
}
.reflection-prompts {
  font-size: 0.83rem;
  color: var(--text-muted);
  font-style: italic;
  line-height: 1.6;
  margin-bottom: 12px;
}
.reflection-textarea {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: 9px;
  padding: 10px 13px;
  font-size: 0.92rem;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  resize: vertical;
  transition: border-color 0.15s;
}
.reflection-textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.reflection-status {
  font-size: 0.75rem;
  color: var(--green);
  margin-top: 6px;
  min-height: 1em;
  text-align: right;
}

/* ── Settings / About ── */
.about-section { font-size: 0.88rem; color: var(--text-muted); line-height: 1.7; }
.about-section p  { margin-bottom: 10px; }
.about-section h4 {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text);
  margin: 14px 0 4px;
}
.about-section ul {
  padding-left: 18px;
  margin-bottom: 10px;
}
.about-section li { margin-bottom: 4px; }
.about-credit {
  margin-top: 16px !important;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 0.8rem !important;
}

/* ── Settings ── */
.settings-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.settings-card h3 {
  font-size: 0.78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.settings-desc {
  font-size: 0.86rem;
  color: var(--text-muted);
  line-height: 1.6;
}
.settings-btns { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }

/* ── Empty state ── */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}
.empty-state p { margin-bottom: 16px; font-size: 0.95rem; }
.empty-hint { font-size: 0.85rem !important; }

/* ── Bottom nav ── */
#bottom-nav {
  display: flex;
  height: var(--nav-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 860px;
  z-index: 20;
}
.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-family: inherit;
  transition: color 0.15s;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.nav-item.active { color: var(--primary); }
.nav-icon  { font-size: 1.25rem; line-height: 1; }
.nav-label { font-size: 0.68rem; font-weight: 600; }

/* ── Buttons ── */
.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 9px;
  padding: 10px 18px;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s;
  white-space: nowrap;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 16px;
  font-size: 0.88rem;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: background 0.15s;
}
.btn-secondary:hover { background: var(--gray-bg); }

.btn-danger {
  background: none;
  color: var(--red);
  border: 1px solid var(--red);
  border-radius: 9px;
  padding: 10px 16px;
  font-size: 0.88rem;
  cursor: pointer;
  font-family: inherit;
}
.btn-danger:hover { background: var(--red-bg); }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.modal {
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 24px;
  width: 100%;
  max-width: 520px;
  max-height: 90dvh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 22px;
}
.modal-header h3 { font-size: 1.05rem; font-weight: 700; }

/* ── Form ── */
.form-group { margin-bottom: 20px; }
.form-group label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 8px;
}
.label-hint { font-weight: 400; }

.form-group input[type="text"],
.form-group input[type="number"] {
  width: 100%;
  border: 1.5px solid var(--border);
  border-radius: 9px;
  padding: 10px 13px;
  font-size: 0.95rem;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  transition: border-color 0.15s;
}
.form-group input:focus {
  outline: none;
  border-color: var(--primary);
}
.form-group small {
  display: block;
  font-size: 0.74rem;
  color: var(--text-muted);
  margin-top: 5px;
}

.radio-group { display: flex; flex-direction: column; gap: 11px; }
.radio-label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.92rem;
  cursor: pointer;
  font-weight: 400 !important;
  color: var(--text) !important;
}
.radio-label input[type="radio"] { accent-color: var(--primary); width: 17px; height: 17px; }

.days-picker { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 4px; }
.day-toggle { cursor: pointer; }
.day-toggle input { display: none; }
.day-toggle span {
  display: inline-block;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1.5px solid var(--border);
  font-size: 0.82rem;
  font-weight: 500;
  background: var(--surface);
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  user-select: none;
}
.day-toggle input:checked + span {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 8px;
}

/* ── Utilities ── */
.hidden { display: none !important; }

/* ── Desktop ── */
@media (min-width: 600px) {
  .view { padding: 24px; }
  .modal {
    border-radius: 20px;
    margin-bottom: 0;
  }
  .modal-overlay { align-items: center; }
  .habit-col-name { max-width: 180px; }
  .settings-btns { flex-direction: row; }
}

/* Mobile-only hint in month view */
.mobile-month-hint {
  display: none;
  background: var(--gray-bg);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.83rem;
  color: var(--text-muted);
  margin-bottom: 16px;
  line-height: 1.5;
}
@media (max-width: 699px) {
  .mobile-month-hint { display: block; }
}

/* On wide screens: table fills full width so all days are visible at once */
@media (min-width: 700px) {
  .month-table-wrapper {
    overflow-x: visible;
  }
  .month-table {
    min-width: unset;
    width: 100%;
    table-layout: fixed;
  }
  .month-table th.habit-col,
  .month-table td.habit-col {
    width: 130px;
    min-width: 130px;
  }
  .month-table thead th:not(.habit-col),
  .month-table tbody td:not(.habit-col) {
    width: auto;
  }
  .day-sq {
    width: 100%;
    max-width: 28px;
    min-width: 14px;
    height: 22px;
    font-size: 0.65rem;
  }
  .month-table thead th:not(.habit-col) {
    padding: 4px 1px;
  }
  .day-num  { font-size: 0.65rem; }
  .day-name { font-size: 0.55rem; }
}
