/*
 * ============================================================
 *  APPLE-STYLE UI COMPONENTS — CSS
 * ============================================================
 *  Implements Apple HIG components for web.
 *  Requires: design-tokens.css loaded first.
 *
 *  Sources:
 *    - https://developer.apple.com/design/human-interface-guidelines/buttons
 *    - https://developer.apple.com/design/human-interface-guidelines/menus
 *    - https://developer.apple.com/design/human-interface-guidelines/sidebars
 *    - https://developer.apple.com/design/human-interface-guidelines/toolbars
 *    - https://codedgar.github.io/Puppertino/
 * ============================================================
 */


/* ============================================================
   1. TYPOGRAPHY UTILITY CLASSES
   ============================================================ */

.text-large-title {
  font-size: var(--text-large-title);
  line-height: var(--lh-large-title);
  font-weight: var(--fw-regular);
  letter-spacing: var(--ls-tight);
}
.text-large-title.emphasized { font-weight: var(--fw-bold); }

.text-title1 {
  font-size: var(--text-title1);
  line-height: var(--lh-title1);
  font-weight: var(--fw-regular);
  letter-spacing: var(--ls-tight);
}
.text-title1.emphasized { font-weight: var(--fw-bold); }

.text-title2 {
  font-size: var(--text-title2);
  line-height: var(--lh-title2);
  font-weight: var(--fw-regular);
}
.text-title2.emphasized { font-weight: var(--fw-bold); }

.text-title3 {
  font-size: var(--text-title3);
  line-height: var(--lh-title3);
  font-weight: var(--fw-regular);
}
.text-title3.emphasized { font-weight: var(--fw-semibold); }

.text-headline {
  font-size: var(--text-headline);
  line-height: var(--lh-headline);
  font-weight: var(--fw-semibold);
}

.text-body {
  font-size: var(--text-body);
  line-height: var(--lh-body);
  font-weight: var(--fw-regular);
}
.text-body.emphasized { font-weight: var(--fw-semibold); }

.text-callout {
  font-size: var(--text-callout);
  line-height: var(--lh-callout);
  font-weight: var(--fw-regular);
}
.text-callout.emphasized { font-weight: var(--fw-semibold); }

.text-subheadline {
  font-size: var(--text-subheadline);
  line-height: var(--lh-subheadline);
  font-weight: var(--fw-regular);
}
.text-subheadline.emphasized { font-weight: var(--fw-semibold); }

.text-footnote {
  font-size: var(--text-footnote);
  line-height: var(--lh-footnote);
  font-weight: var(--fw-regular);
}
.text-footnote.emphasized { font-weight: var(--fw-semibold); }

.text-caption1 {
  font-size: var(--text-caption1);
  line-height: var(--lh-caption1);
  font-weight: var(--fw-regular);
}
.text-caption1.emphasized { font-weight: var(--fw-semibold); }

.text-caption2 {
  font-size: var(--text-caption2);
  line-height: var(--lh-caption2);
  font-weight: var(--fw-regular);
}
.text-caption2.emphasized { font-weight: var(--fw-semibold); }

/* Label color classes */
.apple-ds .text-primary         { color: var(--apple-label); }
.apple-ds .text-secondary       { color: var(--apple-secondary-label); }
.text-tertiary        { color: var(--apple-tertiary-label); }
.text-quaternary      { color: var(--apple-quaternary-label); }
.text-placeholder     { color: var(--apple-placeholder); }
.text-link            { color: var(--apple-link); }


/* ============================================================
   2. BUTTONS
   ============================================================ */

/* ----- Base Button Reset ----- */
.apple-ds .btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  font-family: var(--font-system);
  font-size: var(--text-body);
  font-weight: var(--fw-regular);
  line-height: 1;
  cursor: pointer;
  border: none;
  outline: none;
  text-decoration: none;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
  transition: var(--transition-fast);
  border-radius: var(--radius-button);
  padding: var(--space-2) var(--space-4);
  min-height: var(--touch-target-ios);
}

.apple-ds .btn:focus-visible {
  outline: 2px solid var(--apple-blue);
  outline-offset: 2px;
}

