* { box-sizing: border-box; }

html {
  min-height: 100%;
  background: var(--bg);
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-width: 320px;
  min-height: 100svh;
  overflow-x: hidden;
  overflow-y: auto;
  background:
    radial-gradient(circle at 12% 7%, rgba(71, 171, 255, 0.22), transparent 34rem),
    radial-gradient(circle at 90% 12%, rgba(156, 137, 255, 0.2), transparent 30rem),
    linear-gradient(145deg, var(--bg), var(--bg-warm));
  color: var(--text);
  font-family: var(--font-sans);
  line-height: 1.5;
  text-rendering: optimizeLegibility;
}

button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button { color: inherit; }
a { color: var(--primary); }
img { max-width: 100%; }
audio { width: 100%; max-width: 32rem; }

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

.skip-link {
  position: fixed;
  z-index: 9999;
  inset-block-start: 8px;
  inset-inline-start: 8px;
  transform: translateY(-140%);
  padding: 0.8rem 1rem;
  border-radius: var(--radius-sm);
  background: var(--surface-strong);
  color: var(--text);
  box-shadow: var(--shadow-md);
}
.skip-link:focus { transform: translateY(0); }

.ambient {
  position: fixed;
  z-index: -1;
  width: 36rem;
  height: 36rem;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.28;
  animation: drift 18s ease-in-out infinite alternate;
}
.ambient-one { inset-block-start: -13rem; inset-inline-start: -10rem; background: #65bfff; }
.ambient-two { inset-block-end: -18rem; inset-inline-end: -8rem; background: #ad91ff; animation-delay: -7s; }
@keyframes drift { to { transform: translate3d(4rem, 3rem, 0) scale(1.08); } }

.glass-panel,
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: inset 0 1px 0 var(--border-highlight), var(--shadow-sm);
  -webkit-backdrop-filter: var(--blur);
  backdrop-filter: var(--blur);
}

.app-shell { min-height: 100svh; }

.sidebar {
  position: fixed;
  inset-block: 18px;
  inset-inline-start: 18px;
  z-index: 50;
  display: flex;
  width: var(--sidebar);
  flex-direction: column;
  padding: 1rem;
  border-radius: var(--radius-lg);
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem;
  color: var(--text);
  text-decoration: none;
}
.brand-mark {
  display: grid;
  width: 48px;
  height: 48px;
  place-items: center;
  border-radius: 17px;
  background: linear-gradient(145deg, var(--primary), var(--secondary));
  color: #fff;
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 800;
  box-shadow: 0 10px 26px rgba(21, 101, 192, 0.25);
}
.brand strong, .brand small { display: block; }
.brand strong { font-size: 1.12rem; }
.brand small { color: var(--text-muted); }

.nav-list { display: grid; min-height: 0; gap: 0.3rem; margin-top: 1.25rem; overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; }
.nav-link {
  display: flex;
  min-height: var(--tap);
  align-items: center;
  gap: 0.8rem;
  padding: 0.65rem 0.85rem;
  border-radius: 15px;
  color: var(--text-muted);
  font-weight: 650;
  text-decoration: none;
  transition: background var(--ease), color var(--ease), transform var(--ease);
}
.nav-link:hover { background: var(--surface-muted); color: var(--text); }
.nav-link[aria-current="page"] {
  background: linear-gradient(135deg, rgba(34, 127, 221, 0.16), rgba(116, 94, 204, 0.12));
  color: var(--primary-strong);
  box-shadow: inset 0 0 0 1px rgba(32, 120, 210, 0.14);
}
.nav-icon { width: 1.6rem; font-size: 1.15rem; text-align: center; }
.sidebar-footer { display: grid; gap: 0.7rem; margin-top: auto; }

.app-column {
  min-width: 0;
  min-height: 100svh;
  margin-inline-start: calc(var(--sidebar) + 54px);
}

.topbar {
  position: sticky;
  inset-block-start: 18px;
  z-index: 40;
  display: flex;
  max-width: var(--content);
  min-height: 76px;
  align-items: center;
  justify-content: space-between;
  margin: 18px auto 0;
  padding: 0.8rem 1rem 0.8rem 1.3rem;
  border-radius: var(--radius-md);
}
.topbar-title { margin: 0; font-family: var(--font-display); font-size: clamp(1.4rem, 2vw, 1.8rem); }
.topbar-actions { display: flex; align-items: center; gap: 0.5rem; }
.eyebrow {
  margin: 0 0 0.1rem;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 750;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.icon-button,
.profile-button {
  display: grid;
  width: var(--tap);
  min-width: var(--tap);
  height: var(--tap);
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface-strong);
  cursor: pointer;
}
.profile-button {
  background: linear-gradient(145deg, var(--primary), var(--secondary));
  color: #fff;
  font-weight: 800;
}

.button {
  display: inline-flex;
  min-height: var(--tap);
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  border: 1px solid transparent;
  border-radius: 14px;
  padding: 0.65rem 1rem;
  font-weight: 750;
  text-decoration: none;
  cursor: pointer;
  transition: transform var(--ease), box-shadow var(--ease), background var(--ease);
}
.button:hover:not(:disabled) { transform: translateY(-1px); }
.button:disabled { cursor: not-allowed; opacity: 0.55; }
.button-primary { background: linear-gradient(135deg, var(--primary), var(--primary-strong)); color: #fff; box-shadow: 0 10px 24px rgba(21, 101, 192, 0.2); }
.button-secondary { background: var(--surface-strong); border-color: var(--border); color: var(--text); }
.button-ghost { background: transparent; border-color: var(--border); }
.button-danger { background: linear-gradient(135deg, var(--danger), var(--danger-strong)); color: #fff; }
.button-success { background: linear-gradient(135deg, var(--teal), var(--success)); color: #fff; }
.button-block { width: 100%; }
.button-small { min-height: 40px; padding: 0.45rem 0.75rem; font-size: 0.9rem; }

.card {
  border-radius: var(--radius-md);
  padding: clamp(1rem, 2vw, 1.35rem);
}
.card-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.card-header h2, .card-header h3 { margin: 0; }
.card-title { margin: 0; font-family: var(--font-display); font-size: 1.14rem; }
.card-subtitle { margin: 0.2rem 0 0; color: var(--text-muted); }

.status-pill {
  display: inline-flex;
  width: fit-content;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 0.25rem 0.6rem;
  font-size: 0.78rem;
  font-weight: 800;
}
.status-safe { color: var(--success); background: color-mix(in srgb, var(--success) 10%, transparent); }
.status-warning { color: var(--warning); background: color-mix(in srgb, var(--warning) 10%, transparent); }
.status-danger { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.status-info { color: var(--primary-strong); background: color-mix(in srgb, var(--primary) 10%, transparent); }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.field { display: grid; gap: 0.4rem; }
.field-full { grid-column: 1 / -1; }
.field label, .field-label { font-weight: 750; }
.field small, .help-text { color: var(--text-muted); }
.field input, .field select, .field textarea, .setting-row select {
  width: 100%;
  min-height: var(--tap);
  border: 1px solid var(--border);
  border-radius: 13px;
  background: var(--surface-strong);
  color: var(--text);
  padding: 0.7rem 0.8rem;
}
.field textarea { min-height: 90px; resize: vertical; }
.field input:invalid:not(:placeholder-shown) { border-color: var(--danger); }
.input-with-button { display: flex; gap: 0.45rem; }
.input-with-button input { flex: 1; }

.empty-state, .error-state {
  display: grid;
  min-height: 180px;
  place-items: center;
  padding: 1.5rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius-md);
  color: var(--text-muted);
  text-align: center;
}
.empty-icon { font-size: 2rem; }

.app-dialog {
  width: min(92vw, 720px);
  max-height: min(88dvh, 900px);
  padding: 0;
  overflow: hidden;
  border-radius: var(--radius-lg);
  color: var(--text);
}
.app-dialog::backdrop { background: rgba(7, 18, 34, 0.55); -webkit-backdrop-filter: blur(7px); backdrop-filter: blur(7px); }
.dialog-shell {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: 100%;
  max-height: inherit;
  min-height: 0;
  overflow: hidden;
}
.dialog-header { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: 1.2rem 1.25rem; border-bottom: 1px solid var(--border); }
.dialog-header h2 { margin: 0; }
.dialog-body {
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.25rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
}
.dialog-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 0.65rem; padding: 1rem 1.25rem; border-top: 1px solid var(--border); }

.toast-region { position: fixed; inset-block-start: 1rem; inset-inline-end: 1rem; z-index: 5000; display: grid; gap: 0.65rem; width: min(90vw, 380px); }
.toast { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; padding: 0.9rem 1rem; border: 1px solid var(--border); border-radius: 15px; background: var(--surface-strong); box-shadow: var(--shadow-md); }
.toast[data-type="error"] { border-color: var(--danger); }

.offline-banner { position: fixed; inset-block-start: 0; inset-inline: 0; z-index: 6000; padding: 0.4rem 1rem; background: #6b3b00; color: #fff; text-align: center; }

.settings-stack { display: grid; gap: 0.7rem; }
.role-panel-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.role-panel { display: grid; min-height: 190px; place-items: center; gap: 0.75rem; border: 1px solid var(--border); border-radius: 22px; padding: 1.25rem; background: var(--surface-muted); color: var(--text); text-align: center; cursor: pointer; }
.role-panel:hover { border-color: var(--primary); transform: translateY(-2px); }
.role-panel strong, .role-panel small { display: block; }
.role-panel strong { font-size: 1.2rem; }
.role-panel small { margin-top: 0.35rem; color: var(--text-muted); }
.role-panel-icon { display: grid; width: 64px; height: 64px; place-items: center; border-radius: 20px; background: linear-gradient(145deg, var(--primary), var(--secondary)); color: #fff; font-size: 1.8rem; }
.role-panel-caregiver .role-panel-icon { background: linear-gradient(145deg, var(--teal), var(--success)); }
.setting-row { display: flex; min-height: 62px; align-items: center; justify-content: space-between; gap: 1rem; padding: 0.8rem; border: 1px solid var(--border); border-radius: 14px; }
.setting-row strong, .setting-row small { display: block; }
.setting-row small { color: var(--text-muted); }
.setting-row input[type="checkbox"] { width: 44px; height: 24px; }

.mobile-nav { display: none; }
.emergency-fab {
  position: fixed;
  inset-inline-end: 1.25rem;
  inset-block-end: 1.25rem;
  z-index: 100;
  width: 70px;
  height: 70px;
  border: 3px solid rgba(255, 255, 255, 0.82);
  border-radius: 50%;
  background: linear-gradient(145deg, var(--danger), var(--danger-strong));
  color: #fff;
  font-weight: 900;
  box-shadow: 0 15px 38px rgba(180, 35, 44, 0.38);
  cursor: pointer;
}

.skeleton { overflow: hidden; border-radius: 14px; background: linear-gradient(90deg, var(--surface-muted), var(--surface-strong), var(--surface-muted)); background-size: 200% 100%; animation: shimmer 1.3s infinite; }
.skeleton-title { width: 35%; height: 40px; }
.skeleton-card { height: 190px; }
@keyframes shimmer { to { background-position: -200% 0; } }

.noscript { margin: 2rem; padding: 1rem; border: 2px solid var(--danger); border-radius: 12px; background: #fff; color: #111; }

[dir="rtl"] .sidebar { inset-inline-start: 18px; }
[dir="rtl"] .app-column { margin-inline-start: calc(var(--sidebar) + 54px); }

@media (max-width: 1020px) {
  .sidebar { display: none; }
  .app-column { margin-inline-start: 0; }
  .topbar { margin-inline: 1rem; }
  .mobile-nav {
    position: fixed;
    inset-inline: 0.75rem;
    inset-block-end: max(0.75rem, env(safe-area-inset-bottom));
    z-index: 80;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    padding: 0.4rem;
    border-radius: 22px;
  }
  .mobile-nav .nav-link { display: grid; min-width: 0; gap: 0.1rem; place-items: center; padding: 0.35rem 0.15rem; font-size: 0.68rem; text-align: center; }
  .mobile-nav .nav-icon { width: auto; font-size: 1.1rem; }
  .emergency-fab { inset-block-end: calc(5.8rem + env(safe-area-inset-bottom)); width: 62px; height: 62px; }
}

@media (max-width: 640px) {
  .topbar { inset-block-start: 0.5rem; min-height: 66px; margin: 0.5rem; padding: 0.65rem 0.7rem 0.65rem 1rem; }
  .topbar-actions .icon-button { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .role-panel-grid { grid-template-columns: 1fr; }
  .role-panel { min-height: 140px; }
  .field-full { grid-column: auto; }
  .card { border-radius: 18px; }
  .dialog-actions { flex-direction: column-reverse; }
  .dialog-actions .button { width: 100%; }
  .app-dialog {
    width: calc(100vw - 1rem);
    max-height: calc(100dvh - 1rem);
    margin: auto;
  }
  .dialog-header { padding: 0.9rem 1rem; }
  .dialog-body { padding: 1rem; padding-bottom: calc(1.25rem + env(safe-area-inset-bottom)); }
}
