:root {
    --primary-color: #4CAF50;
    --secondary-color: #4CAF50;
    --user-bg: #d1e7dd;
    --bot-bg: #ffffff;
    --background: #f5f7fa;
    --border-radius: 12px;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    --shadow-hover: 0 4px 16px rgba(0, 0, 0, 0.12);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
  }

  * { box-sizing: border-box; }

  html, body { height: 100%; }

  body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh; /* dynamic viewport for mobile */
    margin: 0;
    background: var(--background);
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
  }

  header {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    padding: 10px 16px;
    padding-top: calc(10px + var(--safe-top));
    padding-left: calc(16px + var(--safe-left));
    padding-right: calc(16px + var(--safe-right));
    text-align: center;
    font-size: 18px;
    font-weight: 700;
    letter-spacing: 0.5px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
  }

  .copyright {
    font-size: 11px;
    font-weight: 400;
    opacity: 0.85;
  }

  #headerImage {
    position: absolute;
    right: calc(16px + var(--safe-right));
    top: 50%;
    transform: translateY(-50%);
    height: 70%;
    width: auto;
    z-index: 0;
  }

  @media (max-width: 500px) {
    #headerImage {
      display: none;
    }
  }

  #chat {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 20px 24px;
    padding-left: calc(24px + var(--safe-left));
    padding-right: calc(24px + var(--safe-right));
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  .message {
    margin: 0;
    padding: 12px 16px;
    border-radius: var(--border-radius);
    max-width: 75%;
    box-shadow: var(--shadow);
    line-height: 1.6;
    word-wrap: break-word;
    overflow-wrap: break-word;
    animation: msgFadeIn 0.3s ease-in;
    font-size: 15px;
  }

  .message img.att-image {
    max-width: 100%;
    max-height: 300px;
    border-radius: 8px;
    margin-top: 8px;
    display: block;
    cursor: pointer;
    transition: opacity 0.2s;
  }
  .message img.att-image:hover { opacity: 0.85; }

  .message .file-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #e8f0fe;
    border: 1px solid #a8c7fa;
    border-radius: 8px;
    padding: 6px 12px;
    margin-top: 8px;
    font-size: 13px;
    color: #1a73e8;
    font-weight: 500;
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .user {
    background: var(--user-bg);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
  }

  .bot {
    background: var(--bot-bg);
    border: 1px solid #e0e0e0;
    align-self: flex-start;
    border-bottom-left-radius: 4px;
  }

  .bot.processing {
    position: relative;
    overflow: visible;
    padding: 16px 20px;
    min-height: 48px;
    display: flex;
    align-items: center;
  }

  .bot pre {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .typing {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    line-height: 1;
  }

  .dots { display: inline-flex; gap: 4px; padding: 0; line-height: 1; }
  .dot {
    width: 7px; height: 7px; border-radius: 999px;
    background: #94a3b8; opacity: 0.35;
    animation: bounce 1.1s infinite ease-in-out;
  }
  .dot:nth-child(2) { animation-delay: 0.15s; }
  .dot:nth-child(3) { animation-delay: 0.30s; }

  @keyframes bounce {
    0%, 80%, 100% { transform: translateY(0); opacity: 0.35; }
    40% { transform: translateY(-5px); opacity: 0.9; }
  }

  .stream-cursor {
    display: inline-block;
    width: 8px;
    margin-left: 2px;
    border-left: 2px solid rgba(53, 122, 189, 0.9);
    height: 1em;
    transform: translateY(2px);
    animation: blink 0.9s step-end infinite;
  }

  @keyframes blink {
    0%, 50% { opacity: 1; }
    50.01%, 100% { opacity: 0; }
  }

  .bot h1, .bot h2, .bot h3, .bot h4 {
    margin: 16px 0 8px 0;
    color: var(--secondary-color);
    line-height: 1.3;
  }
  .bot h1 { font-size: 1.5em; font-weight: 700; }
  .bot h2 { font-size: 1.3em; font-weight: 700; }
  .bot h3 { font-size: 1.1em; font-weight: 600; }
  .bot h4 { font-size: 1em; font-weight: 600; }

  .bot p { margin: 8px 0; }
  .bot ul, .bot ol { margin: 8px 0; padding-left: 24px; }
  .bot code {
    background: #f4f4f4;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    font-size: 14px;
    color: #d63384;
    word-break: break-word;
  }
  .bot pre {
    background: #f4f4f4;
    padding: 12px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 8px 0;
  }
  .bot pre code {
    word-break: normal;
    white-space: pre;
  }

  /* ========= Markdown tables ========= */
  .table-wrap {
    width: 100%;
    overflow-x: auto;
    margin: 0.8rem 0;
  }

  .table-wrap table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95em;
  }

  .table-wrap th,
  .table-wrap td {
    padding: 0.55rem 0.7rem;
    border: 1px solid rgba(128, 128, 128, 0.35);
    vertical-align: top;
  }

  .table-wrap th {
    font-weight: 700;
    background: rgba(128, 128, 128, 0.12);
  }

  .table-wrap tr:nth-child(even) td {
    background: rgba(128, 128, 128, 0.05);
  }

  /* ========= Interactive quizzes ========= */
  .quiz-card {
    margin: 0.8rem 0;
    padding: 0.9rem;
    border: 1px solid rgba(0, 131, 56, 0.35);
    border-radius: var(--border-radius);
    background: rgba(0, 131, 56, 0.05);
  }

  .quiz-title {
    margin: 0 0 0.6rem;
    font-size: 1.02em;
    font-weight: 700;
    color: var(--secondary-color);
  }

  .quiz-prompt {
    margin: 0 0 0.5rem;
    font-weight: 600;
  }

  .quiz-options {
    display: grid;
    gap: 0.4rem;
  }

  .quiz-option {
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
    padding: 0.45rem 0.6rem;
    border-radius: 7px;
    cursor: pointer;
  }

  .quiz-option:hover { background: rgba(0, 131, 56, 0.1); }

  .quiz-option input {
    margin: 0.25rem 0 0;
    accent-color: var(--primary-color);
    flex-shrink: 0;
  }

  /* Class selectors intentionally override the global button rules,
     including the compact phone layout further below. */
  .quiz-navigation button,
  .quiz-retry,
  .quiz-submit-results {
    padding: 0.5rem 0.9rem;
    border: none;
    border-radius: 8px;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    font-weight: 600;
    font-size: 0.9em;
    cursor: pointer;
    min-width: 0;
    max-width: none;
    height: auto;
    flex: 0 0 auto;
    box-shadow: none;
  }

  .quiz-navigation {
    display: flex;
    flex-wrap: wrap;
    gap: 0.65rem;
    margin-top: 1rem;
  }

  .quiz-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.65rem;
    margin-top: 1rem;
  }

  .quiz-navigation button:disabled {
    opacity: 0.45;
    cursor: not-allowed;
  }

  .quiz-submit-results {
    background: #2563eb;
  }

  .quiz-submit-results:hover:not(:disabled) {
    background: #1d4ed8;
  }

  .quiz-submit-results:disabled {
    opacity: 0.6;
    cursor: default;
  }

  .quiz-feedback {
    margin-top: 0.5rem;
    font-size: 0.92em;
    color: #b3261e;
  }

  .quiz-progress {
    margin: 0 0 0.75rem;
    color: #6b7280;
    font-size: 0.9rem;
  }

  .quiz-score {
    margin: 0.6rem 0 0;
    font-weight: 700;
  }

  .quiz-review {
    margin-top: 0.85rem;
    padding: 0.8rem;
    border-left: 4px solid;
    border-radius: 8px;
    background: rgba(128, 128, 128, 0.08);
  }

  .quiz-review.is-correct { border-left-color: #22a06b; }
  .quiz-review.is-incorrect { border-left-color: #dc4c64; }

  .quiz-review p {
    margin: 0.3rem 0;
  }

  .quiz-explanation {
    color: #6b7280;
    font-size: 0.92em;
  }

  .quiz-error {
    margin: 0.8rem 0;
    padding: 0.6rem 0.8rem;
    border: 1px solid #f0b9b3;
    border-radius: 8px;
    background: #fdecea;
    color: #b3261e;
    font-size: 0.92em;
  }

  /* ========= Timestamp ========= */
  .message-timestamp {
    font-size: 11px;
    margin-top: 8px;
    line-height: 1;
    opacity: 0.75;
    user-select: none;
    letter-spacing: 0.2px;
  }
  .user .message-timestamp {
    text-align: right;
    color: #5a8a6a;
  }
  .bot .message-timestamp {
    text-align: left;
    color: #aaa;
  }

  /* ========= Input Area ========= */
  #inputArea {
    display: flex;
    flex-direction: column;
    padding: 0;
    padding-bottom: var(--safe-bottom);
    background: #fff;
    border-top: 1px solid #e0e0e0;
    box-shadow: 0 -2px 12px rgba(0, 0, 0, 0.05);
    flex-shrink: 0;
  }

  /* Attachment preview area */
  #attachmentArea {
    display: none;
    padding: 10px 16px;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
    border-bottom: 1px solid #eee;
    background: #fafbfc;
    overflow-x: auto;
  }
  #attachmentArea.active { display: flex; }

  .att-item {
    display: flex;
    align-items: center;
    gap: 6px;
    background: #e8f0fe;
    border: 1px solid #a8c7fa;
    border-radius: 8px;
    padding: 5px 10px;
    font-size: 13px;
    max-width: 220px;
    animation: msgFadeIn 0.2s ease-in;
  }
  .att-item img {
    max-height: 40px;
    max-width: 60px;
    border-radius: 4px;
    object-fit: cover;
  }
  .att-item .att-icon { font-size: 18px; flex-shrink: 0; }
  .att-item .att-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 120px;
    color: #333;
    font-weight: 500;
  }
  .att-item.processing .att-name::after { content: " ⏳"; }
  .att-item .att-remove {
    background: #e74c3c;
    color: white;
    border: none;
    border-radius: 50%;
    width: 20px;
    height: 20px;
    cursor: pointer;
    font-size: 11px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    transition: background 0.15s;
  }
  .att-item .att-remove:hover { background: #c0392b; }

  #inputRow {
    display: flex;
    align-items: flex-end;
    padding: 8px;
    padding-left: calc(8px + var(--safe-left));
    padding-right: calc(8px + var(--safe-right));
    gap: 8px;
  }

  #messageInput {
    flex: 1;
    min-width: 0;
    padding: 12px 14px;
    font-size: 16px; /* 16px prevents iOS zoom on focus */
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    outline: none;
    resize: none;
    overflow-y: auto;
    font-family: inherit;
    line-height: 1.5;
    max-height: 150px;
    min-height: 44px;
  }
  #messageInput:focus { border-color: var(--primary-color); }

  .button-group {
    display: flex;
    gap: 6px;
    align-items: flex-end;
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  button {
    padding: 10px 12px;
    color: white;
    border: none;
    border-radius: 10px;
    cursor: pointer;
    font-weight: 600;
    font-size: 18px;
    transition: background 0.2s ease, transform 0.1s ease, box-shadow 0.2s ease;
    box-shadow: var(--shadow);
    min-width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    flex-shrink: 0;
  }

  button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    transform: none;
  }

  input[type="file"] { display: none; }

  .upload-hint {
    font-size: 11px;
    color: #aaa;
    text-align: center;
    padding: 0 8px 6px;
    line-height: 1.4;
  }

  /* ========= Overlays & Modals ========= */
  #dropOverlay {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(74, 144, 226, 0.12);
    border: 3px dashed var(--primary-color);
    z-index: 500;
    justify-content: center;
    align-items: center;
    font-size: 22px;
    color: var(--primary-color);
    font-weight: 700;
    pointer-events: none;
  }
  #dropOverlay.active { display: flex; }

  #imageModal {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.85);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    padding: 16px;
  }
  #imageModal.active { display: flex; }
  #imageModal img { max-width: 100%; max-height: 100%; border-radius: 8px; }

  #toast {
    position: fixed;
    bottom: calc(90px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    background: rgba(30, 30, 30, 0.92);
    color: white;
    padding: 10px 22px;
    border-radius: 10px;
    font-size: 14px;
    display: none;
    z-index: 2000;
    max-width: 90%;
    text-align: center;
    box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  }
  #toast.show { display: block; animation: toastFadeIn 0.25s ease-in; z-index: 22000;}

  @keyframes toastFadeIn {
    from { opacity: 0; transform: translateY(10px) translateX(-50%); }
    to { opacity: 1; transform: translateY(0) translateX(-50%); }
  }

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

  /* ========= Draw Modal ========= */
  #drawModal {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 900;
    justify-content: center;
    align-items: center;
    padding: 12px;
    padding-top: calc(12px + var(--safe-top));
    padding-bottom: calc(12px + var(--safe-bottom));
  }
  #drawModal.active { display: flex; }

  #drawContainer {
    background: white;
    border-radius: 14px;
    padding: 14px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
    max-width: 1100px;
    max-height: 100%;
    overflow: auto;
    box-shadow: 0 8px 40px rgba(0,0,0,0.3);
  }
  #drawToolbar {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
  }

  .draw-colors {
    display: flex;
    gap: 6px;
    align-items: center;
    flex-wrap: wrap;
  }

  .draw-color-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 3px solid #ddd;
    cursor: pointer;
    padding: 0;
    min-width: 32px;
    box-shadow: none;
    background: #000;
    transition: border-color 0.15s, box-shadow 0.15s;
  }
  .draw-color-btn.active {
    border-color: #333;
    box-shadow: 0 0 0 3px rgba(74, 144, 226, 0.5);
  }

  .draw-tool-btn {
    background: #f0f0f0;
    color: #333;
    border: 2px solid #ddd;
    border-radius: 8px;
    padding: 0;
    min-width: 38px;
    height: 38px;
    font-size: 16px;
    box-shadow: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: border-color 0.15s, background 0.15s;
  }
  .draw-tool-btn:hover { background: #e4e4e4; }
  .draw-tool-btn.active {
    border-color: var(--primary-color);
    background: #e0edff;
  }

  .draw-size {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: #555;
  }
  .draw-size input[type="range"] {
    width: 80px;
    accent-color: var(--primary-color);
  }

  #drawCanvas {
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    cursor: crosshair;
    touch-action: none;
    display: block;
    background: #fff;
    max-width: 100%;
    height: auto;
  }

  #drawFooter {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    flex-wrap: wrap;
  }

  .draw-action-btn {
    padding: 10px 18px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    border: none;
    min-width: auto;
    height: auto;
    box-shadow: none;
    transition: background 0.15s, opacity 0.15s;
  }
  .draw-action-btn.cancel-btn {
    background: #e0e0e0;
    color: #333;
  }
  .draw-action-btn.cancel-btn:hover { background: #d0d0d0; }
  .draw-action-btn.attach-btn {
    background: linear-gradient(135deg, #27ae60, #219a52);
    color: white;
  }
  .draw-action-btn.attach-btn:hover { opacity: 0.9; }
  .draw-action-btn.send-btn {
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
  }
  .draw-action-btn.send-btn:hover { opacity: 0.9; }

  /* ==================================================== */
  /* ================= MOBILE OPTIMIZATIONS ============= */
  /* ==================================================== */

  /* Tablets and smaller */
  @media (max-width: 768px) {
    header {
      font-size: 16px;
      padding: 8px 12px;
      padding-top: calc(8px + var(--safe-top));
    }
    .copyright { font-size: 10px; }

    #chat {
      padding: 14px;
      padding-left: calc(14px + var(--safe-left));
      padding-right: calc(14px + var(--safe-right));
      gap: 12px;
    }

    .message {
      max-width: 88%;
      padding: 10px 14px;
      font-size: 14.5px;
    }

    .message img.att-image { max-height: 240px; }

    .bot h1 { font-size: 1.3em; }
    .bot h2 { font-size: 1.15em; }
    .bot h3 { font-size: 1.05em; }

    #inputRow {
      padding: 6px;
      padding-left: calc(6px + var(--safe-left));
      padding-right: calc(6px + var(--safe-right));
      gap: 6px;
    }

    .button-group { gap: 4px; }

    button {
      min-width: 40px;
      height: 40px;
      padding: 8px;
      font-size: 16px;
      border-radius: 8px;
    }

    #messageInput {
      min-height: 40px;
      padding: 10px 12px;
    }

    .upload-hint {
      font-size: 10px;
      padding: 0 6px 6px;
    }

    #attachmentArea {
      padding: 8px 12px;
      gap: 6px;
    }

    .att-item {
      font-size: 12px;
      padding: 4px 8px;
      max-width: 180px;
    }
    .att-item .att-name { max-width: 90px; }

    #toast {
      font-size: 13px;
      padding: 9px 18px;
      bottom: calc(80px + var(--safe-bottom));
    }

    #drawToolbar { gap: 6px; }
    .draw-size input[type="range"] { width: 60px; }
  }

  /* Phones */
  @media (max-width: 480px) {
    header {
      font-size: 15px;
      padding: 7px 10px;
      padding-top: calc(7px + var(--safe-top));
    }

    #chat {
      padding: 10px;
      padding-left: calc(10px + var(--safe-left));
      padding-right: calc(10px + var(--safe-right));
      gap: 10px;
    }

    .message {
      max-width: 92%;
      padding: 9px 12px;
      font-size: 14px;
    }

    .message-timestamp { font-size: 10px; margin-top: 6px; }

    /* Allow buttons to wrap to a second row if needed */
    #inputRow {
      flex-wrap: wrap;
      padding: 6px;
    }

    #messageInput {
      flex: 1 1 100%;
      order: -1;
      min-height: 42px;
      margin-bottom: 4px;
    }

    .button-group {
      flex: 1 1 100%;
      justify-content: space-between;
      gap: 4px;
    }

    button {
      min-width: 0;
      flex: 1 1 0;
      max-width: 56px;
      height: 40px;
      padding: 6px;
      font-size: 16px;
    }

    #sendBtn {
      flex: 1.5 1 0;
      max-width: 80px;
    }

    .upload-hint {
      font-size: 10px;
      padding: 0 4px 4px;
    }

    .att-item { max-width: 160px; }
    .att-item .att-name { max-width: 70px; }

    /* Draw modal compact */
    #drawModal { padding: 6px; padding-top: calc(6px + var(--safe-top)); padding-bottom: calc(6px + var(--safe-bottom)); }
    #drawContainer { padding: 10px; gap: 8px; border-radius: 10px; }

    .draw-color-btn { width: 28px; height: 28px; min-width: 28px; border-width: 2px; }
    .draw-tool-btn { min-width: 34px; height: 34px; font-size: 14px; }
    .draw-size { font-size: 12px; }
    .draw-size input[type="range"] { width: 50px; }

    .draw-action-btn {
      padding: 9px 14px;
      font-size: 13px;
      flex: 1 1 0;
    }
    #drawFooter { gap: 6px; }

    #dropOverlay { font-size: 18px; border-width: 2px; }
  }

  /* Very small phones */
  @media (max-width: 360px) {
    .message { max-width: 95%; font-size: 13.5px; }
    button { height: 38px; font-size: 15px; }
    #messageInput { min-height: 40px; font-size: 16px; }
  }

  /* Landscape orientation on short-height devices */
  @media (max-height: 500px) and (orientation: landscape) {
    header { padding: 4px 12px; font-size: 14px; }
    .copyright { display: none; }
    #chat { padding: 8px 14px; gap: 8px; }
    .upload-hint { display: none; }
    #inputRow { padding: 4px 6px; }
    button { height: 38px; }
    #messageInput { min-height: 38px; }
  }
