@layer base, layout, components, utilities, animations;

@layer base {
  :root {
    --bg-color: oklch(0.99 0.005 240);
    --card-bg: oklch(1 0 0);
    --primary: oklch(0.6 0.22 260);
    --primary-hover: oklch(0.55 0.22 260);
    --text-main: oklch(0.2 0.02 240);
    --text-muted: oklch(0.45 0.02 240);
    --shadow-color: oklch(0 0 0 / 0.08);
    --ball-size: 3.5rem;
    --ball-shadow: oklch(0 0 0 / 0.15);
  }

  *, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
  }

  body {
    font-family: 'Inter', 'Noto Sans KR', system-ui, -apple-system, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.6;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    scroll-behavior: smooth;
  }

  /* Navigation Bar */
  .nav-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: oklch(1 0 0 / 0.7);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border-bottom: 1px solid oklch(0.9 0.01 240);
    padding-block: 1rem;
  }

  .nav-container {
    inline-size: min(100% - 2rem, 1000px);
    margin-inline: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .nav-logo {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--text-main);
  }

  .nav-links {
    display: flex;
    gap: 1.5rem;
  }

  .nav-link {
    text-decoration: none;
    color: var(--text-muted);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s ease;
    display: flex;
    flex-direction: column;
    align-items: center;

    &:hover {
      color: var(--primary);
      transform: translateY(-1px);
    }

    .en { font-size: 0.7em; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.7; }
    .ko { font-weight: 700; }
  }

  /* Bilingual display */
  .en { display: block; font-size: 0.85em; }
  .ko { display: block; font-weight: 700; }
}

@layer layout {
  .background-texture {
    position: fixed;
    inset: 0;
    background: 
      radial-gradient(circle at 0% 0%, oklch(0.92 0.1 260 / 0.3), transparent 50%),
      radial-gradient(circle at 100% 0%, oklch(0.85 0.15 320 / 0.3), transparent 50%),
      radial-gradient(circle at 100% 100%, oklch(0.9 0.12 180 / 0.3), transparent 50%),
      radial-gradient(circle at 0% 100%, oklch(0.88 0.1 220 / 0.3), transparent 50%),
      var(--bg-color);
    filter: blur(80px);
    animation: backgroundShift 20s ease-in-out infinite alternate;
    pointer-events: none;
    z-index: -1;
  }

  .container {
    inline-size: min(100% - 2rem, 700px);
    margin-inline: auto;
    padding-block: 4rem;
    display: flex;
    flex-direction: column;
    gap: 4rem;
    flex: 1;
  }

  .header {
    text-align: center;
    .title {
      font-size: clamp(2rem, 8cvi, 3.5rem);
      line-height: 1.1;
      margin-block-end: 1rem;
      background: linear-gradient(135deg, var(--primary), oklch(0.6 0.25 320));
      -webkit-background-clip: text;
      -webkit-text-fill-color: transparent;
      .en { font-size: 0.4em; text-transform: uppercase; letter-spacing: 0.2em; opacity: 0.7; -webkit-text-fill-color: var(--text-muted); }
    }
    .subtitle {
      color: var(--text-muted);
      font-size: 1.1rem;
      .en { font-size: 0.8em; }
    }
  }

  .footer {
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
    padding-block: 2rem;
    opacity: 0.6;
    .en { display: inline; margin-right: 0.5rem; }
    .ko { display: inline; }
  }
}

