/* ============================================
   Shared components — settings, toast, modals, FAB, buttons, forms, tags, etc.
   Loaded via <link> in index.html.
   Depends on tokens.css custom properties.
   ============================================ */

/* ---- Settings Toggle (gear) ---- */
.settings-toggle {
  position: fixed;
  top: var(--top-safe);
  right: 52px;
  z-index: calc(var(--z-drawer) - 1);
  width: 32px;
  height: 32px;
  border-radius: var(--radius-input);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  line-height: 1;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
  transition: background var(--duration-base), border-color var(--duration-base);
}

.settings-toggle:hover {
  background: var(--color-surface-alt);
  border-color: var(--color-border-hover);
}

/* ---- Settings Panel ---- */
.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  height: auto;
  max-height: calc(100vh - 32px);
  z-index: var(--z-toolbar);
  padding-top: 52px;
  pointer-events: none;
}

.settings-panel:not([hidden]) {
  pointer-events: auto;
}

.settings-panel-inner {
  margin: 0 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-base);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

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

.settings-title {
  font-size: 14px;
  font-weight: 600;
  margin: 0;
  color: var(--color-text-primary);
}

.settings-body {
  padding: 8px 0;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
}

.settings-label {
  font-size: 13px;
  color: var(--color-text-primary);
}

.settings-btn {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-input);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  line-height: 1;
  transition: background var(--duration-base), border-color var(--duration-base);
}

.settings-btn:hover {
  background: var(--color-surface-alt);
  border-color: var(--color-border-hover);
}

.settings-btn.active {
  background: var(--color-cta-bg);
  color: var(--color-cta-text);
  border-color: var(--color-cta-bg);
}

.settings-account-actions {
  display: flex;
  gap: var(--spacing-8);
}

.settings-theme-group {
  display: flex;
  gap: 4px;
}

.theme-cycle {
  width: auto;
  padding: 0 12px;
  gap: 8px;
  font-size: 13px;
}

.theme-cycle-icon {
  font-size: 14px;
  line-height: 1;
}

.theme-cycle-label {
  line-height: 1;
}

/* ---- Diagnostics panel (admin-only settings section) ---- */
.diag-section {
  border-top: 1px solid var(--color-border);
}

.diag-header {
  border-bottom: none;
  padding-bottom: 4px;
}

.diag-clear-btn {
  font-size: 11px;
  padding: 2px 8px;
  background: none;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-input);
  color: var(--color-text-muted);
  cursor: pointer;
  line-height: 1.6;
}

.diag-clear-btn:hover {
  border-color: var(--color-border-hover);
  color: var(--color-text-primary);
}

.diag-body {
  padding: 4px 0 8px;
}

.diag-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 4px 16px;
  font-size: 11px;
}

.diag-label {
  flex-shrink: 0;
  width: 44px;
  color: var(--color-text-muted);
  padding-top: 1px;
}

.diag-value {
  color: var(--color-text-secondary);
  font-family: monospace;
  word-break: break-all;
}

.diag-row-errors {
  align-items: flex-start;
}

.diag-error-list {
  margin: 0;
  padding: 0;
  list-style: none;
  flex: 1;
  min-width: 0;
  max-height: 140px;
  overflow-y: auto;
}

.diag-error-empty {
  color: var(--color-text-muted);
  font-size: 11px;
  font-style: italic;
}

.diag-error-item {
  color: var(--color-text-secondary);
  font-family: monospace;
  font-size: 10px;
  line-height: 1.5;
  word-break: break-word;
  padding: 1px 0;
  border-bottom: 1px solid var(--color-border);
}

.diag-error-item:last-child {
  border-bottom: none;
}

.diag-link {
  color: var(--color-text-secondary);
  text-decoration: underline;
  text-underline-offset: 2px;
  pointer-events: auto;
}

/* ---- Toast notifications ---- */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  padding: 10px 16px;
  border-radius: var(--radius-base);
  font-size: 13px;
  line-height: 1.4;
  color: #fff;
  background: #1a1a1a;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: auto;
  cursor: pointer;
  max-width: 320px;
}

.toast.toast-visible {
  opacity: 1;
  transform: translateY(0);
}