/* ========= Global button hover (no motion) ========= */

button {
  transition: background 0.2s ease,
              filter 0.2s ease,
              opacity 0.2s ease;
}

/* Hover: only on devices with a real pointer (avoids "sticky" hover on phones) */
@media (hover: hover) and (pointer: fine) {
  button:not(:disabled):hover {
    filter: brightness(1.12);
  }
}

/* Keyboard accessibility */
button:focus-visible {
  outline: 3px solid var(--primary-color);
  outline-offset: 2px;
}

/* These buttons define their own `transition`, which would override the one above.
   Re-declaring it here lets the brightness change fade smoothly. */
.att-item .att-remove,
.draw-action-btn,
.draw-color-btn,
.draw-tool-btn {
  transition: background 0.2s ease,
              filter 0.2s ease,
              opacity 0.2s ease,
              border-color 0.2s ease,
              box-shadow 0.2s ease;
}

/* Quiz submit already darkens its background on hover, so skip the brightness change */
.quiz-submit-results:hover:not(:disabled) {
  filter: none;
}

dialog {
  box-sizing: border-box;
  width: min(560px, 90vw);  /* bigger, but never wider than the screen */
  max-height: 70vh;         /* don't overflow small screens */
  min-height: min(92vw, 400px);
  overflow: auto;           /* scroll inside if content is long */
  padding: 24px;            /* stays inside the width now */
}
/* ================= AI Enhanced Artifact: layout ================= */
html, body { height: 100%; }
body.aea-body {
  display: block;
  height: 100vh;
  height: 100dvh;
  margin: 0;
  overflow: hidden;
}
[hidden] { display: none !important; }