/* ----- Primary (Filled) Button ----- */
.apple-ds .btn-primary {
  background: var(--apple-blue);
  color: #FFFFFF;
}
.apple-ds .btn-primary:hover  { background: #0077ED; }
.apple-ds .btn-primary:active { background: #006ADB; opacity: 0.85; }

/* ----- Secondary (Gray Fill) Button ----- */
.apple-ds .btn-secondary {
  background: var(--apple-fill);
  color: var(--apple-blue);
}
.apple-ds .btn-secondary:hover  { background: var(--apple-fill-secondary); }
.apple-ds .btn-secondary:active { opacity: 0.7; }

/* ----- Tinted Button ----- */
.btn-tinted {
  background: rgba(0, 122, 255, 0.15);
  color: var(--apple-blue);
}
.btn-tinted:hover  { background: rgba(0, 122, 255, 0.22); }
.btn-tinted:active { opacity: 0.7; }

/* ----- Plain / Text Button ----- */
.btn-plain {
  background: transparent;
  color: var(--apple-blue);
  padding: var(--space-1) var(--space-2);
}
.btn-plain:hover  { background: var(--apple-fill-quaternary); }
.btn-plain:active { opacity: 0.5; }

/* ----- Destructive Button ----- */
.btn-destructive {
  background: transparent;
  color: var(--apple-red);
}
.btn-destructive:hover  { background: rgba(255, 59, 48, 0.1); }
.btn-destructive:active { opacity: 0.7; }

.btn-destructive-filled {
  background: var(--apple-red);
  color: #FFFFFF;
}
.btn-destructive-filled:hover  { opacity: 0.9; }
.btn-destructive-filled:active { opacity: 0.75; }

/* ----- Disabled State ----- */
.apple-ds .btn:disabled,
.apple-ds .btn[disabled] {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

/* ----- Button Sizes ----- */
.apple-ds .btn-sm {
  font-size: var(--text-footnote);
  padding: var(--space-1) var(--space-3);
  min-height: 28px;
  border-radius: var(--radius-xs);
}

.apple-ds .btn-lg {
  font-size: var(--text-title3);
  padding: var(--space-3) var(--space-6);
  min-height: 52px;
  border-radius: var(--radius-lg);
}

/* ----- Icon Button (circular) ----- */
.btn-icon {
  width: var(--touch-target-ios);
  height: var(--touch-target-ios);
  padding: 0;
  border-radius: var(--radius-full);
  background: var(--apple-fill);
  color: var(--apple-blue);
}
.btn-icon:hover  { background: var(--apple-fill-secondary); }
.btn-icon:active { opacity: 0.7; }

/* ----- Liquid Glass Button ----- */
.btn-glass {
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: var(--apple-label);
  border-radius: var(--radius-full);
}
.btn-glass:hover {
  background: rgba(255, 255, 255, 0.25);
}
.btn-glass:active {
  background: rgba(255, 255, 255, 0.10);
}


/* ============================================================
   3. TOGGLE / SWITCH (iOS-style)
   ============================================================ */

.apple-switch {
  position: relative;
  display: inline-block;
  width: 51px;
  height: 31px;
  flex-shrink: 0;
}

.apple-switch input {
  opacity: 0;
  width: 0;
  height: 0;
  position: absolute;
}

.apple-switch .slider {
  position: absolute;
  inset: 0;
  background-color: #E9E9EA;
  border-radius: 31px;
  transition: background-color 0.3s var(--ease-default);
  cursor: pointer;
}

.apple-switch .slider::before {
  content: '';
  position: absolute;
  width: 27px;
  height: 27px;
  left: 2px;
  bottom: 2px;
  background-color: #FFFFFF;
  border-radius: 50%;
  transition: transform 0.3s var(--ease-default);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.apple-switch input:checked + .slider {
  background-color: var(--apple-green, #34C759);
}

.apple-switch input:checked + .slider::before {
  transform: translateX(20px);
}

.apple-switch input:disabled + .slider {
  opacity: 0.35;
  cursor: not-allowed;
}

.apple-switch input:focus-visible + .slider {
  outline: 2px solid var(--apple-blue);
  outline-offset: 2px;
}

/* Dark mode switch */
@media (prefers-color-scheme: dark) {
  .apple-switch .slider {
    background-color: #39393D;
  }
}


/* ============================================================
   4. SEGMENTED CONTROL
   ============================================================ */

.segmented-control {
  display: inline-flex;
  background: var(--apple-fill-tertiary);
  border-radius: var(--radius-md);
  padding: 2px;
  gap: 0;
}

.segmented-control input[type="radio"] {
  display: none;
}

.segmented-control label {
  padding: 6px 16px;
  font-family: var(--font-system);
  font-size: var(--text-footnote);
  font-weight: var(--fw-medium);
  border-radius: calc(var(--radius-md) - 1px);
  cursor: pointer;
  transition: all 0.2s var(--ease-default);
  color: var(--apple-label);
  text-align: center;
  white-space: nowrap;
  user-select: none;
  -webkit-user-select: none;
}

.segmented-control label:hover {
  background: rgba(0, 0, 0, 0.03);
}

.segmented-control input:checked + label {
  background: #FFFFFF;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.08);
}

@media (prefers-color-scheme: dark) {
  .segmented-control input:checked + label {
    background: rgba(120, 120, 128, 0.36);
    box-shadow: none;
  }
  .segmented-control label:hover {
    background: rgba(255, 255, 255, 0.05);
  }
}


/* ============================================================
   5. TEXT INPUT / TEXT FIELD
   ============================================================ */

.input {
  font-family: var(--font-system);
  font-size: var(--text-body);
  line-height: var(--lh-body);
  color: var(--apple-label);
  background: var(--apple-bg-primary);
  border: 1px solid var(--apple-separator-opaque);
  border-radius: var(--radius-input);
  padding: var(--space-2) var(--space-3);
  min-height: var(--touch-target-ios);
  width: 100%;
  box-sizing: border-box;
  outline: none;
  transition: border-color 0.2s var(--ease-default), box-shadow 0.2s var(--ease-default);
}

.input::placeholder {
  color: var(--apple-placeholder);
}

.input:hover {
  border-color: var(--apple-gray2);
}

.input:focus {
  border-color: var(--apple-blue);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
}

.input:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  background: var(--apple-fill-quaternary);
}

/* Search field (macOS style) */
.input-search {
  border-radius: var(--radius-full);
  padding-left: var(--space-8);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%238E8E93' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Ccircle cx='11' cy='11' r='8'%3E%3C/circle%3E%3Cline x1='21' y1='21' x2='16.65' y2='16.65'%3E%3C/line%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 12px center;
  background-size: 16px 16px;
}

/* Textarea */
.textarea {
  font-family: var(--font-system);
  font-size: var(--text-body);
  line-height: var(--lh-body);
  color: var(--apple-label);
  background: var(--apple-bg-primary);
  border: 1px solid var(--apple-separator-opaque);
  border-radius: var(--radius-input);
  padding: var(--space-2) var(--space-3);
  width: 100%;
  box-sizing: border-box;
  outline: none;
  resize: vertical;
  min-height: 80px;
  transition: border-color 0.2s var(--ease-default), box-shadow 0.2s var(--ease-default);
}

.textarea:focus {
  border-color: var(--apple-blue);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
}


/* ============================================================
   6. CHECKBOX & RADIO (macOS-style)
   ============================================================ */

/* Checkbox */
.apple-ds .checkbox {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  user-select: none;
}

.apple-ds .checkbox input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border: 1.5px solid var(--apple-gray2);
  border-radius: 4px;
  background: var(--apple-bg-primary);
  cursor: pointer;
  transition: all 0.15s var(--ease-default);
  position: relative;
  flex-shrink: 0;
}

.apple-ds .checkbox input[type="checkbox"]:checked {
  background: var(--apple-blue);
  border-color: var(--apple-blue);
}

.apple-ds .checkbox input[type="checkbox"]:checked::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 5px;
  width: 5px;
  height: 9px;
  border: solid #FFFFFF;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

.apple-ds .checkbox input[type="checkbox"]:focus-visible {
  outline: 2px solid var(--apple-blue);
  outline-offset: 2px;
}

/* Radio */
.apple-ds .radio {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  cursor: pointer;
  user-select: none;
}

.apple-ds .radio input[type="radio"] {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border: 1.5px solid var(--apple-gray2);
  border-radius: 50%;
  background: var(--apple-bg-primary);
  cursor: pointer;
  transition: all 0.15s var(--ease-default);
  position: relative;
  flex-shrink: 0;
}

.apple-ds .radio input[type="radio"]:checked {
  border-color: var(--apple-blue);
  border-width: 5px;
}

.apple-ds .radio input[type="radio"]:focus-visible {
  outline: 2px solid var(--apple-blue);
  outline-offset: 2px;
}


/* ============================================================
   7. SELECT / DROPDOWN
   ============================================================ */

.select {
  font-family: var(--font-system);
  font-size: var(--text-body);
  color: var(--apple-label);
  background: var(--apple-bg-primary);
  border: 1px solid var(--apple-separator-opaque);
  border-radius: var(--radius-input);
  padding: var(--space-2) var(--space-8) var(--space-2) var(--space-3);
  min-height: var(--touch-target-ios);
  width: 100%;
  box-sizing: border-box;
  outline: none;
  cursor: pointer;
  appearance: none;
  -webkit-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 d='M3 5l3 3 3-3' stroke='%238E8E93' stroke-width='1.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  transition: border-color 0.2s var(--ease-default);
}

.select:hover {
  border-color: var(--apple-gray2);
}

.select:focus {
  border-color: var(--apple-blue);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.25);
}


/* ============================================================
   8. MENU ITEM (dropdown list item)
   ============================================================ */

.menu-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-4);
  font-family: var(--font-system);
  font-size: var(--text-footnote);
  color: var(--apple-label);
  cursor: pointer;
  transition: background 0.1s var(--ease-default);
  border-radius: var(--radius-xs);
  margin: 0 var(--space-1);
  white-space: nowrap;
}

