/* ==========================================================
   AI Math Solver — exact pixel values from Figma
   ========================================================== */

/* Page wrapper — overrides the two-col flex from main-content */
.ams-page {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 32px;
}

/* ── Title section ── */
.ams-header {
    display: flex;
    flex-direction: column;
    gap: 8px;
    text-align: center;
    width: 100%;
}

.ams-title {
    font-size: 36px;
    font-weight: 700;
    line-height: 48px;
    color: #263842;
    margin: 0;
}

.ams-subtitle {
    font-size: 16px;
    font-weight: 400;
    line-height: 24px;
    color: #47677a;
    margin: 0;
}

/* ── Inner container (max 800 px) ── */
.ams-container {
    display: flex;
    flex-direction: column;
    gap: 24px;
    max-width: 800px;
    width: 100%;
}

/* ── Input box ── */
.ams-input-box {
    background: #f6f8f9;
    border: 1px solid #cedae0;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    align-content: stretch;
    overflow: hidden;
    width: 100%;
}

.ams-textarea-area {
    height: 180px;
    padding: 16px;
    display: flex;
    align-items: flex-start;
    justify-content: flex-start;
    flex-shrink: 0;
    width: 100%;
    box-sizing: border-box;
}

/* ── MathQuill wrapper & placeholder ── */
.ams-mathquill-wrap {
    position: relative;
    width: 100%;
    min-height: 148px;
}

#MathInput {
    width: 100%;
    min-height: 100%;
    cursor: text;
}

/* Remove MathQuill's default border — only touch decoration, never layout/font props */
#MathInput,
#MathInput.mq-editable-field,
#MathInput.mq-math-mode {
    border: none !important;
    outline: none !important;
    box-shadow: none !important;
    background: transparent !important;
}

#mathPlaceholder {
    position: absolute;
    top: 0;
    left: 0;
    pointer-events: none;
    color: #7a9aab;
    font-size: 16px;
    font-weight: 400;
    line-height: 24px;
}

/* ── Toolbar ── */
.ams-toolbar {
    background: #ffffff;
    border-top: 1px solid #cedae0;
    padding: 8px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    align-content: stretch;
    flex-shrink: 0;
    width: 100%;
    box-sizing: border-box;
}

.ams-toolbar-left {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-shrink: 0;
}

.ams-tool-btn {
    background: #ffffff;
    border: 1px solid #cedae0;
    display: flex;
    gap: 6px;
    align-items: center;
    justify-content: center;
    padding: 6px 12px;
    font-size: 14px;
    font-weight: 500;
    line-height: 20px;
    color: #47677a;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.15s;
}

.ams-tool-btn:hover {
    background: #edf1f3;
}

.ams-tool-btn svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

.ams-delete-btn {
    background: #ffffff;
    border: 1px solid #cedae0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    padding: 0;
    transition: background 0.15s;
}

.ams-delete-btn:hover {
    background: #edf1f3;
}

.ams-delete-btn svg {
    width: 18px;
    height: 18px;
}

/* ── Solve button ── */
.ams-solve-row {
    display: flex;
    align-items: center;
    justify-content: center;
}

.ams-solve-btn {
    background: #065a8d;
    border: none;
    display: flex;
    gap: 6px;
    align-items: center;
    justify-content: center;
    padding: 12px 24px;
    font-size: 16px;
    font-weight: 500;
    line-height: 24px;
    color: #ffffff;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    transition: background 0.15s;
}

.ams-solve-btn:hover {
    background: #054d78;
}

.ams-solve-btn svg {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

/* ── Result box ── */
.ams-result-box {
    background: #f6f8f9;
    border: 1px solid #cedae0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 300px;
    width: 100%;
}

/* Result header */
.ams-result-header {
    background: #ffffff;
    border-bottom: 1px solid #cedae0;
    padding: 8px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0;
}

.ams-result-title {
    font-size: 24px;
    font-weight: 700;
    line-height: 32px;
    color: #263842;
    flex: 1 0 0;
    min-width: 0;
}

.ams-result-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
}

