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

html, body {
    height: 100%;
    width: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: #f4f4f9;
    overflow: hidden;
    overscroll-behavior: none;
}

#app {
    display: flex;
    flex-direction: row;
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    overflow: hidden;
}

/* ===== LEFT PANEL: Question + Explanation (1/3) ===== */
#reaction-container {
    width: 33.333%;
    min-width: 280px;
    height: 100%;
    background-color: #ffffff;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    border-right: 1px solid #e0e0e0;
    padding: 24px 20px;
    overflow-y: auto;
    z-index: 10;
    flex-shrink: 0;
}

#question-instruction {
    font-family: "Latin Modern", "Computer Modern", "Cambria Math", "Georgia", "Times New Roman", serif;
    font-size: 1.15rem;
    font-weight: 400;
    color: #000000;
    margin-bottom: 12px;
    line-height: 1.5;
    width: 100%;
}

#explanation-display {
    margin-top: 15px;
    padding: 15px;
    border-top: 1px dashed #c7c7cc;
    font-size: 0.95rem;
    color: #48484a;
    line-height: 1.4;
    width: 100%;
    display: none;
    background: #fdfdfd;
    border-radius: 12px;
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0;
}

#explanation-text-content {
    font-style: italic;
    margin-bottom: 15px;
}

#chat-interface {
    border-top: 1px solid #ebebeb;
    padding-top: 15px;
}

#chat-messages {
    max-height: 150px;
    overflow-y: auto;
    margin-bottom: 10px;
    font-size: 0.9rem;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.chat-msg {
    padding: 8px 12px;
    border-radius: 12px;
    max-width: 90%;
}

.user-msg {
    align-self: flex-end;
    background: #e1f5fe;
    color: #01579b;
}

.bot-msg {
    align-self: flex-start;
    background: #f2f2f7;
    color: #1c1c1e;
}

.chat-input-row {
    display: flex;
    gap: 8px;
}

#followup-input {
    flex: 1;
    padding: 10px;
    border: 1px solid #c7c7cc;
    border-radius: 12px;
    font-size: 0.9rem;
    outline: none;
}

#send-followup-btn {
    padding: 8px 15px;
    background: #007aff;
    color: white;
    border: none;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
}

#molecule-display {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 10px;
    width: 100%;
    overflow-x: auto;
    touch-action: pan-x;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 5px;
    flex-wrap: wrap;
}

/* ===== MESSAGE CONTAINER (bottom-right) ===== */
#message-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    left: auto;
    transform: none;
    width: 40%;
    max-width: 500px;
    min-width: 280px;
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(15px);
    border: 1px solid rgba(0, 0, 0, 0.1);
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    padding: 24px;
    border-radius: 24px;
    z-index: 2000;
    text-align: center;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
}


#loading-text {
    font-size: 1.1rem;
    color: #1d1d1f;
    line-height: 1.5;
    font-weight: 500;
    margin-right: 20px;
}

#freedraw-explain-btn {
    margin-top: 12px;
    padding: 10px 28px;
    background: #ff9500;
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, transform 0.2s;
}

#freedraw-explain-btn:hover {
    background: #e08600;
}

#freedraw-explain-btn:active {
    transform: scale(0.95);
}

#freedraw-explain-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#message-close-btn {
    position: absolute;
    top: 12px;
    right: 16px;
    background: none;
    border: none;
    font-size: 1.5rem;
    color: #8e8e93;
    cursor: pointer;
    line-height: 1;
    padding: 4px;
    transition: color 0.2s;
}

#message-close-btn:hover {
    color: #1d1d1f;
}

#message-restore-btn {
    position: fixed;
    bottom: 25px;
    right: 20px;
    left: auto;
    width: 50px;
    height: 50px;
    background: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.15);
    cursor: pointer;
    z-index: 1500;
    transition: transform 0.2s, background 0.2s;
    border: 1px solid rgba(0,0,0,0.05);
}