.menu-item:hover {
  background: var(--apple-blue);
  color: #FFFFFF;
}

.menu-item--disabled {
  color: var(--apple-tertiary-label);
  cursor: not-allowed;
  pointer-events: none;
}

.menu-item--destructive {
  color: var(--apple-red);
}
.menu-item--destructive:hover {
  background: var(--apple-red);
  color: #FFFFFF;
}

.menu-separator {
  height: 1px;
  background: var(--apple-separator);
  margin: var(--space-1) var(--space-3);
}

/* Keyboard shortcut hint */
.menu-item .shortcut {
  margin-left: auto;
  padding-left: var(--space-6);
  color: var(--apple-secondary-label);
  font-size: var(--text-caption1);
}
.menu-item:hover .shortcut {
  color: rgba(255, 255, 255, 0.7);
}


/* ============================================================
   9. SIDEBAR ITEM
   ============================================================ */

.sidebar-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  font-family: var(--font-system);
  font-size: var(--text-footnote);
  color: var(--apple-label);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s var(--ease-default);
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-item:hover {
  background: var(--apple-fill-quaternary);
}

.sidebar-item.active,
.sidebar-item[aria-selected="true"] {
  background: var(--apple-fill);
  font-weight: var(--fw-semibold);
}

.sidebar-item .icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  color: var(--apple-blue);
}