.ams-action-item {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 5px 18px;
    font-size: 16px;
    font-weight: 400;
    line-height: 24px;
    color: #263842;
    cursor: pointer;
    white-space: nowrap;
    border-right: 1px solid #edf1f3;
    transition: background 0.15s;
}

.ams-action-item:hover {
    background: #f0f4f6;
}

.ams-action-last {
    border-right: none;
    padding-right: 0;
}

.ams-action-item svg {
    width: 26px;
    height: 26px;
    flex-shrink: 0;
}

/* Result body */
.ams-result-body {
    padding: 24px;
}

/* Answer section */
.ams-answer-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: center;
    justify-content: center;
    text-align: center;
    width: 100%;
}

.ams-answer-label {
    font-size: 16px;
    font-weight: 400;
    line-height: 24px;
    color: #263842;
    margin: 0;
}

.ams-answer-value {
    font-size: 32px;
    font-weight: 700;
    line-height: 38px;
    color: #065a8d;
    margin: 0;
    word-break: break-word;
}

/* Steps section */
.ams-steps-section {
    display: flex;
    flex-direction: column;
    gap: 36px;
    padding-top: 36px;
}

.ams-step-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.ams-step-label {
    font-size: 16px;
    font-weight: 700;
    line-height: 24px;
    color: #263842;
    margin: 0;
}

.ams-step-text {
    font-size: 16px;
    font-weight: 400;
    line-height: 24px;
    color: #263842;
    margin: 0;
}

.ams-final-answer-title {
    font-size: 16px;
    font-weight: 700;
    line-height: 24px;
    color: #263842;
    margin: 0;
}

/* ── Ask More row ── */
.ams-ask-more-row {
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 16px;
}

.ams-ask-more-btn {
    background: #263842;
    border: none;
    display: flex;
    gap: 6px;
    align-items: center;
    justify-content: center;
    padding: 12px 24px;
    font-size: 16px;
    font-weight: 500;
    line-height: 24px;
    color: #ffffff;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    transition: background 0.15s;
}

.ams-ask-more-btn:hover {
    background: #1a2830;
}

.ams-ask-more-btn svg {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

/* ==========================================================
   Math Keyboard  (exact values from Figma node 1515:2014)
   ========================================================== */
.ams-keyboard {
    background: #ffffff;
    border: 1px solid #edf1f3;
    box-shadow: 0px 0px 48px -8px rgba(0, 0, 0, 0.06);
    display: flex;
    flex-direction: column;
    width: 100%;
    overflow: hidden;
}

.ams-keyboard.d-none {
    display: none;
}

/* ── Reset global p,span overrides inside keyboard ── */
#mathKeyboard span {
    /* font-size: inherit !important; */
    line-height: 1 !important;
}

/* ── Tab row ── */
.ams-kb-tabs {
    background: #f6f8f9;
    border-bottom: 1px solid #edf1f3;
    padding: 12px;
    display: flex;
    gap: 10px;
    align-items: center;
    overflow-x: auto;
    flex-shrink: 0;
    scrollbar-width: none;
}

.ams-kb-tabs::-webkit-scrollbar { display: none; }

.ams-kb-tab {
    background: #ffffff;
    border: 1px solid #edf1f3;
    padding: 8px 12px;
    font-size: 16px;
    font-weight: 500;
    line-height: 24px;
    color: #000000;
    cursor: pointer;
    font-family: inherit;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.ams-kb-tab:hover {
    background: #edf1f3;
}

.ams-kb-tab--active {
    background: #000000;
    border-color: #000000;
    color: #ffffff;
}

.ams-kb-tab--active:hover {
    background: #1a1a1a;
}

/* ── Key panels ── */
.ams-kb-panel {
    display: none;
    flex-wrap: wrap;
    gap: 12px;
    padding: 12px;
    align-items: flex-start;
    align-content: flex-start;
}

.ams-kb-panel--active {
    display: flex;
}

/* ── Individual keys ── */
#mathKeyboard .ams-kb-key {
    background: #f6f8f9;
    border: none;
    flex: 1 0 0;
    min-width: 68px;
    max-width: 76px;
    height: 48px;
    padding: 12px 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px !important;
    font-weight: 600 !important;
    line-height: 1 !important;
    color: #000000 !important;
    cursor: pointer;
    font-family: inherit;
    text-align: center;
    white-space: nowrap;
    box-sizing: border-box;
    transition: background 0.15s;
    user-select: none;
}

