*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  max-width: 100%;
  min-height: 100%;
  overflow-x: clip;
  background: var(--bg-canvas);
  color: var(--text-primary);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  touch-action: manipulation;
}

body {
  background: var(--bg-canvas);
}

button,
input,
select,
textarea {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

button {
  cursor: pointer;
  border: none;
  background: none;
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Custom subtle dark scrollbar */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: rgba(165, 173, 203, 0.24);
  border-radius: 999px;
}
::-webkit-scrollbar-thumb:hover {
  background: rgba(165, 173, 203, 0.42);
}

.app-root {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.boot-screen {
  min-height: 100dvh;
  display: grid;
  place-items: center;
}

.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--border-subtle);
  border-top-color: var(--accent-hover);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Top Navigation Bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--bg-canvas);
  border-bottom: 1px solid var(--border-subtle);
  padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 12px max(16px, env(safe-area-inset-left));
}

.topbar-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, rgba(198, 160, 246, 0.22), rgba(145, 215, 227, 0.16));
  border: 1px solid rgba(198, 160, 246, 0.35);
}

.brand-text {
  min-width: 0;
}

.brand-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 650;
  letter-spacing: -0.015em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-subtitle {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* Account Menu Dropdown */
.account-menu {
  position: relative;
}

.account-menu-trigger {
  padding: 0 12px;
  gap: 8px;
}

.account-menu-name {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-menu-caret {
  display: inline-flex;
  color: var(--text-secondary);
  transition: transform 0.15s ease;
}

.account-menu-trigger[aria-expanded="true"] .account-menu-caret {
  transform: rotate(180deg);
  color: var(--text-primary);
}

.account-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 215px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-modal);
  padding: 6px;
  z-index: 45;
  display: flex;
  flex-direction: column;
  gap: 2px;
  animation: fadeIn 0.12s ease;
}

.account-menu-header {
  padding: 8px 10px 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.account-menu-label {
  font-size: 0.72rem;
  color: var(--text-secondary);
}

.account-menu-username {
  font-size: 0.9rem;
  font-weight: 620;
  color: var(--text-primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-menu-divider {
  height: 1px;
  background: var(--border-subtle);
  margin: 4px 2px;
}

.account-menu-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 520;
  color: var(--text-primary);
  text-align: left;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.account-menu-item:hover {
  background: var(--bg-hover);
}

.account-menu-item-danger {
  color: var(--rose);
}

.account-menu-item-danger:hover {
  background: var(--rose-soft);
  color: var(--rose);
}

/* Main Responsive Dashboard Grid */
.dashboard-main {
  flex: 1;
  width: 100%;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 18px max(16px, env(safe-area-inset-right)) max(28px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.widget-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--gap);
  align-items: start;
  width: 100%;
}

.widget-grid > * {
  min-width: 0;
  max-width: 100%;
}

@media (min-width: 960px) {
  .widget-grid {
    grid-template-columns: repeat(12, minmax(0, 1fr));
  }
  .widget-span-5 {
    grid-column: span 5;
  }
  .widget-span-6 {
    grid-column: span 6;
  }
  .widget-span-7 {
    grid-column: span 7;
  }
  .widget-span-12 {
    grid-column: span 12;
  }
}

/* Shared Widget Card */
.widget-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.widget-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border-subtle);
  background: rgba(255, 255, 255, 0.015);
}

.widget-title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}

.widget-title-wrap > div {
  min-width: 0;
}

.widget-header .btn {
  flex-shrink: 0;
}

