/* Rastreador de Hábitos — CSS3 puro (sem frameworks) */

:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f1efe9;
  --line: #e2ded4;
  --text: #1d2321;
  --muted: #5f6b66;
  --brand: #2f6b4f;
  --brand-ink: #ffffff;
  --brand-soft: #e3efe8;
  --danger: #b4372f;
  --discord: #5865f2;
  --today: #fff5d6;
  --radius: 14px;
  --radius-sm: 8px;
  --cell: 32px;
  --name-col: 230px;
  --shadow: 0 1px 2px rgba(29, 35, 33, 0.06), 0 4px 16px rgba(29, 35, 33, 0.05);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "Liberation Mono", monospace;
}

/* cores dos hábitos (validadas também no servidor) */
.c-azul    { --c: #2f6fd1; --c-soft: #e3ecfa; }
.c-verde   { --c: #2f8a57; --c-soft: #e1f2e8; }
.c-roxo    { --c: #7a4fc9; --c-soft: #eee7fa; }
.c-laranja { --c: #d0661c; --c-soft: #fbeadc; }
.c-rosa    { --c: #c8417a; --c-soft: #f9e2ec; }
.c-ciano   { --c: #138a96; --c-soft: #dcf1f3; }

*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
}

[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

:focus-visible { outline: 3px solid #f0b429; outline-offset: 2px; }

/* ---------- topo ---------- */
.topbar { background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-inner {
  max-width: 1280px; margin: 0 auto; padding: 0.75rem 1.25rem;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
}
.brand { display: inline-flex; align-items: center; gap: 0.6rem; color: var(--text); text-decoration: none; font-weight: 700; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; background: var(--brand);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M9 16.5l4.5 4.5L23 11.5' stroke='white' stroke-width='3.2' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-size: cover;
}
.user { display: flex; align-items: center; gap: 0.65rem; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--surface-2); object-fit: cover; }
.user-text { display: flex; flex-direction: column; line-height: 1.2; }
.user-name { font-weight: 600; }
.user-handle { color: var(--muted); font-size: 0.82rem; }

/* ---------- botões ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem;
  min-height: 40px; padding: 0.5rem 1rem; border-radius: 10px; border: 1px solid transparent;
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background-color 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn-primary { background: var(--brand); color: var(--brand-ink); }
.btn-primary:hover { background: #255a41; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--line); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: transparent; color: var(--danger); border-color: #e9c3bf; }
.btn-danger:hover { background: #fbeceb; }
.btn-discord { background: var(--discord); color: #fff; min-height: 48px; padding: 0.7rem 1.4rem; font-size: 1.05rem; }
.btn-discord:hover { background: #4752c4; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); font-size: 1.5rem; line-height: 1; cursor: pointer; color: var(--text);
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); }
.icon-btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* ---------- layout principal ---------- */
main { flex: 1; width: 100%; max-width: 1280px; margin: 0 auto; padding: 1.5rem 1.25rem 2rem; }

.center-view { min-height: 60vh; display: grid; place-items: center; text-align: center; gap: 0.75rem; align-content: center; }
.spinner {
  width: 32px; height: 32px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--brand);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.login-card {
  max-width: 480px; padding: 2.25rem 2rem; background: var(--surface);
  border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 0.5rem; font-size: 1.75rem; line-height: 1.2; }
.lead { margin: 0 0 1.5rem; color: var(--muted); }
.hint { margin: 1rem 0 0; color: var(--muted); font-size: 0.85rem; }
.alert { margin: 0 0 1rem; padding: 0.65rem 0.85rem; border-radius: var(--radius-sm); background: #fbeceb; color: var(--danger); font-size: 0.92rem; }

/* ---------- barra de ferramentas ---------- */
.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.month-nav { display: flex; align-items: center; gap: 0.75rem; }
.month-title { margin: 0; min-width: 11ch; text-align: center; font-size: 1.45rem; }

/* ---------- indicadores ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0.85rem; margin-bottom: 1.25rem; }
.kpi {
  display: flex; flex-direction: column; gap: 0.15rem; padding: 1rem 1.1rem;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.kpi-label { color: var(--muted); font-size: 0.85rem; font-weight: 600; }
.kpi-value { font-size: 2rem; font-weight: 700; line-height: 1.15; font-variant-numeric: tabular-nums; }
.kpi-sub { color: var(--muted); font-size: 0.8rem; }

/* ---------- formulário ---------- */
.add-form {
  display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap;
  padding: 0.85rem; margin-bottom: 1rem; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius);
}
.add-form input[type="text"], .dialog input[type="text"] {
  flex: 1 1 260px; min-height: 42px; padding: 0.5rem 0.8rem; font: inherit;
  border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--text);
}
.swatches { display: flex; gap: 0.4rem; margin: 0; padding: 0; border: 0; }
.swatch { position: relative; }
.swatch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.swatch span {
  display: block; width: 26px; height: 26px; border-radius: 50%; background: var(--c);
  border: 3px solid var(--surface); box-shadow: 0 0 0 1px var(--line);
}
.swatch input:checked + span { box-shadow: 0 0 0 2px var(--c); }
.swatch input:focus-visible + span { outline: 3px solid #f0b429; outline-offset: 2px; }

/* ---------- grade do mês ---------- */
.grid-wrap {
  overflow-x: auto; background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); -webkit-overflow-scrolling: touch;
}
.grid { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
.grid th, .grid td { padding: 0; text-align: center; border-bottom: 1px solid var(--line); }
.grid tbody tr:last-child td, .grid tbody tr:last-child th { border-bottom: 0; }

.grid thead th { position: sticky; top: 0; background: var(--surface); z-index: 2; font-weight: 600; }
.day-head { width: var(--cell); min-width: var(--cell); padding: 0.45rem 0 0.35rem; font-size: 0.82rem; }
.day-head .wd { display: block; color: var(--muted); font-size: 0.68rem; font-weight: 500; text-transform: uppercase; }
.day-head.weekend, .day-cell.weekend { background: #faf9f6; }
.day-head.today { background: var(--today); color: #7a5300; }
.day-cell.today { background: var(--today); }

.grid .name-head, .grid .name-cell {
  position: sticky; left: 0; z-index: 1; width: var(--name-col); min-width: var(--name-col); max-width: var(--name-col);
  background: var(--surface); text-align: left; border-right: 1px solid var(--line);
}
.grid .name-head { z-index: 3; padding: 0.5rem 0.9rem; color: var(--muted); font-size: 0.82rem; }
.grid .name-cell { padding: 0.65rem 0.9rem 0.7rem; }
.habit-top { display: flex; align-items: center; gap: 0.5rem; }
.habit-dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.habit-name { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.edit-btn {
  flex: none; border: 0; background: transparent; color: var(--muted); cursor: pointer;
  border-radius: 6px; padding: 0.1rem 0.4rem; font: inherit; font-size: 1rem; line-height: 1.4;
}
.edit-btn:hover { background: var(--surface-2); color: var(--text); }

.bar { height: 8px; margin: 0.45rem 0 0.35rem; border-radius: 99px; background: var(--c-soft); overflow: hidden; }
.bar > span {
  display: block; height: 100%; width: var(--p, 0%); border-radius: inherit; background: var(--c);
  transition: width 0.35s ease;
}
.habit-stats { display: flex; gap: 0.75rem; color: var(--muted); font-size: 0.78rem; font-variant-numeric: tabular-nums; }
.habit-stats b { color: var(--text); font-weight: 700; }

.day-cell { height: 52px; }
.check {
  appearance: none; -webkit-appearance: none; margin: 0; display: block; margin-inline: auto;
  width: 24px; height: 24px; border-radius: 7px; border: 2px solid #cfcabd; background: #fff;
  cursor: pointer; transition: background-color 0.12s, border-color 0.12s, transform 0.08s;
}
.check:hover:not(:disabled) { border-color: var(--c); }
.check:active:not(:disabled) { transform: scale(0.92); }
.check:checked {
  border-color: var(--c); background-color: var(--c);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M6 12.5l4 4L18 8' stroke='white' stroke-width='3' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-size: 100% 100%;
}
.check:disabled { cursor: not-allowed; background: var(--surface-2); border-color: var(--line); }
.check.pending { opacity: 0.55; }

.empty { margin-top: 1rem; padding: 2rem; text-align: center; color: var(--muted); background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty p { margin: 0.2rem 0; }

/* ---------- diálogo ---------- */
.dialog {
  width: min(440px, calc(100vw - 2rem)); padding: 1.5rem; border: 1px solid var(--line);
  border-radius: 18px; box-shadow: var(--shadow); color: var(--text);
}
.dialog::backdrop { background: rgba(29, 35, 33, 0.4); }
.dialog h2 { margin: 0 0 1rem; font-size: 1.2rem; }
.dialog label { display: block; margin-bottom: 0.35rem; font-weight: 600; font-size: 0.9rem; }
.dialog input[type="text"] { width: 100%; margin-bottom: 1rem; }
.dialog-actions { display: flex; align-items: center; gap: 0.5rem; margin-top: 1.25rem; }
.spacer { flex: 1; }

/* ---------- aviso ---------- */
.toast {
  position: fixed; left: 50%; bottom: 1.25rem; transform: translateX(-50%); z-index: 50;
  max-width: calc(100vw - 2rem); padding: 0.7rem 1.1rem; border-radius: 10px;
  background: #1d2321; color: #fff; box-shadow: var(--shadow); font-size: 0.92rem;
}
.toast.error { background: var(--danger); }

/* ---------- rodapé ---------- */
.footer {
  padding: 1.1rem 1.25rem; text-align: center; color: var(--muted);
  font-size: 0.85rem; border-top: 1px solid var(--line); background: var(--surface);
}
.footer p { margin: 0.15rem 0; }
.footer code { font-family: var(--mono); color: var(--text); background: var(--surface-2); padding: 0.1rem 0.4rem; border-radius: 6px; }

/* ---------- responsivo ---------- */
@media (max-width: 860px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  :root { --name-col: 190px; }
}
@media (max-width: 520px) {
  main { padding: 1rem 0.75rem 1.5rem; }
  .user-text { display: none; }
  .kpi { padding: 0.8rem; }
  .kpi-value { font-size: 1.6rem; }
  .month-title { font-size: 1.2rem; }
  .add-form .btn { flex: 1 1 100%; }
  :root { --name-col: 150px; --cell: 38px; }
  .habit-stats { flex-direction: column; gap: 0; }
  .habit-top { align-items: flex-start; }
  .habit-dot { margin-top: 0.45rem; }
  .habit-name { white-space: normal; overflow-wrap: anywhere; font-size: 0.92rem; line-height: 1.3; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