#mathKeyboard .ams-kb-key:hover {
    background: #e4e9ec;
}

#mathKeyboard .ams-kb-key:active {
    background: #d0d8de;
}

#mathKeyboard .ams-kb-key--del {
    color: #47677a !important;
}

/* ── Fraction label inside key ── */
#mathKeyboard .kb-frac {
    display: inline-flex !important;
    flex-direction: column !important;
    align-items: center !important;
    gap: 2px !important;
    font-size: 12px !important;
    line-height: 1 !important;
}

#mathKeyboard .kb-frac-bar {
    width: 14px !important;
    height: 1px !important;
    background: currentColor !important;
    display: block !important;
}

/* ── Integral / Sigma with limits ── */
#mathKeyboard .kb-intlim {
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    line-height: 1 !important;
    gap: 1px !important;
}

#mathKeyboard .kb-intlim span:nth-child(1),
#mathKeyboard .kb-intlim span:nth-child(3) {
    font-size: 9px !important;
    font-weight: 600 !important;
    opacity: 0.7 !important;
    line-height: 1 !important;
}

#mathKeyboard .kb-intlim span:nth-child(2) {
    font-size: 18px !important;
    line-height: 18px !important;
}

/* ── Active state for Math Input toolbar button ── */
.ams-tool-btn--active {
    border-color: #263842 !important;
    color: #263842 !important;
    background: #edf1f3 !important;
}

/* ==========================================================
   Responsive
   ========================================================== */
@media (max-width: 767px) {
    .ams-title {
        font-size: 26px;
        line-height: 36px;
    }

    .ams-subtitle {
        font-size: 14px;
    }

    .ams-result-header {
        gap: 8px;
    }

    .ams-result-actions {
        width: 100%;
    }

    .ams-action-item {
        padding: 5px 12px;
        font-size: 14px;
    }

    .ams-action-last {
        padding-right: 0;
    }

    .ams-solve-btn,
    .ams-ask-more-btn {
        padding: 10px 18px;
        font-size: 14px;
    }
}

/* ── Validation shake ── */
.ams-input-box.ai-validate {
    border-color: #e53935;
    animation: ams-shake 0.3s;
}

@keyframes ams-shake {
    0%, 100% { transform: translateX(0); }
    25%       { transform: translateX(-4px); }
    75%       { transform: translateX(4px); }
}

/* ── Crop modal ── */
.ams-crop-modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(38, 56, 66, 0.65);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 9999;
    align-items: center;
    justify-content: center;
    padding: 20px;
    box-sizing: border-box;
    animation: ams-fade-in 0.2s ease-out;
}

@keyframes ams-fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

.ams-crop-modal-inner {
    background: #ffffff;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    width: 640px;
    max-width: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: ams-slide-up 0.25s ease-out;
}