.toast.toast-error {
  background: #b91c1c;
}

.toast.toast-warning {
  background: #92400e;
}

.toast.toast-info {
  background: #1d4ed8;
}

[data-theme="dark"] .toast {
  background: #27272a;
}

[data-theme="dark"] .toast.toast-error {
  background: #991b1b;
}

/* ---- Modal overlay ---- */
.modal {
  position: fixed;
  inset: 0;
  z-index: var(--z-modal);
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-overlay);
  backdrop-filter: blur(6px);
  opacity: 1;
  transition: opacity var(--duration-slow) var(--ease-default), visibility var(--duration-slow) var(--ease-default);
}

.modal[hidden] {
  display: flex !important;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.modal-content {
  background: var(--color-surface);
  border-radius: var(--radius-cards);
  border: 1px solid var(--color-border);
  box-shadow: var(--color-card-shadow);
  padding: var(--spacing-24);
  width: 90%;
  max-width: 380px;
  max-height: 90vh;
  overflow-y: auto;
  transform: scale(1) translateY(0);
  transition: transform var(--duration-slow) var(--ease-out);
}

.modal[hidden] .modal-content {
  transform: scale(0.95) translateY(12px);
}

.modal-wide { max-width: 520px; }

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: var(--spacing-20);
}

.modal-content h2 {
  font-size: var(--text-heading);
  font-weight: var(--font-weight-semibold);
  line-height: var(--leading-heading);
  letter-spacing: var(--tracking-heading);
  color: var(--color-text-heading);
}

.modal-subtitle {
  color: var(--color-text-muted);
  font-size: var(--text-body);
  margin-top: var(--spacing-4);
  margin-bottom: var(--spacing-24);
}

/* ---- Floating Add Button (FAB) ---- */
.fab-add {
  position: fixed;
  /* Offset from the corner + the iOS home-indicator / safe-area insets so the
     FAB isn't crammed against the edge in the full-bleed PWA (#86 follow-up). */
  bottom: calc(var(--spacing-32) + env(safe-area-inset-bottom, 0px));
  right: calc(var(--spacing-32) + env(safe-area-inset-right, 0px));
  z-index: var(--z-toolbar);
}

.fab-add[hidden] { display: none; }

.fab-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  border: none;
  background: var(--color-cta-bg);
  color: var(--color-cta-text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--color-fab-primary-shadow);
  transition: transform var(--duration-base), background var(--duration-base);
}

.fab-btn:hover { transform: scale(1.08); }
.fab-btn:active { transform: scale(0.95); }

.fab-btn.open {
  transform: rotate(45deg);
}

.fab-btn.open:hover {
  transform: rotate(45deg) scale(1.08);
}

.fab-menu {
  position: absolute;
  bottom: 60px;
  right: 0;
  width: 240px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-base);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  opacity: 1;
  transform: scale(1) translateY(0);
  transform-origin: bottom right;
  transition: opacity var(--duration-base) var(--ease-default), transform var(--duration-base) var(--ease-out), visibility var(--duration-base) var(--ease-default);
}

.fab-menu[hidden] {
  display: block !important;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: scale(0.9) translateY(8px);
}

.fab-menu-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  padding: 10px 14px;
  border: none;
  border-bottom: 1px solid var(--color-border);
  background: transparent;
  cursor: pointer;
  text-align: left;
  font-family: var(--font-geist);
  transition: background var(--duration-fast);
}

.fab-menu-item:last-child { border-bottom: none; }

