:root {
  --green: #2f8f4e;
  --green-dark: #246e3d;
  --green-soft: #eaf6ee;
  --bg: #f5f7f5;
  --surface: #ffffff;
  --surface-2: #f0f3f1;
  --text: #16231a;
  --text-soft: #5b6b60;
  --border: #dfe6e0;
  --danger: #c0392b;
  --danger-soft: #fbeae8;
  --shadow: 0 2px 10px rgba(20, 50, 30, 0.08);
  --radius: 14px;
  --radius-sm: 9px;
  --maxw: 760px;
}

[data-theme="dark"] {
  --green: #46bd6c;
  --green-dark: #2f8f4e;
  --green-soft: #16321f;
  --bg: #0f1512;
  --surface: #182019;
  --surface-2: #1f2a22;
  --text: #e9f1ea;
  --text-soft: #9bb0a2;
  --border: #2b3a30;
  --danger: #e76a5c;
  --danger-soft: #321c19;
  --shadow: 0 2px 14px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom);
  overflow-wrap: break-word;
  word-break: break-word;
}

h1, h2, h3 { line-height: 1.25; }

button { font-family: inherit; }

.hidden { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: -999px; top: 0; z-index: 100;
  background: var(--green); color: #fff; padding: 10px 14px; border-radius: 0 0 8px 0;
}
.skip-link:focus { left: 0; }

:focus-visible {
  outline: 3px solid var(--green);
  outline-offset: 2px;
  border-radius: 6px;
}

/* HEADER */
.app-header {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px;
  background: linear-gradient(135deg, var(--green), var(--green-dark));
  color: #fff;
  box-shadow: var(--shadow);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center;
  width: 42px; height: 42px; border-radius: 12px;
  background: rgba(255, 255, 255, 0.18);
  flex: 0 0 auto;
}
.brand-text { min-width: 0; }
.brand-text h1 { font-size: 1.18rem; margin: 0; }
.brand-sub { margin: 0; font-size: 0.78rem; opacity: 0.85; }
.header-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.icon-btn {
  display: grid; place-items: center;
  width: 40px; height: 40px; border-radius: 10px;
  background: rgba(255, 255, 255, 0.15);
  color: inherit; border: none; cursor: pointer; font-size: 1.2rem;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.28); }

.ico-moon { display: none; }
[data-theme="dark"] .ico-sun { display: none; }
[data-theme="dark"] .ico-moon { display: block; }

/* REMINDER BAR */
.reminder-bar {
  max-width: var(--maxw); margin: 12px auto 0; padding: 12px 16px;
  background: var(--green-soft); color: var(--text);
  border-left: 5px solid var(--green); border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  font-size: 0.95rem;
}
.reminder-bar strong { color: var(--green-dark); }

/* TABS */
.tabs {
  position: sticky; top: 66px; z-index: 15;
  display: flex; gap: 4px; max-width: var(--maxw); margin: 14px auto 0;
  padding: 4px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 999px; box-shadow: var(--shadow);
}
.tabs { top: calc(env(safe-area-inset-top) + 70px); }
.tab {
  flex: 1; border: none; background: transparent; color: var(--text-soft);
  padding: 9px 6px; border-radius: 999px; cursor: pointer; font-size: 0.86rem;
  font-weight: 600; white-space: nowrap;
}
.tab.is-active { background: var(--green); color: #fff; }

/* CONTENT */
.content { max-width: var(--maxw); margin: 0 auto; padding: 16px 16px 40px; }
.view { animation: fade 0.18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.section-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  flex-wrap: wrap; margin-bottom: 10px;
}
.section-head h2 { font-size: 1.25rem; margin: 0; }
.muni-tag { font-size: 0.85rem; color: var(--text-soft); }
.hint { color: var(--text-soft); font-size: 0.86rem; margin: 6px 0 14px; }

/* BUTTONS */
.btn {
  border: 1px solid transparent; border-radius: var(--radius-sm); cursor: pointer;
  padding: 11px 16px; font-size: 0.92rem; font-weight: 600; line-height: 1;
}
.btn-sm { padding: 8px 12px; font-size: 0.83rem; }
.btn-primary { background: var(--green); color: #fff; }
.btn-primary:hover { background: var(--green-dark); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(0.92); }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }

/* NEXT LIST */
.next-list { display: flex; flex-direction: column; gap: 10px; }
.next-card {
  display: flex; align-items: center; gap: 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 6px solid var(--c, var(--green));
  border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.next-card.is-today { outline: 2px solid var(--c, var(--green)); }
.nc-icon {
  flex: 0 0 auto; width: 46px; height: 46px; border-radius: 12px;
  display: grid; place-items: center; color: #fff;
}
.nc-icon svg { width: 26px; height: 26px; }
.nc-main { flex: 1; min-width: 0; }
.nc-name { font-weight: 700; font-size: 1.02rem; }
.nc-date { color: var(--text-soft); font-size: 0.88rem; }
.nc-badge {
  flex: 0 0 auto; font-size: 0.78rem; font-weight: 700; padding: 5px 10px;
  border-radius: 999px; background: var(--surface-2); color: var(--text-soft); white-space: nowrap;
}
.nc-badge.urgent { background: var(--green-soft); color: var(--green-dark); }
[data-theme="dark"] .nc-badge.urgent { color: var(--green); }

.empty {
  text-align: center; color: var(--text-soft); padding: 40px 16px;
  background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius);
}
.empty p { margin: 8px 0; }

/* TYPE LIST */
.type-list { display: flex; flex-direction: column; gap: 10px; }
.type-card {
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border);
  border-left: 6px solid var(--c, var(--green));
  border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow);
}
.tc-icon {
  flex: 0 0 auto; width: 40px; height: 40px; border-radius: 10px;
  display: grid; place-items: center; color: #fff;
}
.tc-icon svg { width: 22px; height: 22px; }
.tc-main { flex: 1; min-width: 0; }
.tc-name { font-weight: 700; }
.tc-rule { color: var(--text-soft); font-size: 0.84rem; }
.tc-actions { display: flex; gap: 6px; flex: 0 0 auto; }
.mini-btn {
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  border-radius: 8px; width: 36px; height: 36px; cursor: pointer; display: grid; place-items: center;
}
.mini-btn:hover { background: var(--green-soft); }
.mini-btn.del:hover { background: var(--danger-soft); color: var(--danger); }
.mini-btn svg { width: 18px; height: 18px; }

