:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-strong: #0d5f58;
  --primary-soft: #ccfbf1;
  --danger: #dc2626;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 8px 24px rgba(15, 23, 42, 0.06);
  --radius: 14px;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111a2e;
  --surface-2: #0f1729;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --border: #1e293b;
  --primary: #2dd4bf;
  --primary-strong: #14b8a6;
  --primary-soft: #134e4a;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.3);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { font-size: 26px; }
.brand h1 { font-size: 1.25rem; margin: 0; letter-spacing: -0.02em; }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* Buttons */
.btn {
  font: inherit;
  font-weight: 600;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 16px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s, color .15s;
  background: var(--surface-2);
  color: var(--text);
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-strong); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text-muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--text-muted); }
.btn-icon { padding: 9px 11px; border-color: var(--border); background: transparent; }
.btn-danger { color: var(--danger); background: transparent; border-color: transparent; }
.btn-danger:hover { background: rgba(220, 38, 38, 0.1); }
.btn-sm { padding: 6px 12px; font-size: .875rem; }

/* Layout */
.container { max-width: 1080px; margin: 0 auto; padding: 24px; }

/* Summary */
.summary {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 28px;
}
.summary-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.summary-label { color: var(--text-muted); font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.summary-value { font-size: 1.6rem; font-weight: 700; letter-spacing: -0.02em; }
.summary-ring { position: relative; align-items: center; justify-content: center; }
.ring { width: 96px; height: 96px; transform: rotate(-90deg); }
.ring-bg { fill: none; stroke: var(--border); stroke-width: 10; }
.ring-fg {
  fill: none; stroke: var(--primary); stroke-width: 10; stroke-linecap: round;
  stroke-dasharray: 326.7; stroke-dashoffset: 326.7;
  transition: stroke-dashoffset .6s ease;
}
.ring-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 0;
}
.ring-center .summary-value { font-size: 1.3rem; }
.ring-center .summary-label { font-size: .65rem; }

/* Goals */
.goals { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; }
.goal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.goal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.goal-title { font-size: 1.1rem; font-weight: 700; margin: 0; }
.goal-cat {
  display: inline-block; font-size: .72rem; font-weight: 700; padding: 2px 9px;
  border-radius: 999px; background: var(--primary-soft); color: var(--primary-strong);
  margin-top: 4px; text-transform: uppercase; letter-spacing: .03em;
}
[data-theme="dark"] .goal-cat { color: var(--primary); }
.goal-menu { display: flex; gap: 2px; }

.goal-amounts { display: flex; align-items: baseline; gap: 6px; }
.goal-current { font-size: 1.5rem; font-weight: 700; }
.goal-target { color: var(--text-muted); font-size: .95rem; }

.progress { height: 10px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; background: var(--primary); border-radius: 999px; transition: width .5s ease; }
.progress-bar.complete { background: #16a34a; }

.goal-meta { display: flex; justify-content: space-between; font-size: .82rem; color: var(--text-muted); flex-wrap: wrap; gap: 4px; }
.goal-meta .pct { font-weight: 700; color: var(--text); }
.goal-meta .badge-done { color: #16a34a; font-weight: 700; }
.goal-meta .badge-overdue { color: var(--danger); font-weight: 700; }

.goal-actions { display: flex; gap: 8px; margin-top: 2px; }
.goal-actions .btn { flex: 1; }

/* Empty state */
.empty-state { text-align: center; padding: 64px 20px; color: var(--text-muted); }
.empty-art { font-size: 56px; }
.empty-state h2 { color: var(--text); margin: 12px 0 6px; }
.empty-state p { margin: 0 0 20px; }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 50; display: none; align-items: center; justify-content: center; padding: 16px; }
.modal:not([hidden]) { display: flex; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.55); backdrop-filter: blur(2px); }
.modal-panel {
  position: relative; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 24px; width: 100%; max-width: 460px;
  box-shadow: var(--shadow); max-height: 90vh; overflow-y: auto;
}
.modal-panel h2 { margin: 0 0 18px; font-size: 1.25rem; }
form label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: 14px; color: var(--text); }
form input, form select, form textarea {
  width: 100%; margin-top: 6px; padding: 10px 12px; font: inherit;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface-2);
  color: var(--text); font-weight: 400;
}
form input:focus, form select:focus, form textarea:focus {
  outline: 2px solid var(--primary); outline-offset: 1px; border-color: var(--primary);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.hint { display: block; font-weight: 400; color: var(--text-muted); font-size: .78rem; margin-top: 4px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

/* User chip (Cloudflare Access identity) */
.user-chip {
  display: inline-flex; align-items: center; gap: 8px;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 4px 6px 4px 12px; font-size: .82rem; color: var(--text-muted);
}
.user-dot { font-size: .85rem; }
.user-chip #user-email { font-weight: 600; color: var(--text); max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-logout {
  text-decoration: none; font-weight: 600; color: var(--primary);
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px;
}
.user-logout:hover { border-color: var(--primary); }

/* Sync status bar */
.sync-status[hidden] { display: none; }
.sync-status {
  display: flex; align-items: center; gap: 10px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 14px; margin-bottom: 18px;
  font-size: .85rem; color: var(--text-muted);
}
.sync-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--text-muted); flex: none; }
.sync-dot.on { background: #16a34a; }
.sync-dot.error { background: var(--danger); }
.sync-dot.busy { background: #f59e0b; animation: pulse 1s infinite; }
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .3; } }
.sync-status #sync-now-btn { margin-left: auto; }

/* Sync modal */
.sync-intro { color: var(--text-muted); font-size: .9rem; margin: 0 0 16px; }
.sync-gen { margin: -6px 0 14px; }
.sync-msg { font-size: .85rem; margin: 12px 0 0; padding: 10px 12px; border-radius: 8px; }
.sync-msg.ok { background: var(--primary-soft); color: var(--primary-strong); }
.sync-msg.err { background: rgba(220, 38, 38, 0.12); color: var(--danger); }
[data-theme="dark"] .sync-msg.ok { color: var(--primary); }

/* Footer */
.footer { text-align: center; color: var(--text-muted); font-size: .8rem; padding: 24px; }

/* Responsive */
@media (max-width: 760px) {
  .summary { grid-template-columns: repeat(2, 1fr); }
  .topbar { padding: 14px 16px; }
  .container { padding: 16px; }
  .brand h1 { font-size: 1.1rem; }
}
@media (max-width: 460px) {
  .form-row { grid-template-columns: 1fr; }
}