.fab-menu:not([hidden]) .fab-menu-item {
  animation: fab-item-enter var(--duration-base) var(--ease-out) backwards;
}
.fab-menu:not([hidden]) .fab-menu-item:nth-child(1) { animation-delay: 30ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(2) { animation-delay: 60ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(3) { animation-delay: 90ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(4) { animation-delay: 120ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(5) { animation-delay: 150ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(6) { animation-delay: 180ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(7) { animation-delay: 210ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(8) { animation-delay: 240ms; }
.fab-menu:not([hidden]) .fab-menu-item:nth-child(9) { animation-delay: 270ms; }

.fab-menu-item:hover { background: var(--color-surface-alt); }
.fab-menu-item:active { background: var(--color-border); }

.fab-menu-item strong {
  font-size: var(--text-body);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-heading);
}

.fab-menu-item span {
  font-size: var(--text-caption);
  color: var(--color-text-muted);
  line-height: 1.4;
}

.fab-finish {
  position: fixed;
  bottom: var(--spacing-24);
  right: 84px;
  width: auto;
  padding: 0 var(--spacing-20);
  border-radius: var(--radius-pill);
  font-family: var(--font-geist);
  font-size: var(--text-body);
  font-weight: var(--font-weight-medium);
}

/* ---- Color Swatches ---- */
.color-swatches {
  display: flex;
  gap: var(--spacing-8);
  flex-wrap: wrap;
}

.color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid var(--color-border);
  cursor: pointer;
  padding: 0;
  transition: transform var(--duration-fast), border-color var(--duration-base);
}

.color-swatch:hover {
  transform: scale(1.15);
  border-color: var(--color-border-hover);
}

.color-swatch.active {
  border-color: var(--color-text-heading);
  box-shadow: 0 0 0 2px var(--color-canvas), 0 0 0 4px var(--color-text-heading);
}

/* ---- Type Grid (icon selector) ---- */
.type-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, 72px);
  gap: var(--spacing-8);
}

.type-grid-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: var(--spacing-6) var(--spacing-4);
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-base);
  cursor: pointer;
  background: var(--color-surface);
  transition: border-color var(--duration-fast), background var(--duration-fast);
  user-select: none;
}

.type-grid-item:hover:not(.disabled) {
  border-color: var(--color-border-hover);
  background: var(--color-surface-alt);
}

.type-grid-item.active {
  border-color: var(--color-text-heading);
  background: var(--color-surface-alt);
}

.type-grid-item.disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.type-grid-icon {
  font-size: 20px;
  line-height: 1;
}

.type-grid-label {
  font-size: 10px;
  font-weight: var(--font-weight-medium);
  color: var(--color-text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  line-height: 1.2;
  text-align: center;
}

.type-grid-item.active .type-grid-label {
  color: var(--color-text-heading);
}

/* ---- Buttons ---- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-6);
  padding: 8px 16px;
  border-radius: var(--radius-buttons);
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-primary);
  font-family: var(--font-geist);
  font-size: var(--text-body);
  font-weight: var(--font-weight-medium);
  line-height: 1;
  cursor: pointer;
  white-space: nowrap;
  transition: all var(--duration-base);
  text-decoration: none;
}

.btn:hover {
  background: var(--color-surface-alt);
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--color-cta-bg);
  border-color: var(--color-cta-bg);
  color: var(--color-cta-text);
}

.btn-primary:hover {
  background: var(--color-cta-hover);
  border-color: var(--color-cta-hover);
}

.btn-small {
  padding: 4px 10px;
  font-size: var(--text-caption);
  border-radius: var(--radius-pill);
}


.btn-icon {
  background: none;
  border: none;
  color: var(--color-text-muted);
  font-size: 18px;
  cursor: pointer;
  padding: var(--spacing-4);
  line-height: 1;
  border-radius: var(--radius-input);
  transition: color var(--duration-fast), background var(--duration-fast);
}

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

.btn-icon:active { transform: scale(0.88); }

.btn-danger {
  color: var(--color-callout-red);
  border-color: var(--color-callout-red);
  background: var(--color-surface);
}

.btn-danger:hover {
  background: var(--color-callout-red-bg);
}

/* ---- Forms ---- */
.form-group { margin-bottom: var(--spacing-12); }

.form-group label {
  display: block;
  font-size: var(--text-caption);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-muted);
  margin-bottom: var(--spacing-4);
  letter-spacing: 0.02em;
}

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 8px 12px;
  background: var(--color-canvas);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-input);
  color: var(--color-text-primary);
  font-family: var(--font-geist);
  font-size: var(--text-body);
  line-height: var(--leading-body);
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}

.form-group textarea {
  resize: vertical;
  min-height: 80px;
}

/* Color inputs now use swatch buttons instead */

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--color-border-hover);
  box-shadow: 0 0 0 2px var(--color-accent-bg);
}