#message-restore-btn:hover {
    transform: scale(1.1);
    background: #f2f2f7;
}

#message-restore-btn:active {
    transform: scale(0.9);
}


#shrink-btn {
    position: absolute;
    top: 10px;
    right: 15px;
    background: none;
    border: none;
    font-size: 24px;
    color: #8e8e93;
    cursor: pointer;
    line-height: 1;
    padding: 5px;
    transition: color 0.2s;
}

#shrink-btn:hover {
    color: #1d1d1f;
}

#restore-btn {
    position: fixed;
    bottom: 25px;
    right: 20px;
    left: auto;
    width: 50px;
    height: 50px;
    background: #007aff;
    color: white;
    border: none;
    border-radius: 25px;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 122, 255, 0.3);
    z-index: 999;
    transition: transform 0.2s, background 0.2s;
}

#restore-btn:hover {
    transform: scale(1.1);
    background: #0062cc;
}

/* ===== MOBILE: stack vertically ===== */
@media (max-width: 768px) {
    #app {
        flex-direction: column;
        overflow-y: auto;
    }

    #reaction-container {
        width: 100%;
        min-width: unset;
        height: auto;
        max-height: 40vh;
        border-right: none;
        border-bottom: 1px solid #e0e0e0;
        flex-shrink: 0;
    }

    #whiteboard-container {
        height: 60vh;
        min-height: 400px;
    }

    #message-container {
        bottom: 140px;
        right: 10px;
        width: 90%;
        max-width: unset;
        min-width: unset;
    }

    #toolbar {
        position: fixed;
        top: auto;
        bottom: 15px;
        left: auto;
        right: 8px;
        transform: none;
        flex-direction: column;
        flex-wrap: nowrap;
        justify-content: flex-end;
        align-items: flex-end;
        width: auto;
        gap: 6px;
        padding: 0;
        background: none;
        backdrop-filter: none;
        border-top: none;
        overflow: visible;
        z-index: 9999;
    }

    #toolbar button {
        padding: 8px 10px;
        font-size: 0.75rem;
        white-space: nowrap;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    }

    #reaction-container {
        max-height: 50vh;
    }

    #question-instruction {
        font-size: 0.95rem;
        margin-bottom: 8px;
        line-height: 1.4;
        max-height: 25vh;
        overflow-y: auto;
    }

    #explanation-display {
        font-size: 0.82rem;
        line-height: 1.3;
        padding: 10px;
        margin-top: 10px;
        max-height: 25vh;
        overflow-y: auto;
    }

    #explanation-text-content {
        margin-bottom: 10px;
    }
}



#reaction-target {
    /* Make the LaTeX output reasonably large */
    font-size: 2rem;
    color: #333;
}

.reaction-arrow-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    margin: 0 15px;
    min-width: 80px;
}

.reagents-top, .conditions-bottom {
    font-size: 0.9rem;
    color: #48484a;
    text-align: center;
    width: 100%;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 4px;
}

.reagent-separator {
    margin: 0 4px;
    font-size: 1.1rem;
    color: #8e8e93;
    font-weight: 400;
}



.reagents-top {
    margin-bottom: 2px;
}

.conditions-bottom {
    margin-top: 2px;
}

.arrow-line {
    display: flex;
    align-items: center;
    width: 100%;
    min-width: 100px;
    height: 20px;
    position: relative;
    margin: 5px 0;
}

.arrow-line::before {
    content: '';
    flex: 1;
    height: 2px;
    background: #2c3e50;
    border-radius: 1px;
}

.arrow-line::after {
    content: '';
    width: 0;
    height: 0;
    border-top: 6px solid transparent;
    border-bottom: 6px solid transparent;
    border-left: 12px solid #2c3e50;
    margin-left: -2px;
}

.reaction-arrow-container canvas {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 0;
    margin: 0;
}


