:root {
  color-scheme: light;
  --surface-1: #ffffff;
  --surface-2: #f6f6f4;
  --surface-3: #eeede9;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8b8a84;
  --gridline: #e4e3dd;
  --baseline: #c3c2b7;
  --border: rgba(11, 11, 11, 0.08);
  --shadow-sm: 0 1px 2px rgba(11, 11, 11, 0.05), 0 1px 1px rgba(11, 11, 11, 0.03);
  --shadow-md: 0 6px 16px rgba(11, 11, 11, 0.08), 0 2px 4px rgba(11, 11, 11, 0.04);
  --danger: #e34948;
  --danger-ink: #9c1f1e;

  --cell-empty: #e6e5df;

  --hue-green: #008300;
  --hue-green-dark: #1fae1f;
  --hue-blue: #2a78d6;
  --hue-blue-dark: #3987e5;
  --hue-purple: #4a3aa7;
  --hue-purple-dark: #9085e9;
  --hue-orange: #eb6834;
  --hue-orange-dark: #d95926;
  --hue-red: #e34948;
  --hue-red-dark: #e66767;
  --hue-teal: #1baf7a;
  --hue-teal-dark: #199e70;

  --accent: var(--hue-green);
  --accent-ink: #ffffff;
  --accent-wash: rgba(0, 131, 0, 0.09);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --surface-1: #1c1c1b;
    --surface-2: #121212;
    --surface-3: #262625;
    --text-primary: #f5f5f3;
    --text-secondary: #c3c2b7;
    --text-muted: #8b8a84;
    --gridline: #2e2e2c;
    --baseline: #3a3a37;
    --border: rgba(255, 255, 255, 0.09);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 8px 20px rgba(0, 0, 0, 0.4);
    --cell-empty: #2c2c2a;

    --hue-green: var(--hue-green-dark);
    --hue-blue: var(--hue-blue-dark);
    --hue-purple: var(--hue-purple-dark);
    --hue-orange: var(--hue-orange-dark);
    --hue-red: var(--hue-red-dark);
    --hue-teal: var(--hue-teal-dark);

    --accent: var(--hue-green);
    --accent-ink: #06210a;
    --accent-wash: rgba(31, 174, 31, 0.16);
  }
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--surface-2);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-font-smoothing: antialiased;
}

button {
  font-family: inherit;
}

#app {
  max-width: 900px;
  margin: 0 auto;
  padding: 0 16px 48px;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 0 14px;
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--surface-2) 88%, transparent);
  backdrop-filter: saturate(180%) blur(10px);
  -webkit-backdrop-filter: saturate(180%) blur(10px);
  z-index: 5;
}

.topbar h1 {
  font-size: 21px;
  font-weight: 700;
  margin: 0;
  letter-spacing: -0.01em;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 500;
  padding: 8px 0;
  cursor: pointer;
  transition: color 0.15s ease;
}

.back-btn:hover {
  color: var(--text-primary);
}

.icon-btn {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text-secondary);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform 0.12s ease, background 0.15s ease;
}

.icon-btn:active {
  background: var(--surface-3);
  transform: scale(0.96);
}

.section-heading {
  font-size: 15px;
  font-weight: 650;
  color: var(--text-secondary);
  margin: 22px 0 10px;
}

.section-heading:first-of-type {
  margin-top: 4px;
}

.log-hint {
  font-style: italic;
  font-size: 12px;
  color: var(--text-muted);
  margin: -4px 0 12px;
}

/* Cards */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
  margin-top: 4px;
}

.group-card {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  cursor: pointer;
  color: inherit;
  position: relative;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.18s ease, transform 0.12s ease, border-color 0.18s ease;
}

.group-card:hover {
  box-shadow: var(--shadow-md);
  border-color: transparent;
}

.group-card:active {
  transform: scale(0.985);
}

.group-card:focus-visible,
.habit-row:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.group-card .name {
  font-size: 17px;
  font-weight: 650;
  margin-bottom: 5px;
  padding-right: 20px;
}

.group-card .meta {
  font-size: 13px;
  color: var(--text-muted);
}