@keyframes ams-slide-up {
    from { opacity: 0; transform: translateY(20px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

/* Modal header */
.ams-crop-header {
    background: #ffffff;
    border-bottom: 1px solid #cedae0;
    padding: 16px 20px;
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    flex-shrink: 0;
}

.ams-crop-header-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.ams-crop-title {
    font-size: 18px;
    font-weight: 700;
    line-height: 26px;
    color: #263842;
    margin: 0;
}

.ams-crop-subtitle {
    font-size: 13px;
    font-weight: 400;
    line-height: 18px;
    color: #47677a;
    margin: 0;
}

.ams-crop-close {
    background: #f6f8f9;
    border: 1px solid #cedae0;
    border-radius: 6px;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    padding: 0;
    transition: background 0.15s, border-color 0.15s;
}

.ams-crop-close:hover {
    background: #edf1f3;
    border-color: #b8c8d0;
}

/* Modal body — fixed cropper canvas size */
.ams-crop-body {
    padding: 20px;
    background: #f6f8f9;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.ams-crop-image-wrap {
    width: 100%;
    height: 400px;
    background: #263842;
    border-radius: 8px;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.ams-crop-modal-inner img#cropImage {
    display: block;
    max-width: 100%;
    max-height: 400px;
}

/* Modal actions */
.ams-crop-actions {
    background: #ffffff;
    border-top: 1px solid #cedae0;
    padding: 14px 20px;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    align-items: center;
    flex-shrink: 0;
}

.ams-crop-cancel-btn {
    background: #ffffff;
    border: 1px solid #cedae0;
    border-radius: 6px;
    padding: 9px 18px;
    font-size: 14px;
    font-weight: 500;
    line-height: 20px;
    color: #47677a;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s, border-color 0.15s;
}

.ams-crop-cancel-btn:hover {
    background: #edf1f3;
    border-color: #b8c8d0;
}

.ams-crop-confirm-btn {
    background: #065a8d;
    border: none;
    border-radius: 6px;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 9px 18px;
    font-size: 14px;
    font-weight: 500;
    line-height: 20px;
    color: #ffffff;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.15s;
}

.ams-crop-confirm-btn:hover {
    background: #054d78;
}

.ams-crop-confirm-btn svg {
    flex-shrink: 0;
}

/* Cropper.js — themed handles & box */
.ams-crop-image-wrap .cropper-view-box,
.ams-crop-image-wrap .cropper-face {
    border-radius: 2px;
}

.ams-crop-image-wrap .cropper-view-box {
    outline: 2px solid #065a8d;
    outline-color: #065a8d;
}

.ams-crop-image-wrap .cropper-line,
.ams-crop-image-wrap .cropper-point {
    background-color: #065a8d;
}

.ams-crop-image-wrap .cropper-point.point-se,
.ams-crop-image-wrap .cropper-point.point-sw,
.ams-crop-image-wrap .cropper-point.point-ne,
.ams-crop-image-wrap .cropper-point.point-nw {
    width: 10px;
    height: 10px;
    opacity: 1;
}

.ams-crop-image-wrap .cropper-dashed {
    border-color: rgba(255, 255, 255, 0.6);
}

/* Replace default checkered transparency pattern with themed solid color */
.ams-crop-image-wrap .cropper-bg {
    background-image: none !important;
    background-color: #263842 !important;
}

.ams-crop-image-wrap .cropper-modal {
    background-color: #263842;
    opacity: 0.65;
}

.ams-crop-image-wrap .cropper-container {
    background-color: #263842;
}

.ams-crop-image-wrap .cropper-wrap-box,
.ams-crop-image-wrap .cropper-canvas {
    background-color: #263842;
}

@media (max-width: 600px) {
    .ams-crop-modal { padding: 12px; }
    .ams-crop-modal-inner { width: 100%; }
    .ams-crop-image-wrap { height: 320px; }
    .ams-crop-modal-inner img#cropImage { max-height: 320px; }
    .ams-crop-header { padding: 14px 16px; }
    .ams-crop-body { padding: 16px; }
    .ams-crop-actions { padding: 12px 16px; }
}

/* math-card-image inserted into MathQuill */
.math-card-image {
    max-width: 100%;
    max-height: 120px;
    vertical-align: middle;
}

/* Drag-over highlight */
.ams-input-box.drag-over {
    border-color: #4caf50;
    border-style: dashed;
}

@media (max-width: 425px) {
    .ams-toolbar-left {
        flex-wrap: wrap;
    }

    .ams-tool-btn {
        padding: 5px 10px;
        font-size: 13px;
    }

    .ams-action-item {
        padding: 4px 8px;
        font-size: 13px;
    }

    .ams-answer-value {
        font-size: 26px;
    }
}