#aeaApp {
  --aea-chat-w: 25%;
  display: flex;
  width: 100%;
  height: 100%;
  position: relative;
  background: var(--background, #f5f7fa);
}

#aeaContentPane {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  position: relative;
  overflow: hidden;
  background: #fff;
}
#aeaFrame {
  flex: 1 1 auto;
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  background: #fff;
}

#aeaChatPane {
  flex: 0 0 var(--aea-chat-w);
  min-width: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--background, #f5f7fa);
  container-type: inline-size;
  container-name: aeachat;
}
#aeaApp:not(.dragging) #aeaChatPane,
#aeaApp:not(.dragging) #aeaContentPane { transition: flex-basis .18s ease, flex-grow .18s ease; }
#aeaChatPane > #chat { min-height: 0; }

#aeaApp.mode-chat #aeaChatPane,
#aeaApp.content-hidden #aeaChatPane { flex: 1 1 0; }
#aeaApp.content-hidden #aeaContentPane { flex: 0 0 0; }
#aeaApp.chat-hidden #aeaChatPane { flex: 0 0 0; }
#aeaApp.chat-hidden #aeaContentPane { flex: 1 1 0; }
#aeaApp.dragging iframe { pointer-events: none; }
body.aea-dragging { cursor: col-resize; user-select: none; }