/* ===== RIGHT PANEL: Whiteboard (2/3) ===== */
#whiteboard-container {
    flex: 1 1 66.666%;
    position: relative;
    background-color: #fafafa;
    background-image: radial-gradient(#d7d7d7 1px, transparent 1px);
    background-size: 20px 20px;
    height: 100%;
    min-height: 0;
    overflow: hidden;
}

#toolbar {
    position: absolute;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    gap: 12px;
    z-index: 30;
}




#toolbar button {
    padding: 12px 18px;
    font-size: 1rem;
    font-weight: 500;
    background-color: #007aff;
    color: white;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.15);
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    touch-action: manipulation;
    display: flex;
    align-items: center;
    justify-content: center;
}

#toolbar button:active {
    transform: scale(0.95);
}

/* Whiteboard canvas (Fabric.js wraps it in .canvas-container) */
#whiteboard-container .canvas-container {
    width: 100% !important;
    /* Don't force height — canvas is intentionally taller than container for scrolling */
    z-index: 1;
}

#whiteboard-container canvas {
    display: block;
    cursor: crosshair;
}

/* Molecule canvases in reaction display */
#molecule-display canvas,
#explanation-text-content canvas {
    display: block;
}

.success-text {
    color: #27ae60 !important;
}

.error-text {
    color: #e74c3c !important;
}

/* Toolbar active state */
.active-tool {
    box-shadow: inset 0 3px 10px rgba(0, 0, 0, 0.3) !important;
    transform: scale(0.9) !important;
    filter: brightness(0.9);
}

/* Modal Styling */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(8px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.modal-content {
    background: white;
    padding: 40px;
    border-radius: 24px;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    overflow-y: auto;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    color: #1c1c1e;
}

.modal-content h2 {
    margin-top: 0;
    margin-bottom: 20px;
}

#topics-list {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 15px;
    margin: 25px 0;
    max-height: 250px; /* Limit height and scroll if too many topics */
    overflow-y: auto;
    padding: 5px;
    border: 1px solid #f2f2f7;
    border-radius: 12px;
}

/* Difficulty Slider */
#settings-difficulty-container {
    margin-bottom: 25px;
    padding-bottom: 15px;
    border-bottom: 1px solid #e0e0e0;
}

#settings-difficulty-container h3 {
    font-size: 1rem;
    margin-bottom: 12px;
    color: #333;
}

.difficulty-slider-row {
    padding: 0 5px;
}

#difficulty-slider {
    width: 100%;
    margin-bottom: 10px;
    cursor: pointer;
    accent-color: #5856d6;
}

.difficulty-labels {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: #8e8e93;
    font-weight: 600;
}

/* Learn Mode Toggle */
#settings-learn-mode-container {
    margin-bottom: 25px;
    padding-bottom: 15px;
    border-bottom: 1px solid #e0e0e0;
}

.switch-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    user-select: none;
}

.settings-hint {
    margin-top: 8px;
    font-size: 0.85rem;
    color: #8e8e93;
    line-height: 1.3;
}

#learn-mode-toggle {
    width: 44px;
    height: 24px;
    appearance: none;
    background: #e9e9eb;
    border-radius: 12px;
    position: relative;
    cursor: pointer;
    transition: background 0.3s;
}

#learn-mode-toggle:checked {
    background: #34c759;
}

#learn-mode-toggle::before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    background: white;
    border-radius: 50%;
    top: 2px;
    left: 2px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    transition: transform 0.3s;
}

#learn-mode-toggle:checked::before {
    transform: translateX(20px);
}

/* Inline Molecule in Explanation */

.inline-molecule {
    display: inline-flex;
    background: transparent;
    border: none;
    border-radius: 4px;
    margin: 0 4px;
    padding: 0;
    box-shadow: none;
    vertical-align: middle;
}


.sr-only-smiles {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.inline-molecule-explanation {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    background: white;
    border: 1px solid #f2f2f7;
    border-radius: 8px;
    margin: 2px 4px;
    padding: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.05);
    vertical-align: middle;
    max-width: 200px;
    overflow: hidden;
}

