:root {
  color-scheme: light;
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --background: #f4f6fb;
  --surface: #ffffff;
  --surface-soft: rgba(255, 255, 255, 0.76);
  --text: #182033;
  --muted: #687185;
  --border: #dfe3ec;
  --primary: #5557e8;
  --primary-dark: #4446cf;
  --success: #21805a;
  --danger: #bc3549;
  --shadow: 0 14px 36px rgba(30, 39, 70, 0.1);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-height: 100%;
  background: var(--background);
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100%;
  margin: 0;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 100% -10%, rgba(85, 87, 232, 0.2), transparent 34%),
    var(--background);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

body.locked {
  overflow: hidden;
}

html.auth-locked,
body.auth-locked {
  overflow: hidden;
}

.protected-content[hidden] {
  display: none !important;
}

.auth-screen {
  position: fixed;
  z-index: 1000;
  inset: 0;
  display: grid;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(24px + env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) calc(24px + env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  overflow-y: auto;
  place-items: center;
  background:
    radial-gradient(circle at 100% 0%, rgba(85, 87, 232, 0.24), transparent 38%),
    #f4f6fb;
}

.auth-card {
  width: min(100%, 420px);
  padding: 30px 24px;
  border: 1px solid rgba(214, 219, 231, 0.9);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 22px 54px rgba(30, 39, 70, 0.14);
}

.auth-icon {
  display: grid;
  width: 52px;
  height: 52px;
  margin-bottom: 22px;
  place-items: center;
  border-radius: 16px;
  background: linear-gradient(145deg, #6668f6, #4648cd);
  color: #fff;
  font-size: 19px;
  box-shadow: 0 10px 24px rgba(71, 73, 203, 0.28);
}

.auth-card h1 {
  margin: 0;
  font-size: 28px;
  letter-spacing: -0.035em;
}

.auth-description {
  margin: 10px 0 24px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.auth-form label {
  display: block;
  margin-bottom: 9px;
  font-size: 13px;
  font-weight: 750;
}

.auth-message {
  min-height: 20px;
  margin: 8px 0 12px;
  color: var(--danger);
  font-size: 13px;
}

.auth-form .primary-button {
  width: 100%;
}

button,
input,
textarea {
  font: inherit;
}

button {
  touch-action: manipulation;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fff;
  color: var(--text);
  font-size: 16px;
  outline: none;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

input {
  min-height: 50px;
  padding: 0 15px;
}

textarea {
  min-height: 120px;
  padding: 13px 15px;
  resize: vertical;
  line-height: 1.5;
}

input:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 4px rgba(85, 87, 232, 0.13);
}

input.invalid,
textarea.invalid {
  border-color: var(--danger);
  box-shadow: 0 0 0 4px rgba(188, 53, 73, 0.1);
}

.app-shell {
  width: min(100%, 760px);
  min-height: 100vh;
  margin: 0 auto;
  padding: 0 max(16px, env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

.app-header {
  position: sticky;
  z-index: 20;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: calc(74px + env(safe-area-inset-top));
  margin: 0 -16px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid rgba(218, 222, 233, 0.82);
  background: rgba(244, 246, 251, 0.91);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
}

.brand {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 11px;
}

.brand-icon {
  display: grid;
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 13px;
  background: linear-gradient(145deg, #6668f6, #4648cd);
  color: #fff;
  font-size: 17px;
  box-shadow: 0 9px 22px rgba(71, 73, 203, 0.28);
}

.brand-kicker,
.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.brand h1 {
  overflow: hidden;
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.025em;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 7px;
}

.icon-button {
  display: grid;
  width: 42px;
  height: 42px;
  padding: 0;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.82);
  color: #4d566b;
  cursor: pointer;
  font-size: 20px;
}

.connection-status {
  padding: 5px 8px;
  border-radius: 999px;
  background: #e9f7f0;
  color: var(--success);
  font-size: 10px;
  font-weight: 800;
}

.connection-status.offline {
  background: #fff2e4;
  color: #9a5a16;
}

.welcome-card {
  margin: 18px 0 16px;
  padding: 21px;
  border: 1px solid rgba(218, 221, 244, 0.9);
  border-radius: 20px;
  background: linear-gradient(140deg, rgba(255, 255, 255, 0.93), rgba(245, 245, 255, 0.85));
  box-shadow: var(--shadow);
}

.welcome-card h2 {
  margin: 0;
  font-size: clamp(23px, 7vw, 31px);
  line-height: 1.12;
  letter-spacing: -0.04em;
}

.welcome-card p:last-child {
  margin: 11px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.55;
}

.search-wrap {
  position: relative;
  margin-bottom: 13px;
}

.search-wrap input {
  padding-left: 43px;
  border-radius: 15px;
  background: rgba(255, 255, 255, 0.88);
}

.search-icon {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 16px;
  color: #7a8294;
  font-size: 22px;
  transform: translateY(-52%);
}

.category-navigation {
  display: flex;
  gap: 8px;
  margin: 0 -16px 20px;
  padding: 0 16px 3px;
  overflow-x: auto;
  scrollbar-width: none;
}

.category-navigation::-webkit-scrollbar {
  display: none;
}

.category-button {
  flex: 0 0 auto;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.76);
  color: #626b7e;
  cursor: pointer;
  font-size: 13px;
  font-weight: 750;
  white-space: nowrap;
}

.category-button.active {
  border-color: var(--primary);
  background: var(--primary);
  color: #fff;
  box-shadow: 0 7px 18px rgba(85, 87, 232, 0.2);
}

.loading-state {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 120px;
  color: var(--muted);
  font-size: 13px;
}

.loading-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--primary);
  animation: pulse 1s ease-in-out infinite alternate;
}

@keyframes pulse {
  to {
    opacity: 0.35;
    transform: scale(0.75);
  }
}

.groups-container {
  display: grid;
  gap: 16px;
}

.template-group {
  padding: 17px;
  border: 1px solid rgba(221, 225, 235, 0.96);
  border-radius: 19px;
  background: var(--surface-soft);
  box-shadow: 0 8px 24px rgba(29, 39, 72, 0.055);
}

.group-header {
  margin-bottom: 13px;
}

.group-header h2 {
  margin: 0;
  font-size: 17px;
  letter-spacing: -0.025em;
}

.group-header p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
}

.template-button {
  position: relative;
  display: flex;
  min-height: 70px;
  padding: 12px;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  border: 1px solid #dfe2ef;
  border-radius: 14px;
  background: #fff;
  color: #3d4660;
  cursor: pointer;
  font-size: 12px;
  font-weight: 800;
  line-height: 1.35;
  text-align: left;
  transition: transform 100ms ease, border-color 120ms ease, background 120ms ease;
}

.template-button:active {
  transform: scale(0.975);
}

.template-button::after {
  content: "↗";
  flex: 0 0 auto;
  color: currentColor;
  font-size: 14px;
  opacity: 0.55;
}

.template-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.tone-violet .template-button {
  border-color: #dedfff;
  background: #fafaff;
  color: #4548c9;
}

.tone-green .template-button {
  border-color: #d5eadf;
  background: #f8fdf9;
  color: #23704f;
}

.tone-blue .template-button {
  border-color: #d5e0f7;
  background: #f8faff;
  color: #345b9d;
}

.tone-amber .template-button {
  border-color: #ecdfc5;
  background: #fffbf3;
  color: #795820;
}

.tone-rose .template-button {
  border-color: #efd8df;
  background: #fff9fb;
  color: #9a3e5b;
}

.local-note,
.empty-state {
  margin: 20px 8px 0;
  color: #7a8293;
  font-size: 11px;
  line-height: 1.55;
  text-align: center;
}

.drawer,
.modal {
  position: fixed;
  z-index: 100;
  inset: 0;
}

.drawer-backdrop,
.modal-backdrop {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: rgba(20, 27, 45, 0.42);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

.drawer-panel {
  position: absolute;
  inset: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--background);
  padding-bottom: calc(96px + env(safe-area-inset-bottom));
}

.drawer-header {
  position: sticky;
  z-index: 2;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(15px + env(safe-area-inset-top)) 18px 15px;
  border-bottom: 1px solid var(--border);
  background: rgba(244, 246, 251, 0.94);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.drawer-header h2,
.modal-card h2 {
  margin: 0;
  font-size: 24px;
  letter-spacing: -0.035em;
}

.settings-form {
  display: grid;
  gap: 14px;
  width: min(100%, 680px);
  margin: 0 auto;
  padding: 16px;
}

.settings-section {
  display: grid;
  min-width: 0;
  margin: 0;
  padding: 17px;
  gap: 9px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.83);
}

.settings-section legend {
  padding: 0 6px;
  color: #30394d;
  font-size: 15px;
  font-weight: 850;
}

.settings-section label {
  margin-top: 8px;
  color: #3c4559;
  font-size: 13px;
  font-weight: 750;
}

.settings-section label:first-of-type {
  margin-top: 0;
}

.form-message {
  min-height: 18px;
  margin: 0 4px;
  color: var(--danger);
  font-size: 12px;
  line-height: 1.45;
}

.form-message.success {
  color: var(--success);
}

.settings-tools {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 9px;
}

.settings-tools .danger-button {
  grid-column: 1 / -1;
}

.primary-button,
.secondary-button,
.danger-button {
  min-height: 50px;
  padding: 10px 14px;
  border-radius: 14px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 800;
}

.primary-button {
  border: 0;
  background: var(--primary);
  color: #fff;
  box-shadow: 0 9px 22px rgba(85, 87, 232, 0.25);
}

.primary-button:active {
  background: var(--primary-dark);
}

.secondary-button {
  border: 1px solid var(--border);
  background: #fff;
  color: #525d72;
}

.danger-button {
  border: 1px solid #efcfd5;
  background: #fff7f8;
  color: var(--danger);
}

.drawer-save-bar {
  position: fixed;
  z-index: 3;
  right: 0;
  bottom: 0;
  left: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border);
  background: rgba(244, 246, 251, 0.95);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.drawer-save-bar .primary-button {
  width: min(100%, 648px);
  margin: 0 auto;
  display: block;
}

.modal {
  display: grid;
  align-items: end;
}

.modal-card {
  position: relative;
  z-index: 1;
  width: min(100%, 560px);
  max-height: 88vh;
  margin: 0 auto;
  padding: 13px 20px calc(20px + env(safe-area-inset-bottom));
  overflow-y: auto;
  border-radius: 24px 24px 0 0;
  background: #fff;
  box-shadow: 0 -16px 50px rgba(20, 27, 45, 0.2);
}

.modal-handle {
  width: 42px;
  height: 5px;
  margin: 0 auto 19px;
  border-radius: 999px;
  background: #d7dbe4;
}

.modal-card ol {
  margin: 16px 0 20px;
  padding-left: 22px;
  color: #555f73;
  font-size: 14px;
  line-height: 1.7;
}

.modal-card p {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.modal-card .primary-button,
.modal-card .secondary-button {
  width: 100%;
  margin-top: 9px;
}

.copy-fallback-card textarea {
  min-height: 230px;
  font-size: 13px;
}

.toast {
  position: fixed;
  z-index: 200;
  right: 16px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  left: 16px;
  width: fit-content;
  max-width: calc(100% - 32px);
  margin: 0 auto;
  padding: 12px 16px;
  border-radius: 999px;
  background: #20283b;
  color: #fff;
  box-shadow: 0 12px 32px rgba(20, 27, 45, 0.28);
  font-size: 12px;
  font-weight: 750;
  text-align: center;
}

.toast.error {
  background: #a52e40;
}

.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;
}

noscript {
  display: block;
  padding: 30px;
  text-align: center;
}

@media (min-width: 640px) {
  .app-header {
    margin: 0;
    border-right: 1px solid rgba(218, 222, 233, 0.82);
    border-left: 1px solid rgba(218, 222, 233, 0.82);
    border-radius: 0 0 16px 16px;
  }

  .template-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .drawer-panel {
    inset: 0 0 0 auto;
    width: min(100%, 720px);
    box-shadow: -18px 0 48px rgba(20, 27, 45, 0.18);
  }
}

@media (max-width: 360px) {
  .connection-status {
    display: none;
  }

  .template-grid {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
