:root {
    /* Apple Style Color Palette */
    --primary: #1D1D1F; 
    --action: #007AFF; /* iOS Native Blue */
    --action-light: #E5F1FF;
    --bg: #F2F2F7; /* iOS Grouped Background */
    --surface: #FFFFFF;
    --text: #1D1D1F;
    --text-muted: #86868B;
    --success: #34C759; /* iOS Green */
    --success-light: #E8F8EE;
    --error: #FF3B30; /* iOS Red */
    --error-light: #FFEBE9;
    --warning: #FF9500;
    --warning-light: #FFF4E5;
    --border: rgba(60, 60, 67, 0.12); /* Soft iOS Border */
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", Arial, sans-serif;
    -webkit-tap-highlight-color: transparent;
}

body {
    background-color: var(--bg);
    color: var(--text);
    padding-bottom: 90px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Header */
header {
    background-color: rgba(255, 255, 255, 0.85);
    backdrop-filter: saturate(180%) blur(20px);
    -webkit-backdrop-filter: saturate(180%) blur(20px);
    padding: 1rem;
    border-bottom: 0.5px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 50;
    display: flex;
    justify-content: center;
    align-items: center;
}

header h1 {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--primary);
    letter-spacing: 0.3px;
}

.container {
    max-width: 800px;
    margin: 0 auto;
    padding: 1.25rem 1rem;
}

.page { display: none; }
.page.active { display: block; animation: fadeIn 0.3s cubic-bezier(0.25, 0.8, 0.25, 1); }

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

/* Card - Apple Style */
.card {
    background-color: var(--surface);
    border-radius: 16px;
    padding: 1.5rem;
    margin-bottom: 1.25rem;
    box-shadow: 0 4px 20px rgba(0,0,0,0.04);
    border: 0.5px solid rgba(0,0,0,0.02);
}

/* Modern iOS Dropdowns & Inputs */
.filter-container {
    display: flex; gap: 10px; margin-bottom: 1.25rem; flex-wrap: wrap; align-items: stretch;
}
 
input, select, input[type="number"], input[type="text"] {
    padding: 0.85rem 1rem;
    border-radius: 12px;
    border: 1px solid rgba(0,0,0,0.05);
    font-size: 0.95rem;
    background: #FFFFFF; 
    color: var(--text);
    outline: none;
    transition: all 0.2s;
    width: 100%;
    box-shadow: 0 1px 3px rgba(0,0,0,0.02);
}

input:focus, select:focus {
    border-color: var(--action);
    box-shadow: 0 0 0 3px var(--action-light);
}

select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="%2386868B" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>');
    background-repeat: no-repeat;
    background-position: right 1rem center;
    background-size: 16px;
    padding-right: 2.5rem;
    cursor: pointer;
    font-weight: 500;
}

.filter-container input { flex: 2; min-width: 160px; }
.filter-container select { flex: 1; min-width: 110px; }
 
#btn-filter-star {
    flex-shrink: 0; width: 48px; display: flex; justify-content: center; align-items: center;
    border-radius: 12px; border: 1px solid rgba(0,0,0,0.05); background: #FFFFFF; font-size: 1.3rem;
    cursor: pointer; box-shadow: 0 1px 3px rgba(0,0,0,0.02); transition: all 0.2s; color: #D1D1D6;
}
#btn-filter-star:active { transform: scale(0.95); }