/* Inline formula (simple SMILES rendered as mhchem text) */
.inline-formula {
    display: inline;
    margin: 0 2px;
    vertical-align: baseline;
    font-size: 0.95em;
}

.inline-formula-explanation {
    display: inline;
    margin: 0 2px;
    vertical-align: baseline;
    font-size: 0.95em;
}




#custom-topic-container {
    margin-top: 25px;
    padding-top: 20px;
    border-top: 1px solid #e0e0e0;
}

#custom-topic-container h3 {
    font-size: 1rem;
    margin-bottom: 12px;
    color: #666;
}

.custom-topic-input-row {
    display: flex;
    gap: 10px;
}

#custom-topic-input {
    flex: 1;
    padding: 12px;
    border: 1px solid #c7c7cc;
    border-radius: 12px;
    font-size: 1rem;
    outline: none;
    -webkit-appearance: none;
    appearance: none;

}

#add-custom-topic-btn {
    padding: 12px 20px;
    background: #5856d6;
    color: white;
    border: none;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
}

.remove-topic-btn {
    margin-left: auto;
    background: #ff3b30;
    color: white;
    border: none;
    border-radius: 20px;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: bold;
    cursor: pointer;
}

.topic-item {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 1.1rem;
    cursor: pointer;
    padding: 10px;
    border-radius: 12px;
    transition: background 0.2s;
}

.topic-item:hover {
    background: #f2f2f7;
}

.topic-item input[type="checkbox"] {
    width: 24px;
    height: 24px;
    cursor: pointer;
}

.modal-actions {
    display: flex;
    justify-content: center;
    margin-top: 30px;
}

#save-settings-btn {
    padding: 15px 40px;
    background: #007aff;
    color: white;
    border: none;
    border-radius: 16px;
    font-size: 1.1rem;
    font-weight: 600;
    cursor: pointer;
    width: 100%;
}

#about-btn {
    background-color: rgba(255, 255, 255, 0.9) !important;
    color: #5856d6 !important;
    border: 1px solid #e0e0e0;
}

#about-content {
    max-height: 60vh;
    overflow-y: auto;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    font-size: 1.05rem;
    line-height: 1.6;
    color: #333;
    padding-right: 10px;
}

#topics-list, #explanation-display {
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
}

#about-content h2 {

    color: #5856d6;
    margin-bottom: 15px;
}

/* Settings Modes */
.mode-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #f2f2f7;
    padding: 16px;
    border-radius: 16px;
    margin-bottom: 20px;
}

.mode-info h3 {
    margin: 0 0 4px 0;
    font-size: 1.1rem;
}

#practice-mode-select {
    padding: 10px 14px;
    font-size: 1rem;
    font-weight: 600;
    border: 1px solid #c7c7cc;
    border-radius: 12px;
    background: white;
    color: #1c1c1e;
    cursor: pointer;
    outline: none;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%238e8e93' stroke-width='2' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
    padding-right: 36px;
    min-width: 160px;
    flex-shrink: 0;
    transition: border-color 0.2s, box-shadow 0.2s;
}

#practice-mode-select:focus {
    border-color: #5856d6;
    box-shadow: 0 0 0 3px rgba(88, 86, 214, 0.15);
}

#practice-mode-select:hover {
    border-color: #a1a1a6;
}

/* Switch Styles */
.switch {
  position: relative;
  display: inline-block;
  width: 50px;
  height: 28px;
  flex-shrink: 0;
  margin-left: 15px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #d1d1d6;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 22px;
  width: 22px;
  left: 3px;
  bottom: 3px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
  box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

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

input:focus + .slider {
  box-shadow: 0 0 1px #34c759;
}

input:checked + .slider:before {
  -webkit-transform: translateX(22px);
  -ms-transform: translateX(22px);
  transform: translateX(22px);
}

.slider.round {
  border-radius: 28px;
}

.slider.round:before {
  border-radius: 50%;
}