.delete-x {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  border-radius: 50%;
  color: var(--text-muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.delete-x:hover {
  background: var(--surface-3);
  color: var(--danger-ink);
}

.add-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  margin-top: 16px;
  padding: 15px;
  border-radius: 16px;
  border: 1.5px dashed var(--baseline);
  background: none;
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.add-btn:hover {
  background: var(--accent-wash);
  border-color: var(--accent);
  color: var(--accent);
}

/* Habit rows (inside a group) */
.habit-row {
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 18px;
  margin-bottom: 10px;
  cursor: pointer;
  color: inherit;
  position: relative;
  box-shadow: var(--shadow-sm);
  transition: box-shadow 0.18s ease, transform 0.12s ease;
}

.habit-row:hover {
  box-shadow: var(--shadow-md);
}

.habit-row:active {
  transform: scale(0.99);
}

.habit-row-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding-right: 28px;
  gap: 10px;
}

.habit-row .name {
  font-size: 16px;
  font-weight: 650;
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}

.habit-row .name span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.color-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex: none;
  box-shadow: 0 0 0 3px color-mix(in srgb, currentColor 15%, transparent);
}

.habit-row .stats {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  white-space: nowrap;
}

.mini-heatmap {
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: repeat(7, 1fr);
  gap: 3px;
  overflow: hidden;
  height: 58px;
}

.mini-heatmap .cell {
  width: 7px;
  height: 7px;
  border-radius: 2px;
  background: var(--cell-empty);
}

/* Project links */
.link-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow-sm);
}

.link-info {
  min-width: 0;
  flex: 1;
}

.link-title {
  display: block;
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.link-title:hover {
  color: var(--accent);
}

.link-url {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 2px;
}

.link-actions {
  display: flex;
  align-items: center;
  gap: 2px;
  flex: none;
}

.link-actions .delete-x,
.log-meta .delete-x {
  position: static;
  margin-left: 4px;
}

.move-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 15px;
  cursor: pointer;
  transition: background 0.15s ease;
}

.move-btn:hover:not(:disabled) {
  background: var(--surface-3);
}

.move-btn:disabled {
  opacity: 0.25;
  cursor: default;
}

/* Project log */
.log-form {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow-sm);
}

.log-form textarea {
  width: 100%;
  resize: vertical;
  min-height: 64px;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 15px;
  font-family: inherit;
  margin-bottom: 10px;
}

.log-form textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-wash);
}

.log-form .btn {
  flex: none;
  padding: 10px 18px;
}

.log-entry {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  margin-bottom: 8px;
  box-shadow: var(--shadow-sm);
}

.log-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.log-body {
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.log-meta-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

.edit-btn {
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  border-radius: 50%;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.edit-btn:hover {
  background: var(--surface-3);
  color: var(--accent);
}

.log-edit-textarea {
  width: 100%;
  resize: vertical;
  min-height: 64px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1.5px solid var(--accent);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  margin-bottom: 8px;
}

.log-edit-textarea:focus {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-wash);
}

.log-edit-actions {
  display: flex;
  gap: 8px;
}

.log-edit-actions .btn {
  flex: none;
  padding: 8px 16px;
  font-size: 13px;
}

/* Forms / modal sheet */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 10, 0.45);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 50;
  animation: fade-in 0.15s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slide-up {
  from { transform: translateY(16px); opacity: 0.6; }
  to { transform: translateY(0); opacity: 1; }
}

@media (min-width: 640px) {
  .sheet-backdrop {
    align-items: center;
  }
}

.sheet {
  background: var(--surface-1);
  border-radius: 20px 20px 0 0;
  padding: 22px 22px calc(22px + env(safe-area-inset-bottom));
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-md);
  animation: slide-up 0.2s ease;
}

@media (min-width: 640px) {
  .sheet {
    border-radius: 20px;
  }
}

.sheet h2 {
  margin: 0 0 18px;
  font-size: 18px;
  font-weight: 700;
}

.field {
  margin-bottom: 18px;
}

.field label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 7px;
}

.field input[type="text"],
.field input[type="password"] {
  width: 100%;
  padding: 13px 14px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface-2);
  color: var(--text-primary);
  font-size: 16px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input[type="text"]:focus,
.field input[type="password"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-wash);
}

