/* ==========================================================================
   Prumo: tokens de cor (claro por padrão; escuro via sistema ou escolha manual)
   ========================================================================== */

:root {
  color-scheme: light;
  --bg: #f7f6fc;
  --surface: #ffffff;
  --border: #e3e0f0;
  --text: #1a1830;
  --text-muted: #5f5c7a;
  --cell-empty: #e9e7f2;
  --primary: #6d28d9;
  --primary-contrast: #ffffff;
  --primary-soft: #ede7fb;
  --alert: #b45309;
  --alert-soft: #fdf1e2;
  --danger: #b91c1c;
  --danger-soft: #fbe9e9;
  --success: #15803d;
  --shadow: 0 8px 24px rgb(26 24 48 / 0.12);

  --brand-gradient: linear-gradient(135deg, #8b5cf6 0%, #3b82f6 100%);
  --radius: 12px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0b0a1a;
    --surface: #15132b;
    --border: #2a2748;
    --text: #ecebf7;
    --text-muted: #9a97b8;
    --cell-empty: #221f3d;
    --primary: #a78bfa;
    --primary-contrast: #1a1033;
    --primary-soft: #251f4a;
    --alert: #fbbf24;
    --alert-soft: #2e2512;
    --danger: #f87171;
    --danger-soft: #2f1719;
    --success: #4ade80;
    --shadow: 0 8px 24px rgb(0 0 0 / 0.45);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0b0a1a;
  --surface: #15132b;
  --border: #2a2748;
  --text: #ecebf7;
  --text-muted: #9a97b8;
  --cell-empty: #221f3d;
  --primary: #a78bfa;
  --primary-contrast: #1a1033;
  --primary-soft: #251f4a;
  --alert: #fbbf24;
  --alert-soft: #2e2512;
  --danger: #f87171;
  --danger-soft: #2f1719;
  --success: #4ade80;
  --shadow: 0 8px 24px rgb(0 0 0 / 0.45);
}

/* ==========================================================================
   Base
   ========================================================================== */

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

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

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

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

h1,
h2,
h3 {
  line-height: 1.25;
  margin: 0 0 0.5em;
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.muted {
  color: var(--text-muted);
}

.capitalize {
  display: inline-block;
}

.capitalize::first-letter {
  text-transform: uppercase;
}

/* ==========================================================================
   Layout
   ========================================================================== */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.topbar::before {
  /* fio de identidade roxo → azul */
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 3px;
  background: var(--brand-gradient);
}

.brand {
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  text-decoration: none;
  background: var(--brand-gradient);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.topbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 4px;
}

.topbar-actions form {
  margin: 0;
}

.container {
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}

.stack > * + * {
  margin-top: 16px;
}

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

.row form {
  margin: 0;
}

.login-card {
  max-width: 400px;
  margin: 32px auto 0;
}

/* ==========================================================================
   Hábitos de hoje
   ========================================================================== */

.today-header h1 {
  margin: 0;
  font-size: 1.4rem;
}

.today-header h1::first-letter {
  text-transform: uppercase;
}

.today-header p {
  margin: 4px 0 0;
}

.habit-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.habit {
  /* --habit vem do style inline com a cor do hábito */
  scroll-margin-top: 80px; /* ao chegar por #habit-N, não fica atrás do topo fixo */
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 14px 16px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.habit-row {
  position: relative;
  display: flex;
  align-items: center;
  gap: 14px;
  border-radius: var(--radius-sm);
}

@media (hover: hover) {
  .habit-row:hover .habit-check:not(:disabled) {
    background: color-mix(in srgb, var(--habit) 25%, transparent);
  }

  .habit.is-done .habit-row:hover .habit-check {
    background: var(--habit);
  }
}

.habit-check {
  flex: none;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  box-shadow: inset 0 0 0 2px var(--habit);
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s;
}

/* A linha de cima inteira (nome + critério) é a área de toque do botão; a grade não. */
.habit-check::after {
  content: "";
  position: absolute;
  inset: -6px;
  border-radius: var(--radius-sm);
}

.habit-check:active {
  transform: scale(0.92);
}

.habit-check:disabled {
  cursor: progress;
  opacity: 0.6;
}

.habit.is-done .habit-check {
  background: var(--habit);
}

.habit-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.habit-name {
  font-weight: 600;
}

.habit-minimum {
  font-size: 0.9rem;
}

.habit-info {
  flex: 1;
}

.habit-streak {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  line-height: 1.2;
  text-align: right;
}

.streak-main {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.streak-main strong {
  font-size: 1.35rem;
  color: var(--text);
}

.streak-strict {
  font-size: 0.75rem;
}

/* Regra "nunca falhe dois dias": ontem falhou e hoje está pendente. */
.habit.is-alert {
  border-color: var(--alert);
  box-shadow: 0 0 0 1px var(--alert);
}

.habit-alert {
  margin-top: 2px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--alert);
}

/* ==========================================================================
   Grade (colunas = semanas, de domingo a sábado; mais recente à direita)
   ========================================================================== */

.habit-grid {
  --weeks: 15;
  --gap: 3px;
  width: 100%;
  max-width: calc(var(--weeks) * 22px);
}

.grid-months,
.grid-cells {
  display: grid;
  grid-template-columns: repeat(var(--weeks), minmax(0, 1fr));
  column-gap: var(--gap);
}

.grid-months {
  margin-bottom: 4px;
  font-size: 0.7rem;
  line-height: 1;
  color: var(--text-muted);
}

.grid-months span {
  white-space: nowrap;
  overflow: visible;
}

.grid-cells {
  grid-template-rows: repeat(7, auto);
  grid-auto-flow: column;
  row-gap: var(--gap);
}

.cell {
  aspect-ratio: 1;
  border-radius: 3px;
  background: var(--cell-empty);
}

.cell-done {
  background: var(--habit);
}

.cell-miss-second {
  box-shadow: inset 0 0 0 1.5px var(--alert);
}

.cell-pending {
  background: transparent;
  box-shadow: inset 0 0 0 2px var(--habit);
}

.cell-paused {
  background: repeating-linear-gradient(
    135deg,
    var(--cell-empty) 0 2px,
    transparent 2px 4px
  );
}

.cell-neutral {
  background: transparent;
  box-shadow: inset 0 0 0 1px var(--border);
}

.cell-future {
  background: transparent;
}

/* ==========================================================================
   Página de estilo
   ========================================================================== */

.swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}

.swatch {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-family: ui-monospace, Consolas, monospace;
}

.swatch span {
  flex: none;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}

.cells-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.cells-preview figure {
  margin: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
}

.cells-preview .cell {
  width: 18px;
}

/* ==========================================================================
   Botões
   ========================================================================== */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 18px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
}

.btn:disabled,
.btn.htmx-request {
  opacity: 0.6;
  cursor: progress;
}

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

.btn-primary:hover {
  opacity: 0.9;
}

.btn-ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}