/* Grammar Section */
.grammar-section {
    background: var(--surface); border-radius: 16px; margin-bottom: 1.25rem;
    overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.04); border: 0.5px solid rgba(0,0,0,0.02);
}
.grammar-header {
    padding: 1.25rem; font-weight: 700; font-size: 1.05rem; cursor: pointer;
    display: flex; justify-content: space-between; align-items: center; background-color: var(--surface); transition: background-color 0.2s;
}
.grammar-header:active { background-color: #F2F2F7; }
.grammar-content { display: none; padding: 0 1.25rem 1.25rem 1.25rem; font-size: 0.95rem; line-height: 1.6; }
.tag-level {
    font-size: 0.75rem; background: var(--primary); color: white; padding: 0.25rem 0.6rem;
    border-radius: 10px; margin-left: 0.5rem; font-weight: 600; letter-spacing: 0.2px; vertical-align: middle;
}
.sub-item { margin-top: 1.25rem; }
.sub-title { font-weight: 700; color: var(--primary); margin-bottom: 0.5rem; font-size: 1rem; }
.description { margin-bottom: 0.85rem; color: #3C3C43; }
.example-box { background-color: var(--bg); padding: 1rem; border-radius: 12px; font-size: 0.9em; color: #3C3C43; }
.example-box div { margin-bottom: 0.5rem; }
.example-box div:last-child { margin-bottom: 0; }

.btn-go-practice {
    display: block; width: 100%; text-align: center; background-color: var(--action-light); color: var(--action);
    border: none; padding: 0.9rem; border-radius: 12px; font-weight: 600; cursor: pointer; margin-top: 1.5rem; transition: opacity 0.2s; font-size: 0.95rem;
}
.btn-go-practice:active { opacity: 0.7; }

/* Quiz Section */
.quiz-topic-title { font-size: 1.2rem; font-weight: 700; color: var(--primary); margin: 0.5rem 0 1.25rem 0; letter-spacing: 0.2px; }

/* 修改：支援換行與靠右對齊，避免按鈕擠壓 */
.quiz-actions { display: flex; gap: 8px; flex-shrink: 0; flex-wrap: wrap; justify-content: flex-end; }

/* 新增：Ask AI 按鈕專屬樣式 */
.btn-ask-ai {
    background: #F2F2F7; color: var(--action); border: 1px solid rgba(0,122,255,0.2); padding: 6px 12px; border-radius: 16px;
    font-size: 0.75rem; font-weight: 600; cursor: pointer; white-space: nowrap; transition: all 0.2s;
    display: inline-flex; align-items: center; gap: 4px;
}
.btn-ask-ai:active { opacity: 0.7; background: #E5E5EA; }

.btn-icon {
    background: var(--action-light); color: var(--action); border: none; width: 32px; height: 32px;
    border-radius: 50%; display: flex; justify-content: center; align-items: center; cursor: pointer; transition: all 0.2s;
}
.btn-icon:active { background: #D0E3FF; transform: scale(0.95); }
.btn-icon svg { width: 16px; height: 16px; fill: currentColor; }

.btn-translate {
    background: #E5E5EA; color: var(--text); border: none; padding: 6px 12px; border-radius: 16px;
    font-size: 0.75rem; font-weight: 600; cursor: pointer; white-space: nowrap; transition: all 0.2s;
}
.btn-translate:active { opacity: 0.7; }

.quiz-translation { font-size: 0.9rem; color: #3C3C43; margin-bottom: 1rem; background: #F2F2F7; padding: 10px 14px; border-radius: 12px; line-height: 1.5; }
.quiz-option {
    display: block; width: 100%; padding: 1rem; margin: 0.6rem 0; border: 1px solid var(--border); border-radius: 12px;
    background-color: var(--surface); text-align: left; font-size: 0.95rem; cursor: pointer; transition: all 0.2s; color: var(--text); font-weight: 500; box-shadow: 0 1px 2px rgba(0,0,0,0.01);
}
.quiz-option:active { background-color: var(--bg); }
.quiz-option.correct { background-color: var(--success-light); border-color: var(--success); color: #147A34; font-weight: 600; }
.quiz-option.wrong { background-color: var(--error-light); border-color: var(--error); color: #C93400; }
.explanation { margin-top: 1rem; padding: 1rem; background-color: var(--bg); border-radius: 12px; font-size: 0.9rem; display: none; line-height: 1.5; color: #3C3C43; }

/* Dashboard Styles */
.dash-level-card {
    background: var(--surface); border-radius: 16px; padding: 1.5rem; margin-bottom: 1.25rem;
    border: 1px solid var(--border); box-shadow: 0 4px 20px rgba(0,0,0,0.04);
}
.dash-level-title { font-size: 1.15rem; font-weight: 700; margin-bottom: 0.75rem; color: var(--primary); display: flex; justify-content: space-between; align-items: center; }
.progress-bar-bg { background: #E5E5EA; border-radius: 8px; height: 10px; width: 100%; overflow: hidden; margin-bottom: 1rem;}
.progress-bar-fill { background: var(--action); height: 100%; border-radius: 8px; transition: width 0.5s ease; }
.dash-stats { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 0.75rem; }
.dash-badge { display: inline-block; padding: 4px 10px; border-radius: 8px; font-size: 0.75rem; font-weight: 600; }
.badge-ready { background: var(--success-light); color: #147A34; }
.badge-learning { background: var(--action-light); color: var(--action); }

.topic-unlocked { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid rgba(0,0,0,0.04); }
.topic-locked { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px dashed var(--border); filter: grayscale(100%); opacity: 0.6; }
.btn-quick-add {
    background: var(--action-light); color: var(--action); border: none; padding: 6px 12px;
    border-radius: 8px; font-size: 0.75rem; cursor: pointer; font-weight: 700; transition: opacity 0.2s;
}
.btn-quick-add:active { opacity: 0.7; }

/* Bottom Nav */
.bottom-nav {
    position: fixed; bottom: 0; left: 0; right: 0;
    background-color: rgba(255, 255, 255, 0.85);
    backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
    border-top: 0.5px solid rgba(0,0,0,0.1); 
    display: flex; justify-content: space-around; align-items: center; 
    z-index: 100;
    padding-bottom: env(safe-area-inset-bottom);
    height: calc(65px + env(safe-area-inset-bottom));
}
.nav-item { 
    display: flex; flex-direction: column; align-items: center; justify-content: center; 
    color: var(--text-muted); text-decoration: none; font-size: 0.7rem; font-weight: 600; 
    cursor: pointer; width: 25%; height: 65px; transition: color 0.2s; 
}
.nav-item.active { color: var(--action); }
.nav-icon { font-size: 1.4rem; margin-bottom: 4px; line-height: 1; display: block; }

/* Settings Textareas & Buttons */
textarea#json-unified-input, textarea.prompt-box {
    width: 100%; padding: 1rem; border: 1px solid rgba(0,0,0,0.05); border-radius: 12px; margin-bottom: 0.75rem;
    font-family: monospace; font-size: 0.85rem; background-color: #FAFAFA; color: var(--text); box-shadow: inset 0 1px 3px rgba(0,0,0,0.02); resize: vertical;
}
textarea#json-unified-input { height: 120px; }
textarea.prompt-box { height: 250px; transition: all 0.2s; }
textarea.prompt-box:focus { outline: 2px solid var(--action); background-color: #FFFFFF; }

button.btn-primary, button.btn-secondary {
    border: none; padding: 0.85rem 1.2rem; border-radius: 12px; font-weight: 600; cursor: pointer;
    font-size: 0.95rem; transition: opacity 0.2s, transform 0.1s; width: 100%; display: flex; justify-content: center; align-items: center;
}
button.btn-primary { background-color: var(--action); color: white; box-shadow: 0 4px 10px rgba(0, 122, 255, 0.2); }
button.btn-secondary { background-color: #E5E5EA; color: var(--text); margin-top: 0.5rem; }
button.btn-primary:active, button.btn-secondary:active { opacity: 0.7; transform: scale(0.98); }
 
.form-group { margin-bottom: 1.25rem; }

/* Data Manager List & New Group Dropdowns */
.manage-list { list-style: none; margin-top: 0.5rem; padding: 0;}
.manage-item {
    display: flex; flex-direction: column; align-items: stretch; padding: 1rem; border: 1px solid var(--border);
    border-radius: 12px; margin-bottom: 0.75rem; background: var(--surface); gap: 0.5rem;
}
.manage-item-info { width: 100%; }
.manage-item-title { font-weight: 700; font-size: 0.95rem; color: var(--primary); line-height: 1.4; display:flex; align-items:center; flex-wrap:wrap; gap:5px;}
.manage-item-stats { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.3rem; }
 
.manage-actions { display: flex; justify-content: space-between; width: 100%; margin-top: 5px; flex-wrap: wrap; gap: 8px;}
.manage-actions-group { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }

.btn-action-small {
    padding: 6px 12px; border-radius: 8px; font-size: 0.8rem; font-weight: 600; cursor: pointer; border: none; background: #E5E5EA; color: var(--text); transition: opacity 0.2s; display: inline-flex; align-items: center;
}
.btn-action-small:active { opacity: 0.7; }

/* Unified Bulk Action Tools UI */
.bulk-actions-section {
    border-top: 0.5px solid var(--border); padding-top: 1rem; margin-bottom: 0.5rem;
    display: flex; flex-direction: column; gap: 12px;
}
.bulk-select-all {
    font-size: 0.95rem; font-weight: 700; display:flex; align-items:center; gap:8px; cursor:pointer; flex-shrink: 0;
}
.bulk-select-all input[type="checkbox"] { margin: 0; width: 20px; height: 20px; }
 
.bulk-actions-buttons {
    display: flex; flex-wrap: wrap; gap: 8px; width: 100%;
}
.bulk-actions-buttons > * {
    flex: 1 1 0px;
    min-width: 80px;
}

.bulk-action-btn {
    background-color: var(--action-light); color: var(--action); border: none; padding: 0 8px;
    border-radius: 8px; font-size: 0.8rem; font-weight: 600; cursor: pointer; transition: all 0.2s;
    display: inline-flex; align-items: center; justify-content: center; height: 34px; white-space: nowrap; box-sizing: border-box; width: 100%;
}
.bulk-action-btn:active { opacity: 0.7; transform: scale(0.96); }
.bulk-action-btn.btn-warning { background: var(--warning-light); color: #B45309; }
.bulk-action-btn.btn-danger { background: var(--error-light); color: var(--error); }

.bulk-action-select {
    background-color: var(--action-light); color: var(--action); border: none; padding: 0 24px 0 10px;
    border-radius: 8px; font-size: 0.8rem; font-weight: 600; cursor: pointer; transition: all 0.2s;
    height: 34px; -webkit-appearance: none; appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="%23007AFF" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>');
    background-repeat: no-repeat; background-position: right 8px center; box-shadow: none; text-overflow: ellipsis; white-space: nowrap; margin: 0; box-sizing: border-box; width: 100%;
}
.bulk-action-select:focus { box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.3); }

select.inline-group-select {
    padding: 6px 24px 6px 10px; border-radius: 8px; font-size: 0.8rem; font-weight: 600; border: none; background-color: #E5E5EA; color: var(--text);
    cursor: pointer; -webkit-appearance: none; appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="%231D1D1F" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>');
    background-repeat: no-repeat; background-position: right 8px center; max-width: 140px; text-overflow: ellipsis; transition: opacity 0.2s;
}
select.inline-group-select:active { opacity: 0.7; }

.flex-row { display: flex; gap: 0.75rem; }

/* Checkbox Override for Apple Style */
input[type="checkbox"] {
    -webkit-appearance: none; appearance: none; background-color: #fff; margin: 0; font: inherit; color: currentColor;
    width: 1.15em; height: 1.15em; border: 1px solid #C7C7CC; border-radius: 0.25em; display: grid; place-content: center; cursor: pointer;
}
input[type="checkbox"]::before {
    content: ""; width: 0.65em; height: 0.65em; transform: scale(0); transition: 120ms transform ease-in-out;
    box-shadow: inset 1em 1em var(--surface); transform-origin: center;
    clip-path: polygon(14% 44%, 0 65%, 50% 100%, 100% 16%, 80% 0%, 43% 62%);
}
input[type="checkbox"]:checked { background-color: var(--action); border-color: var(--action); }
input[type="checkbox"]:checked::before { transform: scale(1); }

@media (min-width: 481px) {
    .bulk-actions-section { flex-direction: row; justify-content: space-between; align-items: center; }
    .bulk-actions-buttons { width: auto; flex-wrap: nowrap; justify-content: flex-end; }
    .bulk-actions-buttons > * { flex: 0 0 auto; min-width: auto; width: auto; }
}

@media (max-width: 480px) {
    .container { padding: 1rem 0.75rem; }
    .card, .dash-level-card { padding: 1.25rem; }
    .grammar-header { padding: 1rem; font-size: 1rem; }
    .grammar-content { padding: 0 1rem 1rem 1rem; }
    .quiz-option { padding: 0.85rem 1rem; font-size: 0.9rem; }
    .quiz-topic-title { font-size: 1.1rem; }
    .flex-row.stack-mobile { flex-direction: column; gap: 0.5rem; align-items: stretch !important;}
}
/* 修改 Bottom Nav 寬度以適應 5 個 Tab */
.nav-item { width: 20%; }

/* --- 浮動選取文字按鈕 --- */
#floating-save-btn {
    position: fixed; z-index: 9999; background: var(--primary); color: white;
    border: none; padding: 8px 16px; border-radius: 20px; font-weight: 600; font-size: 0.85rem;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15); transform: translateX(-50%);
    cursor: pointer; transition: transform 0.1s;
}
#floating-save-btn:active { transform: translateX(-50%) scale(0.95); }

/* --- 通用彈出視窗 (Modal) --- */
.modal-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0; z-index: 10000;
    background: rgba(0,0,0,0.4); backdrop-filter: blur(5px);
    display: flex; justify-content: center; align-items: center; padding: 1rem;
}
.modal-content {
    background: var(--surface); width: 100%; max-width: 400px; border-radius: 16px;
    padding: 1.5rem; box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.modal-content label { font-size: 0.85rem; font-weight: 700; color: var(--primary); display: block; margin-bottom: 5px; }

/* --- 生字簿列表 & 向左滑動刪除 --- */
.vocab-list { list-style: none; padding: 0; margin: 0; overflow: hidden; }
.vocab-item-wrapper {
    position: relative; margin-bottom: 0.75rem; border-radius: 12px; overflow: hidden;
    background: var(--error); /* 底層顯示紅色刪除背景 */
}
.vocab-delete-bg {
    position: absolute; top: 0; right: 0; bottom: 0; width: 80px;
    display: flex; justify-content: center; align-items: center;
    color: white; font-weight: bold; cursor: pointer; font-size: 0.9rem;
}
.vocab-item-inner {
    position: relative; background: var(--surface); padding: 1rem;
    border: 1px solid var(--border); border-radius: 12px; z-index: 2;
    transition: transform 0.2s ease-out; display: flex; flex-direction: column; gap: 5px;
}
.vocab-word { font-size: 1.1rem; font-weight: 700; color: var(--primary); }
.vocab-pos { font-size: 0.75rem; background: #E5E5EA; padding: 2px 6px; border-radius: 6px; color: #3C3C43; font-weight: 600;}
.vocab-trans { font-size: 0.95rem; color: #3C3C43; }
.vocab-sent { font-size: 0.85rem; color: var(--text-muted); font-style: italic; line-height: 1.4; margin-top: 5px;}

/* --- Flashcard 翻牌特效 --- */
.flashcard-container { perspective: 1000px; width: 100%; height: 350px; cursor: pointer; }
.flashcard {
    width: 100%; height: 100%; position: relative; transition: transform 0.6s;
    transform-style: preserve-3d; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,0.3);
}
.flashcard.flipped { transform: rotateY(180deg); }
.flashcard .front, .flashcard .back {
    width: 100%; height: 100%; position: absolute; backface-visibility: hidden;
    border-radius: 16px; background: white; display: flex; flex-direction: column;
    justify-content: center; align-items: center; padding: 2rem; text-align: center;
}
.flashcard .back { transform: rotateY(180deg); align-items: flex-start; text-align: left; }