.sidebar-item .badge {
  margin-left: auto;
  background: var(--apple-gray4);
  color: var(--apple-secondary-label);
  font-size: var(--text-caption2);
  font-weight: var(--fw-medium);
  padding: 1px 7px;
  border-radius: var(--radius-full);
  min-width: 20px;
  text-align: center;
}

/* Section header */
.sidebar-header {
  font-family: var(--font-system);
  font-size: var(--text-caption1);
  font-weight: var(--fw-semibold);
  color: var(--apple-secondary-label);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: var(--space-4) var(--space-3) var(--space-1);
}


/* ============================================================
   10. BADGE / TAG
   ============================================================ */

.apple-ds .badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-system);
  font-size: var(--text-caption2);
  font-weight: var(--fw-medium);
  padding: 2px 8px;
  border-radius: var(--radius-full);
  white-space: nowrap;
}

.badge-blue    { background: rgba(0, 122, 255, 0.12); color: var(--apple-blue); }
.badge-green   { background: rgba(52, 199, 89, 0.12); color: var(--apple-green); }
.badge-red     { background: rgba(255, 59, 48, 0.12); color: var(--apple-red); }
.badge-orange  { background: rgba(255, 149, 0, 0.12); color: var(--apple-orange); }
.badge-purple  { background: rgba(175, 82, 222, 0.12); color: var(--apple-purple); }
.badge-gray    { background: var(--apple-fill-tertiary); color: var(--apple-secondary-label); }