.form-group input::placeholder,
.form-group textarea::placeholder {
  color: var(--color-text-faint);
}

.form-group select {
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23737373' d='M3 5l3 3 3-3'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}

.form-actions {
  display: flex;
  gap: var(--spacing-8);
  justify-content: flex-end;
  margin-top: var(--spacing-20);
  padding-top: var(--spacing-16);
  border-top: 1px solid var(--color-border);
}

.error-text {
  color: var(--color-callout-red);
  font-size: var(--text-caption);
  margin-top: var(--spacing-6);
}

.error-text[hidden] { display: none; }

/* ---- Tags ---- */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: var(--spacing-6);
}

.tag {
  display: inline-block;
  padding: 2px 8px;
  background: var(--color-tag-bg);
  border: 1px solid var(--color-tag-border);
  border-radius: var(--radius-badge);
  font-size: var(--text-caption);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: background var(--duration-base), border-color var(--duration-base), color var(--duration-base);
}

.tag:hover {
  border-color: var(--color-border-hover);
}

.tag.active {
  background: var(--color-tag-active-bg);
  border-color: var(--color-tag-active-bg);
  color: var(--color-tag-active-text);
}

/* ---- Empty / Loading states ---- */
.empty-state {
  color: var(--color-text-muted);
  font-size: var(--text-caption);
}

/* ---- CSV Import ---- */
.csv-modal-desc {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--spacing-12);
  margin-bottom: var(--spacing-12);
  font-size: var(--text-body);
  color: var(--color-text-muted);
}

.csv-drop-zone {
  border: 2px dashed var(--color-border);
  border-radius: var(--radius-input);
  padding: var(--spacing-24) var(--spacing-16);
  text-align: center;
  cursor: pointer;
  transition: border-color var(--duration-base), background var(--duration-base);
  margin-bottom: var(--spacing-12);
}

.csv-drop-zone:hover,
.csv-drop-zone.drag-over {
  border-color: var(--color-border-hover);
  background: var(--color-surface-alt);
}

.csv-drop-icon {
  font-size: 24px;
  color: var(--color-text-muted);
  margin-bottom: var(--spacing-6);
  line-height: 1;
}

.csv-drop-text {
  font-size: var(--text-body);
  color: var(--color-text-muted);
}

.csv-browse-link {
  background: none;
  border: none;
  font-family: var(--font-geist);
  font-size: var(--text-body);
  color: var(--color-text-primary);
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
}

.csv-browse-link:hover { color: var(--color-text-muted); }

.csv-parse-result {
  font-size: var(--text-caption);
  color: var(--color-text-muted);
  margin-bottom: var(--spacing-4);
}

.csv-parse-result.ready {
  color: var(--color-success-green);
  font-weight: var(--font-weight-medium);
}

/* ---- Backup Modal ---- */

.backup-last-export {
  font-size: var(--text-caption);
  color: var(--color-text-muted);
  margin: 0 0 var(--spacing-16);
}
.backup-last-export.stale { color: var(--color-amber, #f59e0b); }
.backup-last-export.very-stale { color: var(--color-danger, #ef4444); }

.backup-section {
  margin-bottom: var(--spacing-16);
}

.backup-section-title {
  font-size: var(--text-body);
  font-weight: var(--font-weight-medium);
  margin: 0 0 var(--spacing-8);
}

.backup-export-btns {
  display: flex;
  gap: var(--spacing-8);
}

.backup-import-progress {
  font-size: var(--text-caption);
  color: var(--color-text-muted);
  margin-bottom: var(--spacing-8);
}

/* ---- Lightbox ---- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: calc(var(--z-modal) + 10);
  background: var(--color-lightbox);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  animation: fade-in var(--duration-slow) var(--ease-default);
}

.lightbox img {
  max-width: 95%;
  max-height: 95%;
  object-fit: contain;
  border-radius: var(--radius-input);
  animation: scale-in var(--duration-slow) var(--ease-out);
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes scale-in {
  from { opacity: 0; transform: scale(0.92); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes list-row-enter {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fab-item-enter {
  from { opacity: 0; transform: translateX(8px); }
  to { opacity: 1; transform: translateX(0); }
}