/* Splitter + discreet dock */
#aeaSplitter {
  flex: 0 0 8px;
  position: relative;
  z-index: 20;
  cursor: col-resize;
  touch-action: none;
  outline: none;
  background: #e3e7ee;
  border-left: 1px solid #d6dbe4;
  border-right: 1px solid #d6dbe4;
}
#aeaSplitter:hover,
#aeaSplitter:focus-visible,
#aeaApp.dragging #aeaSplitter { background: #cfd6e2; }

#aeaDock {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 21;
}
#aeaApp.content-hidden #aeaDock { left: 0; transform: translate(0, -50%); }
#aeaApp.chat-hidden #aeaDock { left: auto; right: 0; transform: translate(0, -50%); }

#aeaDock .aea-dock-btn {
  all: unset;
  box-sizing: border-box;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  border: 1px solid #c5ccd8;
  background: #fff;
  color: #3b4556;
  box-shadow: 0 1px 3px rgba(0,0,0,.12);
  font: 600 11px/1 system-ui, sans-serif;
  cursor: pointer;
  opacity: .78;
}
#aeaDock .aea-dock-btn:hover { opacity: 1; background: #f1f5ff; }
#aeaDock .aea-dock-btn:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 1px; }
#aeaDock .aea-dock-btn:disabled { opacity: .25; cursor: default; }
#aeaDock .aea-dock-btn[hidden] { display: none !important; }