/* Notification dot */
.badge-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--apple-red);
  padding: 0;
}

/* Notification count (iOS-style red pill) */
.badge-count {
  background: var(--apple-red);
  color: #FFFFFF;
  font-size: 11px;
  font-weight: var(--fw-medium);
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: var(--radius-full);
}


/* ============================================================
   11. PROGRESS BAR
   ============================================================ */

.apple-ds .progress {
  width: 100%;
  height: 4px;
  background: var(--apple-fill-tertiary);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.apple-ds .progress-bar {
  height: 100%;
  background: var(--apple-blue);
  border-radius: var(--radius-full);
  transition: width 0.3s var(--ease-default);
}

/* Indeterminate */
.apple-ds .progress--indeterminate .progress-bar {
  width: 30%;
  animation: progress-indeterminate 1.5s ease-in-out infinite;
}

@keyframes progress-indeterminate {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(400%); }
}


/* ============================================================
   12. ALERT / NOTIFICATION BANNER
   ============================================================ */

.apple-ds .alert {
  display: flex;
  align-items: flex-start;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-lg);
  font-family: var(--font-system);
  font-size: var(--text-subheadline);
  line-height: var(--lh-subheadline);
}

.apple-ds .alert-info {
  background: rgba(0, 122, 255, 0.08);
  color: var(--apple-blue);
  border: 1px solid rgba(0, 122, 255, 0.15);
}

.apple-ds .alert-success {
  background: rgba(52, 199, 89, 0.08);
  color: var(--apple-green);
  border: 1px solid rgba(52, 199, 89, 0.15);
}

.apple-ds .alert-warning {
  background: rgba(255, 149, 0, 0.08);
  color: var(--apple-orange);
  border: 1px solid rgba(255, 149, 0, 0.15);
}

.apple-ds .alert-error {
  background: rgba(255, 59, 48, 0.08);
  color: var(--apple-red);
  border: 1px solid rgba(255, 59, 48, 0.15);
}


/* ============================================================
   13. DIVIDER / SEPARATOR
   ============================================================ */

.divider {
  border: none;
  height: 1px;
  background: var(--apple-separator);
  margin: 0;
}

.divider-opaque {
  background: var(--apple-separator-opaque);
}


/* ============================================================
   14. AVATAR / USER ICON
   ============================================================ */

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--apple-fill-tertiary);
  flex-shrink: 0;
}

.avatar-sm { width: 28px; height: 28px; }
.avatar-lg { width: 56px; height: 56px; }
.avatar-xl { width: 80px; height: 80px; }

/* Initials avatar */
.avatar-initials {
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-system);
  font-weight: var(--fw-medium);
  font-size: 16px;
  color: #FFFFFF;
  background: var(--apple-blue);
}


/* ============================================================
   15. TOOLTIP
   ============================================================ */

.apple-ds .tooltip {
  position: relative;
}

.apple-ds .tooltip::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%) translateY(-4px);
  padding: var(--space-1) var(--space-2);
  font-family: var(--font-system);
  font-size: var(--text-caption1);
  color: #FFFFFF;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: var(--radius-xs);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s var(--ease-default);
  z-index: 1000;
}

.apple-ds .tooltip:hover::after {
  opacity: 1;
}


/* ============================================================
   16. TABLE
   ============================================================ */

.apple-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-family: var(--font-system);
  font-size: var(--text-footnote);
}

.apple-table th {
  text-align: left;
  font-weight: var(--fw-semibold);
  color: var(--apple-secondary-label);
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--apple-separator-opaque);
  font-size: var(--text-caption1);
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.apple-table td {
  padding: var(--space-2) var(--space-3);
  border-bottom: 1px solid var(--apple-separator);
  color: var(--apple-label);
}

.apple-table tr:hover td {
  background: var(--apple-fill-quaternary);
}

.apple-table tr.selected td {
  background: rgba(0, 122, 255, 0.08);
}