.widget-icon {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.widget-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.widget-subtitle {
  margin: 0;
  font-size: 0.78rem;
  color: var(--text-secondary);
}

.widget-body {
  padding: 16px 18px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 550;
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.08s ease;
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 650;
  border: 1px solid rgba(198, 160, 246, 0.35);
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
}

.btn-secondary:hover {
  background: var(--bg-elevated);
  border-color: var(--border-strong);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid transparent;
}

.btn-ghost:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-danger {
  background: var(--rose-soft);
  color: var(--rose);
  border: 1px solid rgba(237, 135, 150, 0.3);
}

.btn-danger:hover {
  background: rgba(237, 135, 150, 0.26);
}

.btn-icon {
  width: 38px;
  min-width: 38px;
  padding: 0;
}

@media (max-width: 600px) {
  .topbar {
    padding: max(10px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 10px max(12px, env(safe-area-inset-left));
  }
  .topbar-inner {
    gap: 8px;
  }
  .brand {
    gap: 9px;
  }
  .brand-mark {
    width: 32px;
    height: 32px;
    border-radius: 9px;
  }
  .brand-mark img {
    width: 20px;
    height: 20px;
  }
  .brand-title {
    font-size: 0.94rem;
  }
  .brand-subtitle {
    font-size: 0.73rem;
  }
  .topbar-actions {
    gap: 5px;
  }
  .topbar-actions .btn {
    width: 36px;
    min-width: 36px;
    min-height: 36px;
    padding: 0;
  }
  .topbar-actions .account-menu-trigger {
    width: auto;
    min-width: 38px;
    min-height: 36px;
    padding: 0 10px;
  }
  .account-menu-name {
    max-width: 92px;
  }
  .btn {
    min-height: var(--touch-min);
  }
  .btn-icon {
    width: var(--touch-min);
    min-width: var(--touch-min);
  }
  .widget-header .btn {
    padding: 0 11px;
    font-size: 0.84rem;
    min-height: 36px;
  }
  .hide-mobile {
    display: none;
  }
  .modal-backdrop {
    padding: 12px;
  }
  .modal-header,
  .modal-body,
  .modal-footer {
    padding-left: 16px;
    padding-right: 16px;
  }
  .toast-root {
    left: 12px;
    right: 12px;
    align-items: center;
  }
}

/* Form Controls */
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.82rem;
  font-weight: 550;
  color: var(--text-secondary);
}

.input,
.select,
.textarea {
  width: 100%;
  min-width: 0;
  max-width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text-primary);
  /* 16px minimum prevents iOS Safari from auto-zooming on input focus */
  font-size: 16px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="date"].input,
input[type="time"].input {
  -webkit-appearance: none;
  appearance: none;
  min-height: 44px;
}

.input:focus,
.select:focus,
.textarea:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px rgba(198, 160, 246, 0.2);
}

.textarea {
  resize: vertical;
  min-height: 76px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 480px) {
  .form-row {
    grid-template-columns: 1fr;
  }
}

.form-error {
  color: var(--rose);
  font-size: 0.85rem;
  background: var(--rose-soft);
  border: 1px solid rgba(237, 135, 150, 0.3);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(24, 25, 38, 0.76);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 16px;
  animation: fadeIn 0.14s ease;
}

.modal-panel {
  width: 100%;
  max-width: 470px;
  max-height: 90dvh;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-subtle);
}

.modal-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
}

.modal-body {
  padding: 18px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid var(--border-subtle);
}

.modal-footer-split {
  justify-content: space-between;
}

/* Custom Themed Date Picker */
.dp-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 44px;
  text-align: left;
  cursor: pointer;
}

.dp-trigger:hover {
  border-color: var(--border-strong);
}

.dp-trigger-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dp-trigger-text.is-placeholder {
  color: var(--text-muted);
}

.dp-trigger-icon {
  display: inline-flex;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.dp-backdrop {
  position: fixed;
  inset: 0;
  z-index: 75;
  background: rgba(24, 25, 38, 0.72);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  padding: 16px;
  animation: fadeIn 0.12s ease;
}

.dp-card {
  width: 100%;
  max-width: 328px;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-modal);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: slideUp 0.14s ease;
}

.dp-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.dp-nav {
  width: 36px;
  min-width: 36px;
  min-height: 36px;
}