/* Floating "show chat" button over the material pane (visible only when chat is hidden) */
.aea-chat-fab {
  all: unset;
  box-sizing: border-box;
  position: absolute;
  right: 18px;
  bottom: 10px;
  z-index: 40;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  line-height: 1;
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  color: #fff;
  box-shadow: 0 4px 14px rgba(0,0,0,.28);
  cursor: pointer;
  opacity: .92;
  transition: opacity .15s ease, transform .15s ease;
}
.aea-chat-fab:hover { opacity: 1; transform: scale(1.07); }
.aea-chat-fab:active { transform: scale(.96); }
.aea-chat-fab:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 2px; }
.aea-chat-fab:disabled { opacity: .3; cursor: default; transform: none; }
#aeaApp:not(.is-mobile) .aea-chat-fab { display: flex; }

/* Mobile: one pane at a time */
#aeaMobileSwitch { display: none; }
#aeaApp.is-mobile #aeaSplitter { display: none; }
#aeaApp.is-mobile ~ #aeaMobileSwitch {
  display: flex;
  position: fixed;
  top: calc(6px + env(safe-area-inset-top, 0px));
  left: calc(6px + env(safe-area-inset-left, 0px));
  z-index: 60;
  padding: 3px;
  gap: 2px;
  border-radius: 999px;
  background: rgba(255,255,255,.92);
  box-shadow: 0 1px 6px rgba(0,0,0,.18);
}
#aeaMobileSwitch button {
  all: unset;
  box-sizing: border-box;
  width: 34px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  font-size: 15px;
  cursor: pointer;
}
#aeaMobileSwitch button.is-active { background: var(--primary-color); }