.btn-ghost:hover {
  background: var(--primary-soft);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-icon {
  min-width: 44px;
  padding: 0 10px;
  background: transparent;
  color: var(--text-muted);
  border-color: transparent;
}

.btn-icon:hover {
  color: var(--text);
  background: var(--primary-soft);
}

.btn-block {
  width: 100%;
}

.btn-sm {
  min-height: 36px;
  padding: 0 12px;
  font-size: 0.9rem;
}

/* ==========================================================================
   Formulários (validação sempre inline; forms usam novalidate)
   ========================================================================== */

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-weight: 600;
  font-size: 0.95rem;
}

.field input {
  min-height: 44px;
  padding: 0 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.field input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.field.has-error input {
  border-color: var(--danger);
}

.field-help {
  margin: 0;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.field-color input {
  width: 72px;
  padding: 4px;
  cursor: pointer;
}

.form-card {
  max-width: 480px;
  margin: 0 auto;
}

.form-card h1 {
  font-size: 1.4rem;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.field-error,
.form-error {
  color: var(--danger);
  font-size: 0.9rem;
  margin: 0;
}

.form-error {
  padding: 10px 12px;
  background: var(--danger-soft);
  border-radius: var(--radius-sm);
}

/* ==========================================================================
   Toasts
   ========================================================================== */

.toasts {
  position: fixed;
  z-index: 100;
  left: 16px;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
}

.toast {
  --toast-accent: var(--primary);
  pointer-events: auto;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  max-width: 420px;
  padding: 12px 12px 12px 16px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: 4px solid var(--toast-accent);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  animation: toast-in 0.2s ease-out;
}

.toast.is-leaving {
  animation: toast-out 0.2s ease-in forwards;
}

.toast-success { --toast-accent: var(--success); }
.toast-warning { --toast-accent: var(--alert); }
.toast-error { --toast-accent: var(--danger); }

.toast-message {
  flex: 1;
  margin: 0;
}

.toast-close {
  flex: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@keyframes toast-out {
  to { opacity: 0; transform: translateY(8px); }
}

/* ==========================================================================
   Modal de confirmação
   ========================================================================== */

.modal {
  width: calc(100% - 32px);
  max-width: 400px;
  padding: 24px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.modal::backdrop {
  background: rgb(5 4 15 / 0.6);
}

.modal-message {
  margin: 0 0 20px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

@media (prefers-reduced-motion: reduce) {
  .toast,
  .toast.is-leaving {
    animation: none;
  }
}

/* ==========================================================================
   Menu (gaveta lateral em <dialog>)
   ========================================================================== */

.drawer {
  /* padding 0: todo clique no próprio <dialog> é no fundo, e fecha o menu */
  margin: 0 0 0 auto;
  padding: 0;
  width: min(300px, 85vw);
  height: 100%;
  max-height: none;
  border: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.drawer[open] {
  display: flex;
  flex-direction: column;
  animation: drawer-in 0.2s ease-out;
}

.drawer::backdrop {
  background: rgb(5 4 15 / 0.6);
}

@keyframes drawer-in {
  from { transform: translateX(100%); }
  to { transform: none; }
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 12px 12px 20px;
  border-bottom: 1px solid var(--border);
}

.drawer-nav {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 4px;
}

.drawer-nav a,
.drawer-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  min-height: 48px;
  padding: 0 12px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.drawer-nav a:hover,
.drawer-item:hover {
  background: var(--primary-soft);
}

.drawer-nav a[aria-current="page"] {
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
}

.drawer-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
}

.drawer-footer form {
  margin: 0;
}

.drawer-item {
  color: var(--text-muted);
}

/* ==========================================================================
   Gerenciar hábitos
   ========================================================================== */

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.page-header h1 {
  margin: 0;
  font-size: 1.4rem;
}

/* Título + subtítulo à esquerda, ação à direita (sem quebrar a linha). */
.page-header > .today-header {
  flex: 1 1 0;
  min-width: 0;
}

.page-header:has(> .today-header) {
  align-items: flex-start;
  flex-wrap: nowrap;
}

.section-title {
  margin: 0;
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.stack > .section-help {
  margin-top: 4px;
  font-size: 0.85rem;
}

.manage-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
}

.manage-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.manage-item.is-paused {
  opacity: 0.75;
}

.manage-item.is-ghost {
  opacity: 0.4;
}

.manage-item.is-dragging {
  border-color: var(--primary);
  box-shadow: var(--shadow);
}

.drag-handle {
  flex: none;
  width: 32px;
  height: 44px;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: grab;
  touch-action: none; /* no celular, arrastar pela alça não rola a página */
}

.drag-handle:active {
  cursor: grabbing;
}

.habit-dot {
  flex: none;
  width: 14px;
  height: 14px;
  border-radius: 4px;
  background: var(--habit);
}

.manage-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 4px 0;
  font-size: 0.9rem;
  color: var(--text);
  text-decoration: none;
}

.manage-info:hover .habit-name {
  color: var(--primary);
}

.manage-info .habit-name {
  font-size: 1rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.manage-actions {
  flex: none;
  display: flex;
  gap: 6px;
}

.manage-actions form {
  margin: 0;
}

/* ==========================================================================
   Início: resumos dos módulos (somente leitura)
   ========================================================================== */

.summary-card {
  padding: 16px;
}

.summary-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.summary-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.summary-header h2 a {
  color: var(--text);
  text-decoration: none;
}

.summary-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.summary-list li + li {
  border-top: 1px solid var(--border);
}

.summary-item {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 0 12px;
}

.summary-top {
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Nome do hábito = botão de marcar (pendente) ou desmarcar com confirmação (cumprido). */
.summary-name {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  margin: 0 -6px;
  padding: 0 6px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}

.summary-name:hover {
  background: var(--primary-soft);
}

.summary-name:disabled {
  opacity: 0.6;
  cursor: progress;
}

.summary-check {
  flex: none;
  width: 18px;
  height: 18px;
  border-radius: 5px;
  box-shadow: inset 0 0 0 2px var(--habit);
}

.summary-item.is-done .summary-check {
  background: var(--habit);
}

.summary-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.summary-streak {
  flex: none;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.summary-streak strong {
  font-size: 1.05rem;
  color: var(--text);
}

.summary-row {
  display: grid;
  grid-template-columns: repeat(14, minmax(0, 1fr));
  gap: 4px;
  max-width: calc(14 * 26px);
}

.summary-item.is-alert .summary-streak strong {
  color: var(--alert);
}


.habit-yesterday {
  margin-top: 2px;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* ==========================================================================
   Balão dos quadradinhos
   ========================================================================== */

.cell[data-date] {
  cursor: pointer;
}

.cell.is-selected {
  outline: 2px solid var(--text);
  outline-offset: 1px;
}

.popover {
  position: absolute;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}

.popover[hidden],
.popover [hidden] {
  display: none;
}

.popover-label {
  margin: 0;
  font-size: 0.9rem;
  white-space: nowrap;
}

.popover-label::first-letter {
  text-transform: uppercase;
}

/* ==========================================================================
   Toast com ação ("Desfazer") e modal de formulário
   ========================================================================== */

.toast-action {
  flex: none;
  align-self: center;
  padding: 4px 8px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--primary);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.toast-action:hover {
  background: var(--primary-soft);
}

.modal-form {
  max-width: 440px;
}

.form-title {
  margin: 0 0 12px;
  font-size: 1.3rem;
}

/* ==========================================================================
   Página offline
   ========================================================================== */

.offline-card {
  max-width: 400px;
  margin: 48px auto 0;
  text-align: center;
}

.offline-icon {
  border-radius: 18px;
}

.offline-card h1 {
  font-size: 1.4rem;
}