.dp-selects {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.dp-select {
  background: var(--bg-surface-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  /* 16px prevents iOS Safari auto-zoom when tapping Month/Year */
  font-size: 16px;
  font-weight: 600;
  padding: 5px 7px;
  min-height: 36px;
  outline: none;
  cursor: pointer;
}

.dp-select:focus {
  border-color: var(--border-focus);
  box-shadow: 0 0 0 2px rgba(198, 160, 246, 0.22);
}

.dp-weekdays {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 4px;
}

.dp-weekday {
  text-align: center;
  font-size: 0.74rem;
  font-weight: 600;
  color: var(--text-muted);
  padding: 2px 0;
}

.dp-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 4px;
}

.dp-day {
  height: 38px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 0.88rem;
  font-weight: 520;
  font-variant-numeric: tabular-nums;
  color: var(--text-primary);
  border: 1px solid transparent;
  transition: background-color 0.12s ease, border-color 0.12s ease, color 0.12s ease, transform 0.06s ease;
}

.dp-day:hover {
  background: var(--bg-surface-2);
  border-color: var(--border-subtle);
}

.dp-day:active {
  transform: scale(0.95);
}

.dp-day.is-outside {
  color: var(--text-muted);
  opacity: 0.52;
}

.dp-day.today {
  background: var(--accent-soft);
  border-color: rgba(198, 160, 246, 0.45);
  color: var(--accent);
  font-weight: 680;
}

.dp-day.selected {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-fg);
  font-weight: 700;
  opacity: 1;
}

.dp-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid var(--border-subtle);
}

.dp-foot-btn {
  min-height: 34px;
  padding: 0 12px;
  font-size: 0.84rem;
}

/* Toasts */
.toast-root {
  position: fixed;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 80;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  background: var(--bg-elevated);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-md);
  padding: 10px 14px;
  font-size: 0.88rem;
  box-shadow: var(--shadow-card);
  animation: slideUp 0.18s ease;
}

.toast.error {
  border-color: rgba(237, 135, 150, 0.45);
  color: var(--rose);
}

.toast-undo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.toast-undo-btn {
  padding: 3px 9px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 650;
  border: 1px solid rgba(198, 160, 246, 0.32);
  cursor: pointer;
}

.toast-undo-btn:hover {
  background: rgba(198, 160, 246, 0.24);
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* Badges & Pills */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.74rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: var(--bg-surface-2);
  color: var(--text-secondary);
  border: 1px solid var(--border-subtle);
}

.badge-high {
  background: var(--rose-soft);
  color: var(--rose);
  border-color: rgba(237, 135, 150, 0.3);
}

.badge-medium {
  background: var(--amber-soft);
  color: var(--amber);
  border-color: rgba(238, 212, 159, 0.3);
}

.badge-low {
  background: var(--cyan-soft);
  color: var(--cyan);
  border-color: rgba(145, 215, 227, 0.3);
}

.badge-accent {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: rgba(198, 160, 246, 0.32);
}

.badge-success {
  background: var(--emerald-soft);
  color: var(--emerald);
  border-color: rgba(166, 218, 149, 0.34);
}

/* Account & Auth helpers */
.auth-field-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.auth-field-head label {
  margin: 0;
}

.auth-info-box {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  border: 1px solid rgba(198, 160, 246, 0.28);
  color: var(--text-primary);
  font-size: 0.84rem;
  line-height: 1.4;
}

.acct-box {
  padding: 12px 14px;
  border-radius: var(--radius-md);
  background: rgba(255, 255, 255, 0.025);
  border: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.acct-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.acct-check {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--text-primary);
  user-select: none;
}

.acct-check input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}

.acct-method-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

@media (max-width: 420px) {
  .acct-method-grid {
    grid-template-columns: 1fr;
  }
}

.acct-method-btn {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface-2);
  color: var(--text-secondary);
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: border-color 0.15s ease, background-color 0.15s ease, color 0.15s ease;
}

.acct-method-btn strong {
  font-size: 0.88rem;
  color: var(--text-primary);
}

.acct-method-btn span {
  font-size: 0.76rem;
  color: var(--text-secondary);
}

.acct-method-btn.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.acct-method-btn.active strong {
  color: var(--accent);
}

.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;
}