.aea-empty {
  margin: auto;
  padding: 24px;
  color: #556;
  font: 15px/1.5 system-ui, sans-serif;
  text-align: center;
}

/* Narrow chat pane: mimic the phone layout of the tutor */
@container aeachat (max-width: 560px) {
  #chat { padding: 12px; gap: 12px; }
  .message { max-width: 94%; padding: 10px 13px; font-size: 14.5px; }
  #headerImage { display: none; }
  #pageHeader { font-size: 15px; }
  #inputRow { flex-wrap: wrap; }
  #messageInput { flex: 1 1 100%; order: -1; margin-bottom: 4px; }
  .button-group { flex: 1 1 100%; justify-content: flex-end; }
}

/* ================= Tool buttons in chat ================= */
.aea-tools { display: flex; flex-direction: column; gap: 6px; margin: 10px 0 2px; }
.aea-tool { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.message .aea-tool .aea-tool-btn {
  all: unset;
  box-sizing: border-box;
  flex: 1 1 200px;
  min-width: 0;
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 7px 10px;
  border: 1px solid #d6dbe4;
  border-left: 4px solid var(--aea-tool-c, #c5ccd8);
  border-radius: 9px;
  background: #f8fafc;
  color: #1f2937;
  font: inherit;
  font-size: 13px;
  line-height: 1.35;
  text-align: left;
  cursor: pointer;
}
.message .aea-tool .aea-tool-btn:hover { background: #eef3fb; }
.message .aea-tool .aea-tool-btn:focus-visible { outline: 2px solid var(--primary-color); outline-offset: 1px; }
.message .aea-tool .aea-tool-btn.is-active {
  background: color-mix(in srgb, var(--aea-tool-c, #fff176) 40%, white);
  border-color: color-mix(in srgb, var(--aea-tool-c, #fff176) 70%, #888);
}
.message .aea-tool .aea-tool-btn.is-missing { opacity: .6; }
.aea-tool-icon { flex: 0 0 auto; font-size: 15px; line-height: 1.25; }
.aea-tool-body { display: flex; flex-direction: column; min-width: 0; }
.aea-tool-text { font-weight: 600; overflow-wrap: anywhere; }
.aea-tool-desc { font-size: 11.5px; color: #5b6474; }

.aea-swatches { display: flex; gap: 5px; align-items: center; padding: 0 2px; }
.message .aea-swatches .aea-swatch {
  all: unset;
  box-sizing: border-box;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 0 0 1px #b9c1cd;
  cursor: pointer;
}
.message .aea-swatches .aea-swatch.is-sel { box-shadow: 0 0 0 2px #334155; }
.message .aea-swatches .aea-swatch:focus-visible { outline: 2px solid var(--primary-color); }
.aea-sw-yellow { background: #fff176 !important; }
.aea-sw-green  { background: #b9f6ca !important; }
.aea-sw-blue   { background: #b3e5fc !important; }
.aea-sw-pink   { background: #f8bbd0 !important; }
.aea-sw-orange { background: #ffcc80 !important; }

.aea-tools-error {
  margin: .6rem 0;
  padding: .5rem .7rem;
  border: 1px solid #f0b9b3;
  border-radius: 8px;
  background: #fdecea;
  color: #b3261e;
  font-size: .9em;
}

/* ================= Popover / selection menu ================= */
.aea-pop {
  position: fixed;
  z-index: 1600;
  width: min(340px, calc(100vw - 16px));
  padding: 10px 12px;
  border: 1px solid #e3cf7a;
  border-radius: 10px;
  background: #fffdf3;
  color: #2b2b2b;
  box-shadow: 0 8px 28px rgba(0,0,0,.18);
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
.aea-pop.is-user { background: #f3fbf4; border-color: #9fd3a8; }
.aea-pop-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
  color: #6b5a14;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}
.aea-pop.is-user .aea-pop-head { color: #1f6b33; }
.aea-pop-body { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 40vh; overflow: auto; }
.aea-pop textarea {
  box-sizing: border-box;
  width: 100%;
  min-height: 90px;
  padding: 8px;
  border: 1px solid #c9ced8;
  border-radius: 7px;
  font: 14px/1.4 system-ui, sans-serif;
  resize: vertical;
}
.aea-pop-foot { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; margin-top: 8px; }

.aea-pop .aea-mini,
.aea-pop .aea-x,
.aea-selmenu .aea-mini {
  all: unset;
  box-sizing: border-box;
  padding: 5px 10px;
  border-radius: 7px;
  border: 1px solid #d3d9e3;
  background: #eef1f6;
  color: #263142;
  font: 600 12px/1.2 system-ui, sans-serif;
  cursor: pointer;
}
.aea-pop .aea-x { padding: 2px 7px; border: none; background: transparent; font-size: 13px; }
.aea-pop .aea-mini.primary {
  border-color: transparent;
  background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
  color: #fff;
}
.aea-pop .aea-mini.danger { color: #b42318; }
.aea-pop .aea-mini:focus-visible,
.aea-selmenu .aea-mini:focus-visible { outline: 2px solid var(--primary-color); }

.aea-selmenu {
  position: fixed;
  z-index: 1600;
  display: flex;
  gap: 2px;
  padding: 4px;
  border-radius: 9px;
  background: #1f2937;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
}
.aea-selmenu .aea-mini { border: none; background: transparent; color: #fff; }
.aea-selmenu .aea-mini:hover { background: rgba(255,255,255,.14); }

.aea-toast-fallback {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 3000;
  max-width: 90%;
  padding: 9px 18px;
  border-radius: 10px;
  background: rgba(30,30,30,.92);
  color: #fff;
  font: 14px system-ui, sans-serif;
}

