/**
 * Design System Apple iOS (Human Interface Guidelines)
 * Supporto Tema Chiaro, Tema Scuro e Modalità Automatica oraria.
 * Rispetto rigoroso della Safe Area (Clock, Dynamic Island, Home Bar).
 */

@layer reset, base, theme, components;

@layer reset {
  :where(*, *::before, *::after) {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
  }

  :where(html) {
    -webkit-text-size-adjust: 100%;
  }

  :where(button) {
    font: inherit;
    color: inherit;
    background: none;
    border: none;
    cursor: pointer;
  }

  :where(a) {
    color: inherit;
    text-decoration: none;
  }
}

@layer theme {
  /* Tema Scuro di Default (Dark Mode) */
  :root,
  html[data-theme="dark"] {
    color-scheme: dark;
    --system-bg: #000000;
    --system-card-bg: #1c1c1e;
    --system-card-active: #2c2c2e;
    --system-modal-bg: #161618;
    --system-header-bg: rgba(0, 0, 0, 0.85);
    
    --separator: rgba(84, 84, 88, 0.35);
    --border-subtle: rgba(255, 255, 255, 0.08);
    
    --label-primary: #ffffff;
    --label-secondary: #8e8e93;
    --label-tertiary: #636366;
    --article-text: rgba(255, 255, 255, 0.9);

    --segmented-bg: rgba(118, 118, 128, 0.24);
    --segmented-active-bg: #636366;
    --segmented-active-color: #ffffff;
    --segmented-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);

    --btn-secondary-bg: rgba(255, 255, 255, 0.09);
    --skeleton-start: #1c1c1e;
    --skeleton-mid: #2c2c2e;

    --apple-red: #ff453a;
    --apple-orange: #ff9f0a;
    --apple-blue: #0a84ff;
    --apple-teal: #30b0c7;

    --font-system: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", sans-serif;
    --radius-card: 14px;
    --radius-pill: 9999px;
  }

  /* Tema Chiaro Apple (Light Mode) */
  html[data-theme="light"] {
    color-scheme: light;
    --system-bg: #f2f2f7;
    --system-card-bg: #ffffff;
    --system-card-active: #e5e5ea;
    --system-modal-bg: #ffffff;
    --system-header-bg: rgba(242, 242, 247, 0.88);
    
    --separator: rgba(60, 60, 67, 0.18);
    --border-subtle: rgba(60, 60, 67, 0.08);
    
    --label-primary: #000000;
    --label-secondary: #6c6c70;
    --label-tertiary: #8e8e93;
    --article-text: rgba(0, 0, 0, 0.88);

    --segmented-bg: rgba(118, 118, 128, 0.14);
    --segmented-active-bg: #ffffff;
    --segmented-active-color: #000000;
    --segmented-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);

    --btn-secondary-bg: rgba(0, 0, 0, 0.06);
    --skeleton-start: #e5e5ea;
    --skeleton-mid: #f2f2f7;

    --apple-red: #d70015;
    --apple-orange: #c93400;
    --apple-blue: #007aff;
    --apple-teal: #0071a4;
  }
}

@layer base {
  body {
    font-family: var(--font-system);
    background-color: var(--system-bg);
    color: var(--label-primary);
    min-height: 100vh;
    min-height: 100dvh;
    overflow-x: hidden;
    user-select: none;
    -webkit-user-select: none;
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.25s ease, color 0.25s ease;
  }

  .app-layout {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    max-width: 640px;
    width: 100%;
    margin-inline: auto;
    padding-inline: 16px;
    padding-bottom: max(28px, env(safe-area-inset-bottom, 20px));
    box-sizing: border-box;
  }
}