/* CARDS / FORMS */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: var(--shadow);
}
.card h3 { margin: 0 0 10px; font-size: 1.02rem; }
.field-label { display: block; font-size: 0.82rem; font-weight: 600; color: var(--text-soft); margin: 0 0 6px; }
.field-inline { margin-top: 12px; }
.input {
  width: 100%; padding: 11px 12px; font-size: 0.95rem;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--text);
}
.input-sm { max-width: 150px; }
.input-color { padding: 4px; height: 46px; }
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 4px 0 10px; }
.switch-row input { width: 22px; height: 22px; accent-color: var(--green); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* CALENDAR */
.cal-toolbar { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.cal-title { flex: 1; text-align: center; font-size: 1.1rem; margin: 0; }
.cal-toolbar .icon-btn { background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.cal-toolbar .icon-btn:hover { background: var(--surface-2); }
.cal-weekdays { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 4px; }
.cal-weekdays span { text-align: center; font-size: 0.74rem; font-weight: 700; color: var(--text-soft); }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.cal-cell {
  aspect-ratio: 1 / 1; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); padding: 4px; cursor: pointer; position: relative;
  display: flex; flex-direction: column; min-width: 0;
}
.cal-cell.empty-cell { background: transparent; border-color: transparent; cursor: default; }
.cal-cell.is-today { border-color: var(--green); border-width: 2px; }
.cal-cell.selected { background: var(--green-soft); }
.cal-num { font-size: 0.8rem; font-weight: 600; }
.cal-cell.is-today .cal-num { color: var(--green-dark); }
[data-theme="dark"] .cal-cell.is-today .cal-num { color: var(--green); }
.cal-dots { display: flex; flex-wrap: wrap; gap: 2px; margin-top: auto; }
.cal-dot { width: 7px; height: 7px; border-radius: 50%; }
.cal-day-detail {
  margin-top: 14px; background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; min-height: 30px; box-shadow: var(--shadow);
}
.cal-day-detail h4 { margin: 0 0 8px; font-size: 0.95rem; }
.dd-item { display: flex; align-items: center; gap: 10px; padding: 6px 0; }
.dd-swatch { width: 14px; height: 14px; border-radius: 4px; flex: 0 0 auto; }
.dd-empty { color: var(--text-soft); font-size: 0.9rem; }

/* RULE BOX */
.rule-box { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 14px; margin-top: 14px; }
.rule-box legend { padding: 0 8px; font-weight: 700; font-size: 0.9rem; }
.rule-field { margin-top: 12px; }
.date-add-row { display: flex; gap: 8px; align-items: center; }
.date-add-row .input { flex: 1; }
.dates-list { list-style: none; margin: 10px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.dates-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  background: var(--surface-2); border-radius: 8px; padding: 8px 12px; font-size: 0.9rem;
}
.dates-list button {
  border: none; background: transparent; color: var(--danger); cursor: pointer;
  font-size: 1rem; padding: 0 4px;
}

/* MODAL */
.modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(10, 20, 14, 0.55);
  display: grid; place-items: center; padding: 16px;
  animation: fade 0.15s ease;
}
.modal {
  background: var(--surface); border-radius: var(--radius); width: 100%;
  max-width: 520px; max-height: 90vh; overflow: auto; box-shadow: 0 18px 50px rgba(0,0,0,0.3);
}
.modal-sm { max-width: 400px; }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--surface); z-index: 1;
}
.modal-head h3 { margin: 0; font-size: 1.1rem; }
.modal-close { background: var(--surface-2); color: var(--text); }
.modal-close:hover { background: var(--danger-soft); color: var(--danger); }
.modal-body { padding: 16px; }
.modal-body .field-label { margin-top: 14px; }
.modal-body .field-label:first-of-type { margin-top: 0; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; }
.confirm-text { margin: 0 0 4px; }

/* TOAST */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 20px);
  transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg);
  padding: 12px 18px; border-radius: 999px; font-size: 0.9rem; font-weight: 600;
  box-shadow: 0 8px 26px rgba(0,0,0,0.3); max-width: 90vw; text-align: center;
  animation: toastIn 0.2s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

.app-foot { text-align: center; color: var(--text-soft); font-size: 0.78rem; margin-top: 20px; }

@media (max-width: 420px) {
  .brand-text h1 { font-size: 1.05rem; }
  .tab { font-size: 0.8rem; padding: 9px 4px; }
  .grid-2 { grid-template-columns: 1fr; }
}

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