@layer components {
  .generator-card {
    background: var(--card-bg);
    padding: 3.5rem 2rem;
    border-radius: 3rem;
    box-shadow: 
      0 10px 30px var(--shadow-color),
      0 30px 60px oklch(0 0 0 / 0.05),
      inset 0 0 0 1px oklch(1 0 0 / 0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3.5rem;
    transition: all 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;

    &::before {
      content: '';
      position: absolute;
      top: 0; left: 0; right: 0; height: 1px;
      background: linear-gradient(90deg, transparent, oklch(1 0 0 / 0.8), transparent);
    }
  }

  .ball-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 1.25rem;
    min-height: calc(var(--ball-size) + 1rem);
    inline-size: 100%;
  }

  .action-group {
    display: flex;
    align-items: center;
    gap: 1rem;
  }

  .generate-btn {
    appearance: none;
    border: none;
    padding: 1.25rem 3rem;
    border-radius: 1.5rem;
    background: var(--primary);
    color: white;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 10px 25px oklch(0.6 0.22 260 / 0.3);
    position: relative;
    overflow: hidden;

    &:hover {
      background: var(--primary-hover);
      transform: translateY(-2px);
      box-shadow: 0 15px 35px oklch(0.6 0.22 260 / 0.4);
    }

    &:active {
      transform: translateY(0);
    }

    .en { font-size: 0.7em; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.9; margin-bottom: 2px; }
  }

  .copy-btn {
    appearance: none;
    background: oklch(0.95 0.01 240);
    border: 1px solid oklch(0.9 0.02 240);
    color: var(--text-muted);
    padding: 1rem;
    border-radius: 1.25rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: grid;
    place-items: center;

    &:hover {
      background: oklch(0.9 0.02 240);
      color: var(--text-main);
    }
  }

  .history-section {
    display: flex;
    flex-direction: column;
    gap: 2rem;
    animation: fadeIn 1s ease forwards;

    .section-title {
      font-size: 1.5rem;
      color: var(--text-main);
      display: flex;
      flex-direction: column;
      align-items: center;
      .en { font-size: 0.6em; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.1em; }
    }
  }

  .history-list {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .history-item {
    background: oklch(1 0 0 / 0.6);
    backdrop-filter: blur(10px);
    padding: 1.25rem;
    border-radius: 1.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border: 1px solid oklch(0.9 0.01 240);
    animation: slideUp 0.5s ease-out forwards;
    box-shadow: 0 4px 12px var(--shadow-color);
  }

  .history-balls {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  .history-timestamp {
    font-size: 0.8rem;
    color: var(--text-muted);
  }

  .contact-section {
    display: flex;
    flex-direction: column;
    gap: 2.5rem;
    animation: fadeIn 1s ease forwards;
    background: oklch(1 0 0 / 0.4);
    padding: 3rem 2rem;
    border-radius: 2.5rem;
    border: 1px solid oklch(0.9 0.01 240);
  }

  .contact-form {
    display: grid;
    gap: 1.5rem;
    inline-size: 100%;
    max-inline-size: 500px;
    margin-inline: auto;

    .form-group {
      display: flex;
      flex-direction: column;
      gap: 0.5rem;

      label {
        font-size: 0.9rem;
        color: var(--text-muted);
        display: flex;
        flex-direction: column;
        .en { font-size: 0.7em; text-transform: uppercase; letter-spacing: 0.05em; }
      }

      input, textarea {
        appearance: none;
        background: white;
        border: 1px solid oklch(0.9 0.02 240);
        padding: 1rem 1.25rem;
        border-radius: 1rem;
        font-family: inherit;
        font-size: 1rem;
        transition: all 0.2s ease;
        color: var(--text-main);

        &:focus {
          outline: none;
          border-color: var(--primary);
          box-shadow: 0 0 0 4px oklch(0.6 0.22 260 / 0.1);
        }

        &::placeholder {
          color: oklch(0.7 0.02 240);
        }
      }

      textarea {
        resize: vertical;
      }
    }

    .submit-btn {
      appearance: none;
      border: none;
      padding: 1.1rem;
      border-radius: 1.25rem;
      background: var(--text-main);
      color: white;
      font-weight: 700;
      cursor: pointer;
      transition: all 0.3s ease;
      margin-top: 1rem;

      &:hover {
        background: black;
        transform: translateY(-2px);
      }

      &:active {
        transform: translateY(0);
      }

      .en { font-size: 0.7em; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.8; margin-bottom: 2px; }
    }
  }

  /* Animal Test Styles */
  .animal-test-card {
    background: var(--card-bg);
    padding: 3rem 2rem;
    border-radius: 3rem;
    box-shadow: 0 20px 50px var(--shadow-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3rem;
  }

  .input-controls {
    display: flex;
    gap: 1rem;
    background: oklch(0.96 0.01 240);
    padding: 0.5rem;
    border-radius: 1.5rem;
  }

  .input-btn {
    appearance: none;
    border: none;
    padding: 0.8rem 1.5rem;
    border-radius: 1rem;
    background: transparent;
    color: var(--text-muted);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;

    &.active {
      background: white;
      color: var(--text-main);
      box-shadow: 0 4px 12px oklch(0 0 0 / 0.05);
    }
    
    .en { font-size: 0.7em; }
  }

  .media-container {
    inline-size: 100%;
    max-inline-size: 400px;
    aspect-ratio: 1;
    background: oklch(0.98 0.005 240);
    border-radius: 2rem;
    overflow: hidden;
    position: relative;
    border: 1px solid oklch(0.9 0.01 240);
  }

  .preview-box {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    
    canvas {
      width: 100% !important;
      height: 100% !important;
      object-fit: cover;
    }
  }

  .upload-placeholder {
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    color: var(--text-muted);
    text-align: center;
    padding: 2rem;
    
    &:hover {
      color: var(--primary);
    }
  }

  .result-section {
    inline-size: 100%;
    display: flex;
    flex-direction: column;
    gap: 2rem;
  }

  .top-result {
    text-align: center;
    font-size: 1.5rem;
    color: var(--primary);
    min-height: 2.2rem;
  }

  .label-container {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
  }

  .result-bar-wrapper {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    opacity: 0.6;
    transition: all 0.3s ease;

    &.is-top {
      opacity: 1;
      .progress-fill {
        background: var(--primary);
      }
    }
  }

  .result-label {
    display: flex;
    justify-content: space-between;
    font-weight: 600;
    font-size: 0.95rem;
  }

  .progress-bg {
    height: 8px;
    background: oklch(0.94 0.01 240);
    border-radius: 4px;
    overflow: hidden;
  }

  .progress-fill {
    height: 100%;
    background: oklch(0.8 0.05 240);
    border-radius: 4px;
    transition: width 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  }

  .analyze-action {
    inline-size: 100%;
    display: flex;
    justify-content: center;
  }

  .model-upload-section {
    background: oklch(0.9 0.05 30 / 0.1);
    border: 2px dashed oklch(0.8 0.1 30 / 0.3);
    padding: 2rem;
    border-radius: 2rem;
    text-align: center;
    display: none;

    p {
      margin-bottom: 1rem;
      font-size: 0.95rem;
    }
    
    .file-input-field {
      font-size: 0.8rem;
    }
  }
}

@layer animations {
  @keyframes backgroundShift {
    0% { transform: scale(1) translate(0, 0); }
    50% { transform: scale(1.1) translate(-2%, 2%); }
    100% { transform: scale(1) translate(0, 0); }
  }

  @keyframes popIn {
    0% { transform: scale(0) rotate(-45deg); opacity: 0; }
    70% { transform: scale(1.1) rotate(5deg); }
    100% { transform: scale(1) rotate(0); opacity: 1; }
  }

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

  @keyframes slideUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
  }
}

@layer utilities {
  .glow {
    box-shadow: 0 0 20px currentColor;
  }
}