@layer components {
  /* ================= PULL TO REFRESH ================= */
  .pull-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 0;
    overflow: hidden;
    color: var(--label-secondary);
    font-size: 13px;
    transition: height 0.2s ease, opacity 0.2s ease;
    opacity: 0;
  }

  .pull-indicator.visible {
    height: 44px;
    opacity: 1;
  }

  .pull-spinner {
    width: 14px;
    height: 14px;
    border: 2px solid var(--border-subtle);
    border-top-color: var(--label-secondary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
  }

  /* ================= HEADER FISSO APPLE ================= */
  .app-header {
    position: sticky;
    top: 0;
    z-index: 50;
    margin-inline: -16px;
    padding-inline: 16px;
    padding-top: calc(env(safe-area-inset-top, 44px) + 6px);
    padding-bottom: 12px;
    background-color: var(--system-header-bg);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    border-bottom: 1px solid var(--border-subtle);
    transition: background-color 0.25s ease;
    box-sizing: border-box;
    width: calc(100% + 32px);
    max-width: calc(100% + 32px);
  }

  .header-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    width: 100%;
    min-width: 0;
  }

  .brand {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
    flex: 1;
    overflow: hidden;
  }

  .brand-title {
    font-size: 21px;
    font-weight: 700;
    letter-spacing: -0.4px;
    color: var(--label-primary);
    line-height: 1.2;
  }

  .brand-sub {
    font-size: 11.5px;
    color: var(--label-secondary);
    font-weight: 400;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: block;
  }

  .header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
  }

  .last-updated {
    font-size: 12px;
    color: var(--label-secondary);
    font-variant-numeric: tabular-nums;
  }

  .icon-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--btn-secondary-bg);
    color: var(--label-primary);
    transition: background 0.15s ease, transform 0.15s ease;
  }

  .icon-button:active {
    transform: scale(0.92);
    opacity: 0.7;
  }

  .icon-button.spinning .icon {
    animation: spin 0.8s linear infinite;
  }

  /* ================= SEGMENTED CONTROL ================= */
  .segmented-control {
    display: flex;
    background: var(--segmented-bg);
    border-radius: 9px;
    padding: 2px;
    position: relative;
    user-select: none;
    overflow-x: auto;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    transition: background-color 0.25s ease;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    box-sizing: border-box;
  }

  .segmented-control::-webkit-scrollbar {
    display: none;
  }

  .segment-btn {
    flex: 1;
    min-width: fit-content;
    text-align: center;
    padding: 6px 11px;
    font-size: 13px;
    font-weight: 500;
    color: var(--label-secondary);
    border-radius: 7px;
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    white-space: nowrap;
  }

  .segment-btn.active {
    background: var(--segmented-active-bg);
    color: var(--segmented-active-color);
    font-weight: 600;
    box-shadow: var(--segmented-shadow);
  }

  /* ================= FEED NOTIZIE ================= */
  .news-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding-top: 14px;
    flex: 1;
  }

  .news-card {
    background: var(--system-card-bg);
    border-radius: var(--radius-card);
    padding: 14px 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    transition: background 0.15s ease, transform 0.12s ease;
    cursor: pointer;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }

  .news-card:active {
    background: var(--system-card-active);
    transform: scale(0.99);
  }

  .card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
  }

  .card-meta-left {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
  }

  .card-topic,
  .modal-kicker {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.4px;
    text-transform: uppercase;
  }

  /* Palette Apple per i Topic della notizia */
  .topic-politica { color: var(--apple-orange); }
  .topic-esteri { color: var(--apple-blue); }
  .topic-cronaca { color: #bf5af2; }
  .topic-economia { color: #30d158; }
  .topic-sport { color: var(--apple-red); }
  .topic-tecnologia { color: var(--apple-teal); }
  .topic-cultura { color: #ff375f; }
  .topic-default { color: var(--label-secondary); }

  html[data-theme="light"] .topic-cronaca { color: #9a41c8; }
  html[data-theme="light"] .topic-economia { color: #28a745; }
  html[data-theme="light"] .topic-cultura { color: #d30f4b; }

  .badge-flash {
    font-size: 9.5px;
    font-weight: 800;
    padding: 1px 5px;
    border-radius: 4px;
    background: rgba(255, 69, 58, 0.18);
    color: var(--apple-red);
    letter-spacing: 0.4px;
  }

  .card-time {
    font-size: 11.5px;
    color: var(--label-secondary);
    font-weight: 400;
    text-align: right;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
  }

  .card-title {
    font-size: 15.5px;
    font-weight: 600;
    line-height: 1.35;
    letter-spacing: -0.2px;
    color: var(--label-primary);
  }

  .card-preview {
    font-size: 13.5px;
    line-height: 1.45;
    color: var(--label-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  /* ================= SKELETON LOADER ================= */
  .skeleton-card {
    height: 90px;
    border-radius: var(--radius-card);
    background: linear-gradient(90deg, var(--skeleton-start) 25%, var(--skeleton-mid) 50%, var(--skeleton-start) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
  }

  /* ================= FOOTER ================= */
  .app-footer {
    margin-top: 32px;
    padding-block: 16px;
    text-align: center;
    color: var(--label-tertiary);
    font-size: 12px;
  }

  .app-footer p + p {
    margin-top: 4px;
  }

  /* ================= MODALITÀ LETTURA APPLE CON SAFE AREA PERFETTA ================= */
  .reader-dialog:not([open]) {
    display: none !important;
  }

  .reader-dialog[open] {
    display: block;
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    margin: 0;
    z-index: 999;
    border: none;
    background: var(--system-modal-bg);
    width: 100%;
    max-width: 640px;
    height: 100vh;
    height: 100dvh;
    padding: 0;
    color: var(--label-primary);
    overflow: hidden;
    margin-inline: auto;
  }

  .reader-dialog::backdrop {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    animation: fadeIn 0.2s ease-out;
  }

  .modal-wrapper {
    background: var(--system-modal-bg);
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
  }

  /* HEADER MODALE: Risolve l'overlap con l'orario e la batteria di iOS */
  .modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Spazio di sicurezza vitale sotto l'orario (15:38) e la Dynamic Island */
    padding-top: calc(env(safe-area-inset-top, 47px) + 8px);
    padding-bottom: 12px;
    padding-inline: 16px;
    border-bottom: 1px solid var(--border-subtle);
    background: var(--system-header-bg);
    backdrop-filter: blur(24px);
    -webkit-backdrop-filter: blur(24px);
    position: sticky;
    top: 0;
    z-index: 20;
    flex-shrink: 0;
  }

  .modal-back-btn {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    color: var(--apple-blue);
    font-size: 16px;
    font-weight: 500;
    padding: 4px 6px;
    margin-left: -6px;
    border-radius: 8px;
  }

  .modal-back-btn:active {
    opacity: 0.6;
  }

  .modal-kicker {
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.4px;
    text-transform: uppercase;
  }

  .modal-kicker.breaking { color: var(--apple-red); }
  .modal-kicker.highlight { color: var(--apple-orange); }
  .modal-kicker.televideo { color: var(--apple-teal); }
  .modal-kicker.ansa { color: var(--apple-blue); }

  .modal-ext-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--btn-secondary-bg);
    color: var(--label-primary);
  }

  .modal-scroll-area {
    padding: 24px 20px max(48px, calc(32px + env(safe-area-inset-bottom, 20px))) 20px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    user-select: text;
    -webkit-user-select: text;
    flex: 1;
  }

  .modal-title {
    font-size: 22px;
    font-weight: 700;
    line-height: 1.25;
    letter-spacing: -0.4px;
    margin-bottom: 10px;
    color: var(--label-primary);
  }

  .modal-meta {
    display: flex;
    align-items: center;
    gap: 6px;
    color: var(--label-secondary);
    font-size: 13px;
    margin-bottom: 20px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border-subtle);
  }

  .modal-image-container {
    margin-bottom: 20px;
    border-radius: 12px;
    overflow: hidden;
    background: var(--system-card-bg);
  }

  .modal-image-container img {
    width: 100%;
    height: auto;
    display: block;
    object-fit: cover;
  }

  /* Tipografia Unica per Televideo e ANSA */
  .article-content {
    font-size: 16.5px;
    line-height: 1.65;
    color: var(--article-text);
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .article-content p {
    text-align: left;
    margin: 0;
  }

  .article-lead {
    font-size: 17.5px;
    font-weight: 500;
    line-height: 1.55;
    color: var(--label-primary);
  }

  .article-loading {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 0;
    color: var(--label-secondary);
    font-size: 13px;
  }

  .modal-footer-action {
    margin-top: 36px;
    padding-top: 20px;
    border-top: 1px solid var(--border-subtle);
    text-align: center;
  }

  .external-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 9px 18px;
    background: var(--btn-secondary-bg);
    border-radius: var(--radius-pill);
    color: var(--apple-blue);
    font-size: 13px;
    font-weight: 500;
  }

  /* ================= MODALE IMPOSTAZIONI ================= */
  .settings-dialog:not([open]) {
    display: none !important;
  }

  .settings-dialog[open] {
    display: block;
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
    z-index: 1000;
    border: none;
    background: var(--system-modal-bg);
    width: 100%;
    max-width: 500px;
    max-height: 88vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 20px 20px 0 0;
    padding: 0;
    color: var(--label-primary);
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.4);
  }

  .settings-dialog::backdrop {
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    animation: fadeIn 0.2s ease-out;
  }

  .settings-wrapper {
    padding: 20px 20px max(28px, env(safe-area-inset-bottom, 24px)) 20px;
  }

  .settings-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .settings-title {
    font-size: 18px;
    font-weight: 700;
  }

  .settings-done-btn {
    color: var(--apple-blue);
    font-size: 16px;
    font-weight: 600;
    padding: 4px 8px;
    background: none;
    border: none;
    outline: none;
    box-shadow: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .settings-done-btn:focus,
  .settings-done-btn:focus-visible {
    outline: none;
    box-shadow: none;
  }

  .settings-section-title {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    color: var(--label-secondary);
    letter-spacing: 0.3px;
    margin-bottom: 8px;
    padding-left: 4px;
  }

  .theme-options-group {
    background: var(--system-card-bg);
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 24px;
  }

  .theme-option-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 13px 16px;
    cursor: pointer;
    transition: background 0.15s ease;
  }

  .theme-option-row:not(:last-child) {
    border-bottom: 1px solid var(--border-subtle);
  }

  .theme-option-row:active {
    background: var(--system-card-active);
  }

  .theme-option-left {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  .theme-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    color: var(--label-secondary);
  }

  .theme-option-row.selected .theme-icon {
    color: var(--apple-blue);
  }

  .theme-name {
    font-size: 15px;
    font-weight: 500;
  }

  .theme-desc {
    font-size: 11.5px;
    color: var(--label-secondary);
  }

  .theme-check {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--apple-blue);
    opacity: 0;
    transition: opacity 0.15s ease;
  }

  .theme-option-row.selected .theme-check {
    opacity: 1;
  }

  /* ================= FONTI & SWITCH TOGGLE APPLE ================= */
  .source-options-group {
    background: var(--system-card-bg);
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 24px;
  }

  .source-option-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    gap: 14px;
  }

  .source-option-row:not(:last-child) {
    border-bottom: 1px solid var(--border-subtle);
  }

  .source-info {
    display: flex;
    flex-direction: column;
    gap: 2px;
  }

  .source-name {
    font-size: 15px;
    font-weight: 500;
    color: var(--label-primary);
  }

  .source-desc {
    font-size: 11.5px;
    color: var(--label-secondary);
  }

  /* Switch Apple HIG */
  .ios-switch {
    position: relative;
    display: inline-block;
    width: 48px;
    height: 28px;
    flex-shrink: 0;
  }

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

  .ios-slider {
    position: absolute;
    cursor: pointer;
    inset: 0;
    background-color: rgba(120, 120, 128, 0.32);
    transition: background-color 0.22s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 28px;
  }

  .ios-slider::before {
    position: absolute;
    content: "";
    height: 24px;
    width: 24px;
    left: 2px;
    bottom: 2px;
    background-color: #ffffff;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2), 0 0 1px rgba(0, 0, 0, 0.15);
    transition: transform 0.22s cubic-bezier(0.4, 0, 0.2, 1);
    border-radius: 50%;
  }

  .ios-switch input:checked + .ios-slider {
    background-color: #34c759;
  }

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

/* ================= ANIMAZIONI ================= */
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@keyframes shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Desktop & iPad */
@media (min-width: 641px) {
  .reader-dialog[open] {
    top: 50%;
    bottom: auto;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 18px;
    max-height: 88vh;
    border: 1px solid var(--separator);
  }
  .modal-wrapper {
    border-radius: 18px;
  }
  .modal-header {
    padding-top: 14px;
    border-radius: 18px 18px 0 0;
  }
  .settings-dialog[open] {
    top: 50%;
    bottom: auto;
    transform: translate(-50%, -50%);
    border-radius: 18px;
    border: 1px solid var(--separator);
  }
}