.color-picker {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.color-swatch {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  box-shadow: 0 0 0 2px transparent;
  cursor: pointer;
  padding: 0;
  position: relative;
  transition: box-shadow 0.15s ease, transform 0.12s ease;
}

.color-swatch:hover {
  transform: scale(1.08);
}

.color-swatch.selected {
  box-shadow: 0 0 0 2px var(--surface-1), 0 0 0 4px currentColor;
}

.color-swatch.selected::after {
  content: '✓';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}

.sheet-actions {
  display: flex;
  gap: 10px;
  margin-top: 6px;
}

.btn {
  flex: 1;
  padding: 14px;
  border-radius: 12px;
  border: none;
  font-size: 15px;
  font-weight: 650;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.15s ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-secondary {
  background: var(--surface-3);
  color: var(--text-primary);
}

.btn-secondary:hover {
  background: var(--gridline);
}

.btn:disabled {
  opacity: 0.5;
}

.form-error {
  color: var(--danger-ink);
  font-size: 13px;
  margin: -10px 0 14px;
  min-height: 16px;
}

/* Empty state */
.empty-state {
  text-align: center;
  color: var(--text-muted);
  padding: 64px 16px;
  font-size: 14px;
  line-height: 1.6;
}

.empty-state .emoji {
  display: block;
  font-size: 34px;
  margin-bottom: 10px;
}

/* Login */
.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 340px;
  text-align: center;
}

.login-logo {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  width: 44px;
  height: 44px;
  margin: 0 auto 18px;
}

.login-logo span {
  border-radius: 4px;
  background: var(--cell-empty);
}

.login-box h1 {
  font-size: 23px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin-bottom: 6px;
}

.login-box p {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 26px;
}

/* Habit detail */
.habit-header {
  display: flex;
  align-items: center;
  gap: 11px;
  margin-bottom: 2px;
}

.habit-header h1 {
  font-size: 21px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 0;
}

.stat-row {
  display: flex;
  gap: 28px;
  margin: 16px 0 22px;
}

.stat {
  text-align: left;
}

.stat .value {
  font-size: 26px;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.stat .label {
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
  margin-top: 1px;
}

.heatmap-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 16px 12px;
  box-shadow: var(--shadow-sm);
}

.heatmap-scroll {
  overflow-x: auto;
  padding-bottom: 8px;
  -webkit-overflow-scrolling: touch;
}

.heatmap-inner {
  display: inline-flex;
  flex-direction: column;
  gap: 4px;
}

.heatmap-months {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 18px;
  gap: 3px;
  padding-left: 29px;
  font-size: 11px;
  font-weight: 500;
  color: var(--text-muted);
  height: 16px;
}

.heatmap-body {
  display: flex;
  gap: 3px;
}

.heatmap-daylabels {
  display: grid;
  grid-template-rows: repeat(7, 18px);
  gap: 3px;
  font-size: 10px;
  color: var(--text-muted);
  width: 26px;
  text-align: right;
  padding-right: 4px;
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface-1);
  flex: none;
}

.heatmap-daylabels span {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 18px;
}

.heatmap-grid {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 18px;
  grid-template-rows: repeat(7, 18px);
  gap: 3px;
}

.heatmap-cell {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  background: var(--cell-empty);
  border: none;
  padding: 0;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.12s ease;
}

.heatmap-cell:not(:disabled):hover {
  transform: scale(1.18);
}

.heatmap-cell:disabled {
  cursor: default;
  opacity: 0;
  pointer-events: none;
}

.heatmap-cell.is-today {
  box-shadow: 0 0 0 2px var(--text-primary) inset;
}

.heatmap-cell:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 14px;
}

.legend-cell {
  width: 14px;
  height: 14px;
  border-radius: 4px;
}

.date-readout {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  min-height: 18px;
  margin-top: 10px;
}

.danger-zone {
  margin-top: 32px;
  padding-top: 16px;
  border-top: 1px solid var(--gridline);
}

.link-danger {
  background: none;
  border: none;
  color: var(--danger-ink);
  font-size: 14px;
  font-weight: 500;
  padding: 8px 0;
  cursor: pointer;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(6px);
  background: var(--text-primary);
  color: var(--surface-1);
  padding: 11px 20px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
