.#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <U8g2_for_Adafruit_GFX.h>

// =====================================================
// TFT PINS
// =====================================================
#define TFT_CS    5
#define TFT_DC    27
#define TFT_RST   33
#define TFT_MOSI  23
#define TFT_MISO  19
#define TFT_SCLK  18

Adafruit_ILI9341 tft(TFT_CS, TFT_DC, TFT_RST);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;

// =====================================================
// BUTTON PINS
// =====================================================
#define BTN_UP      32
#define BTN_DOWN    25
#define BTN_PREV    26
#define BTN_NEXT    13
#define BTN_SUBMIT  14
#define BTN_BACK     4

// =====================================================
// BUTTON STRUCTURE
// =====================================================
struct Button {
  uint8_t pin;
  bool lastReading;
  bool steadyState;
  unsigned long lastDebounceTime;
};

Button btnUp     = {BTN_UP, HIGH, HIGH, 0};
Button btnDown   = {BTN_DOWN, HIGH, HIGH, 0};
Button btnPrev   = {BTN_PREV, HIGH, HIGH, 0};
Button btnNext   = {BTN_NEXT, HIGH, HIGH, 0};
Button btnSubmit = {BTN_SUBMIT, HIGH, HIGH, 0};
Button btnBack   = {BTN_BACK, HIGH, HIGH, 0};

// =====================================================
// VOCAB CONTENT STRUCT
// =====================================================
struct VocabEntry {
  const char* french;
  const char* pronunciation;
  const char* translation;
  const char* example;
  const char* examplePronunciation;
  const char* exampleTranslation;
};

// -----------------------------------------------------
// Vocab Data (Sessions 01 - 04)
// -----------------------------------------------------
VocabEntry session01_vocab[] = {
  { "avoir", "[авуа́р]", "to have", "J'ai quatorze ans.", "[жэ като́рз ан]", "I am fourteen years old." },
  { "un an", "[эн ан]", "a year", "J'ai quatorze ans.", "[жэ като́рз ан]", "I am fourteen years old." },
  { "l'âge", "[лаж]", "age", "Quel âge as-tu ?", "[кэль аж а-тю]", "How old are you?" },
  { "Quel âge as-tu ?", "[кэль аж а-тю]", "How old are you?", "J'ai quatorze ans.", "[жэ като́рз ан]", "I am fourteen years old." },
  { "un anniversaire", "[эн аниверсэ́р]", "a birthday", "C'est mon anniversaire.", "[сэ мон аниверсэ́р]", "It's my birthday." },
  { "mon anniversaire", "[мон аниверсэ́р]", "my birthday", "Mon anniversaire est en juillet.", "[мон аниверсэ́р э ан жюийэ́]", "My birthday is in July." },
  { "une date", "[юн дат]", "a date", "Quelle est la date de ton anniversaire ?", "[кэль э ла дат дё тон аниверсэ́р]", "What is the date of your birthday?" },
  { "une naissance", "[юн нэсáнс]", "a birth", "C'est ma date de naissance.", "[сэ ма дат дё нэсáнс]", "It's my date of birth." },
  { "la date de naissance", "[ла дат дё нэсáнс]", "date of birth", "Quelle est ta date de naissance ?", "[кэль э та дат дё нэсáнс]", "What is your date of birth?" },
  { "Quand est ton anniversaire ?", "[кан э тон аниверсэ́р]", "When is your birthday?", "Mon anniversaire est le 26 juillet.", "[мон аниверсэ́р э лё вэн-сис жюийэ́]", "My birthday is July 26th." },
  { "un", "[эн]", "one", "J'ai un an.", "[жэ эн ан]", "I am one year old." },
  { "deux", "[дё]", "two", "J'ai deux ans.", "[жэ дё зан]", "I am two years old." },
  { "trois", "[труа]", "three", "J'ai trois ans.", "[жэ труа зан]", "I am three years old." },
  { "quatorze", "[като́рз]", "fourteen", "J'ai quatorze ans.", "[жэ като́рз ан]", "I am fourteen years old." },
  { "quinze", "[кэнз]", "fifteen", "J'ai quinze ans.", "[жэ кэнз ан]", "I am fifteen years old." },
  { "vingt", "[вэн]", "twenty", "J'ai vingt ans.", "[жэ вэн ан]", "I am twenty years old." }
};

VocabEntry session02_vocab[] = {
  { "être", "[этр]", "to be", "Je suis français.", "[жэ сюи франсэ́]", "I am French." },
  { "français / française", "[франсэ́ / франсэ́з]", "French", "Elle est française.", "[эль э франсэ́з]", "She is French." },
  { "azerbaïdjanais / azerbaïdjanaise", "[азербайджанэ́ / азербайджанэ́з]", "Azerbaijani", "Je suis azerbaïdjanais.", "[жэ сюи азербайджанэ́]", "I am Azerbaijani." },
  { "américain / américaine", "[амэрикэ́н / амэрикэ́нз]", "American", "Il est américain.", "[иль э амэрикэ́н]", "He is American." },
  { "anglais / anglaise", "[англэ́ / англэ́з]", "English", "Parles-tu anglais ?", "[парль-тю англэ́]", "Do you speak English?" },
  { "espagnol / espagnole", "[эспаньо́ль]", "Spanish", "Il parle espagnol.", "[иль парль эспаньо́ль]", "He speaks Spanish." },
  { "allemand / allemande", "[альма́н / альма́нд]", "German", "Elle est allemande.", "[эль э альма́нд]", "She is German." },
  { "italien / italienne", "[италье́н / италье́нн]", "Italian", "J'aime la cuisine italienne.", "[жэм ла кюизи́н италье́нн]", "I love Italian cuisine." },
  { "russe", "[рюс]", "Russian", "Il est russe.", "[иль э рюс]", "He is Russian." },
  { "une nationalité", "[юн насьоналитэ́]", "a nationality", "Quelle est ta nationalité ?", "[кэль э та насьоналитэ́]", "What is your nationality?" },
  { "un pays", "[эн пэи́]", "a country", "C'est un grand pays.", "[сэ тэн гран пэи́]", "It is a big country." },
  { "Je suis...", "[жэ сюи...]", "I am...", "Je suis étudiant.", "[жэ сюи этюдья́н]", "I am a student." },
  { "Je viens de...", "[жэ вьян дё...]", "I come from...", "Je viens de France.", "[жэ вьян дё франс]", "I come from France." },
  { "Tu es de quel pays ?", "[тю э дё кэль пэи́]", "What country are you from?", "Je viens d'Azerbaïdjan.", "[жэ вьян дазербайджа́н]", "I come from Azerbaijan." },
  { "Tu es de quelle nationalité ?", "[тю э дё кэль насьоналитэ́]", "What nationality are you?", "Je suis azerbaïdjanais.", "[жэ сюи азербайджанэ́]", "I am Azerbaijani." }
};

VocabEntry session03_vocab[] = {
  { "un métier", "[эн мэтье́]", "a profession / job", "Quel est ton métier ?", "[кэль э тон мэтье́]", "What is your profession?" },
  { "un étudiant / une étudiante", "[эн этюдья́н / юн этюдья́нт]", "a student", "Je suis étudiante.", "[жё сюи этюдья́нт]", "I am a student." },
  { "un élève / une élève", "[эн эле́в / юн эле́в]", "a pupil / student", "Il est un bon élève.", "[иль э тэн бон эле́в]", "He is a good student." },
  { "un professeur / une professeure", "[эн профессё́р / юн профессё́р]", "a teacher", "Elle est professeure.", "[эль э профессё́р]", "She is a teacher." },
  { "un ingénieur / une ingénieure", "[эн энжэньё́р / юн энжэньё́р]", "an engineer", "Mon père est ingénieur.", "[мон пэр э энжэньё́р]", "My father is an engineer." },
  { "un médecin / une médecin", "[эн медсэ́н / юн медсэ́н]", "a doctor", "Elle est médecin.", "[эль э медсэ́н]", "She is a doctor." },
  { "un artiste / une artiste", "[эн артист / юн артист]", "an artist", "C'est un grand artiste.", "[сэ тэн гран артист]", "He is a great artist." },
  { "un scientifique / une scientifique", "[эн сьянтифи́к / юн сьянтифи́к]", "a scientist", "Il est scientifique.", "[иль э сьянтифи́к]", "He is a scientist." },
  { "étudier", "[этюдье́]", "to study", "J'étudie les mathématiques.", "[жэтю́ди ле матэма́тик]", "I study mathematics." },
  { "travailler", "[травайе́]", "to work", "Je travaille à l'école.", "[жё трава́й а леко́ль]", "I work at school." },
  { "une école", "[юн эко́ль]", "a school", "J'aime mon école.", "[жэм мон эко́ль]", "I like my school." },
  { "une université", "[юн юниверсите́]", "a university", "Il va à l'université.", "[иль ва а люниверсите́]", "He goes to university." },
  { "une matière", "[юн матье́р]", "a subject", "Ma matière préférée est la physique.", "[ма матье́р прэфэрэ́ э ла физИ́к]", "My favorite subject is physics." },
  { "les études", "[лез этю́д]", "studies / education", "Je parle de mes études.", "[жё парль дё ме зэтю́д]", "I talk about my studies." },
  { "Qu'est-ce que tu fais ?", "[кэс кё тю фэ]", "What do you do?", "Je suis étudiante.", "[жё сюи этюдья́нт]", "I am a student." },
  { "Qu'est-ce que tu étudies ?", "[кэс кё тю этю́ди]", "What do you study?", "J'étudie les sciences.", "[жэтю́ди ле сьянс]", "I study science." }
};

VocabEntry session04_vocab[] = {
  { "un loisir", "[эн луази́р]", "a hobby / leisure activity", "La lecture est mon loisir préféré.", "[ла лектю́р э мон луази́р прэфэрэ́]", "Reading is my favorite hobby." },
  { "un passe-temps", "[эн пас-тан]", "a pastime / hobby", "C'est un bon passe-temps.", "[сэ тэн бон пас-тан]", "It is a good pastime." },
  { "un hobby", "[эн оби́]", "a hobby", "Quel est ton hobby ?", "[кэль э тон оби́]", "What is your hobby?" },
  { "aimer", "[эме́]", "to like / love", "J'aime lire.", "[жэм ли́р]", "I like reading." },
  { "adorer", "[адорэ́]", "to love / really like", "J'adore voyager.", "[жадо́р вуаяжэ́]", "I love travelling." },
  { "lire", "[ли́р]", "to read", "J'aime lire des livres.", "[жэм ли́р дэ ли́вр]", "I like reading books." },
  { "écouter de la musique", "[экутэ́ дё ла мюзи́к]", "to listen to music", "J'aime écouter de la musique.", "[жэм экутэ́ дё ла мюзи́к]", "I like listening to music." },
  { "regarder des films", "[рёгардэ́ дэ фильм]", "to watch movies", "J'aime regarder des films.", "[жэм рёгардэ́ дэ фильм]", "I like watching movies." },
  { "jouer aux jeux vidéo", "[жуэ́ о жё видэо́]", "to play video games", "J'aime jouer aux jeux vidéo.", "[жэм жуэ́ о жё видэо́]", "I like playing video games." },
  { "faire du sport", "[фэр дю спо́р]", "to do sports / exercise", "Je fais du sport.", "[жё фэ дю спо́р]", "I do sports / exercise." },
  { "dessiner", "[дэсинэ́]", "to draw", "J'aime dessiner.", "[жэм дэсинэ́]", "I like drawing." },
  { "danser", "[дансэ́]", "to dance", "Elle aime danser.", "[эль эм dansé]", "She likes dancing." },
  { "chanter", "[шантэ́]", "to sing", "Il aime chanter.", "[иль эм шантэ́]", "He likes singing." },
  { "voyager", "[вуаяжэ́]", "to travel", "J'aime voyager.", "[жэм вуаяжэ́]", "I like travelling." },
  { "J'aime...", "[жэм...]", "I like...", "J'aime la musique.", "[жэм ла мюзи́к]", "I like music." },
  { "Qu'est-ce que tu aimes faire ?", "[кэс кё тю эм фэр]", "What do you like to do?", "J'aime lire et voyager.", "[жэм ли́р э вуаяжэ́]", "I like reading and travelling." }
};

// =====================================================
// PRACTICE CONTENT STRUCT & DATA
// =====================================================
struct Question {
  const char* prompt;
  const char* options[4];
  int correctAnswer; // 0..3
  const char* note;
};

// -----------------------------------------------------
// Practice 01
// -----------------------------------------------------
Question practice01_questions[] = {
  { "What does l'age mean?", { "birthday", "age", "year", "date" }, 1, "l'age = age" },
  { "What does un anniversaire mean?", { "a birthday", "a birth", "a year", "a date" }, 0, "anniversaire = birthday" },
  { "What does une date mean?", { "age", "birthday", "date", "year" }, 2, "une date = a date" },
  { "What does J'ai quinze ans mean?", { "I am 15 years old.", "I have 15 years.", "I was born in 15.", "My birthday is 15." }, 0, "J'ai 15 ans = I am 15." },
  { "Someone asks: Quel age as-tu ?", { "When is your birthday?", "Where are you from?", "How old are you?", "What is your name?" }, 2, "Quel age as-tu ? = How old?" },
  { "Someone asks: Quand est ton anniversaire ?", { "How old are you?", "When is your birthday?", "What is your date of birth?", "Where were you born?" }, 1, "Quand = When" },
  { "Which sentence is correct?", { "Je suis quatorze ans.", "J'ai quatorze ans.", "Je fais quatorze ans.", "Je suis avoir quatorze ans." }, 1, "Use 'avoir' for age!" },
  { "Complete: J'___ vingt ans.", { "suis", "fais", "ai", "es" }, 2, "J'ai = I have/am" },
  { "Complete: C'est mon ___ .", { "age", "anniversaire", "an", "naissance" }, 1, "mon anniversaire" },
  { "What is quatorze?", { "12", "13", "14", "15" }, 2, "quatorze = 14" },
  { "What is vingt?", { "18", "19", "20", "21" }, 2, "vingt = 20" },
  { "Which means 'I am 3 years old'?", { "J'ai trois ans.", "Je suis trois ans.", "J'ai trois anniversaire.", "Je suis trois anniversaire." }, 0, "J'ai trois ans." },
  { "Marie a dix-sept ans. How old is she?", { "15", "16", "17", "18" }, 2, "dix-sept = 17" },
  { "Mon anniversaire est en mars.", { "In March", "In May", "In July", "In December" }, 0, "mars = March" },
  { "Quel age as-tu ? Best answer:", { "J'ai seize ans.", "Je suis francais.", "J'aime lire.", "Je m'appelle Aylin." }, 0, "J'ai seize ans = I am 16." },
  { "C'est ma date de naissance.", { "Birthday party", "Their age", "Date of birth", "Nationality" }, 2, "date de naissance = birth date" }
};

// -----------------------------------------------------
// Practice 02
// -----------------------------------------------------
Question practice02_questions[] = {
  { "What does etre mean?", { "to have", "to be", "to come", "to study" }, 1, "etre = to be" },
  { "What does une nationalite mean?", { "a country", "a language", "a nationality", "an origin" }, 2, "nationalite = nationality" },
  { "What does un pays mean?", { "a person", "a country", "a nationality", "a city" }, 1, "un pays = a country" },
  { "Je suis azerbaidjanaise. means:", { "I come from Azerbaijan.", "I live in Azerbaijan.", "I am Azerbaijani.", "I speak Azerbaijani." }, 2, "Je suis = I am" },
  { "Je viens d'Azerbaidjan. means:", { "I am Azerbaijani.", "I'm from Azerbaijan.", "I speak Azerbaijani.", "I live in Azerbaijan." }, 1, "Je viens de = I come from" },
  { "Which sentence expresses nationality?", { "Je viens de France.", "Je suis francais.", "J'habite en France.", "Je vais en France." }, 1, "Je suis + nationality" },
  { "Which sentence expresses origin?", { "Je suis americain.", "Je parle anglais.", "Je viens des Etats-Unis.", "J'aime les Etats-Unis." }, 2, "Je viens de = Origin" },
  { "Tu es de quel pays ? asks:", { "What is your nationality?", "What country are you from?", "What language do you speak?", "Where do you live?" }, 1, "quel pays = what country" },
  { "Tu es de quelle nationalite ? asks:", { "What country are you from?", "What is your nationality?", "What do you study?", "Where do you live?" }, 1, "nationalite = nationality" },
  { "Complete: Je ___ francais.", { "ai", "suis", "viens", "fais" }, 1, "Je suis francais" },
  { "Complete: Je ___ de France.", { "suis", "ai", "viens", "fais" }, 2, "Je viens de France" },
  { "What does francais / francaise mean?", { "French", "Spanish", "English", "Italian" }, 0, "francais = French" },
  { "What does anglais / anglaise mean?", { "German", "American", "English", "French" }, 2, "anglais = English" },
  { "What does espagnol / espagnole mean?", { "Spanish", "Italian", "Russian", "German" }, 0, "espagnol = Spanish" },
  { "Elle est allemande. means:", { "She is Italian.", "She is German.", "She is English.", "She is Spanish." }, 1, "allemande = German" },
  { "Il est russe. means:", { "He is Russian.", "He is French.", "He is American.", "He is Azerbaijani." }, 0, "russe = Russian" },
  { "Best answer to: Tu es de quel pays ?", { "Je suis italien.", "Je viens d'Italie.", "Je suis une nationalite.", "J'aime l'Italie." }, 1, "Je viens d'Italie" },
  { "Best answer to: Tu es de quelle nationalite ?", { "Je viens d'Italie.", "Je suis italien.", "Je vais en Italie.", "J'habite en Italie." }, 1, "Je suis italien" },
  { "Je suis americain tells us about:", { "birthday", "profession", "nationality", "hobby" }, 2, "Nationality" },
  { "Je viens de France tells us about:", { "origin", "age", "profession", "hobby" }, 0, "Origin" },
  { "Which sentence is correct?", { "Je suis de France.", "Je viens de France.", "Je ai de France.", "Je fais de France." }, 1, "Je viens de France" },
  { "Which sentence is correct?", { "Je suis Azerbaidjan.", "Je viens Azerbaidjan.", "Je viens d'Azerbaidjan.", "Je suis d'Azerbaidjan." }, 2, "Je viens d'Azerbaidjan" },
  { "Je suis russe vs Je viens de Russie:", { "exactly the same", "nationality vs origin", "age vs birthday", "profession vs studies" }, 1, "Nationality vs Origin" },
  { "Translation: Quelle est ta nationalite ?", { "Where are you from?", "What country do you like?", "What is your nationality?", "What language do you speak?" }, 2, "What is your nationality?" },
  { "Translation: Je viens de France.", { "I am French.", "I'm from France.", "I speak French.", "I live in France." }, 1, "I'm from France" }
};

// -----------------------------------------------------
// Practice 03
// -----------------------------------------------------
Question practice03_questions[] = {
  { "What does un metier mean?", { "a subject", "a profession / job", "a school", "a hobby" }, 1, "metier = profession / job" },
  { "What does un etudiant / une etudiante mean?", { "a teacher", "an engineer", "a student", "a scientist" }, 2, "etudiant = student" },
  { "What does un professeur mean?", { "a teacher", "a doctor", "an artist", "an engineer" }, 0, "professeur = teacher" },
  { "What does un ingenieur mean?", { "a scientist", "an engineer", "a teacher", "a student" }, 1, "ingenieur = engineer" },
  { "What does un medecin mean?", { "a doctor", "a scientist", "an artist", "a teacher" }, 0, "medecin = doctor" },
  { "What does un scientifique mean?", { "an engineer", "a scientist", "a student", "an artist" }, 1, "scientifique = scientist" },
  { "Je suis etudiante. means:", { "I study at university.", "I am a student.", "I am a teacher.", "I work at school." }, 1, "Je suis etudiante = I am a student" },
  { "Elle est professeure. means:", { "She is a student.", "She is a scientist.", "She is a teacher.", "She is an engineer." }, 2, "professeure = teacher" },
  { "Mon pere est ingenieur. means:", { "My father studies engineering.", "My father is an engineer.", "My father teaches engineering.", "My father is a scientist." }, 1, "ingenieur = engineer" },
  { "What does etudier mean?", { "to work", "to study", "to teach", "to go" }, 1, "etudier = to study" },
  { "What does travailler mean?", { "to study", "to work", "to learn", "to teach" }, 1, "travailler = to work" },
  { "What does une ecole mean?", { "a university", "a subject", "a school", "a profession" }, 2, "ecole = school" },
  { "What does une universite mean?", { "a university", "a school", "a subject", "a profession" }, 0, "universite = university" },
  { "What does une matiere mean?", { "a profession", "a subject", "a university", "a study" }, 1, "matiere = school subject" },
  { "Ma matiere preferee est la physique.", { "My favorite profession is physics.", "I study physics at university.", "My favorite subject is physics.", "I teach physics." }, 2, "matiere = subject" },
  { "What does les etudes mean?", { "studies / education", "schools", "subjects", "professions" }, 0, "les etudes = studies" },
  { "Quel est ton metier ? asks:", { "What do you study?", "What is your profession?", "What is your favorite subject?", "Where do you work?" }, 1, "metier = profession" },
  { "Qu'est-ce que tu fais ? Best answer:", { "Je suis etudiante.", "Je viens d'Azerbaidjan.", "J'ai quinze ans.", "J'aime dessiner." }, 0, "Je suis etudiante" },
  { "Qu'est-ce que tu etudies ? Best answer:", { "Je suis ingenieure.", "Je viens de France.", "J'etudie les sciences.", "J'ai quinze ans." }, 2, "J'etudie les sciences" },
  { "Complete: J'___ les mathematiques.", { "suis", "etudie", "fais", "ai" }, 1, "J'etudie" },
  { "Complete: Je ___ a l'ecole.", { "travaille", "suis", "ai", "viens" }, 0, "Je travaille" },
  { "Which sentence means 'I study science'?", { "Je suis scientifique.", "J'etudie les sciences.", "Je travaille les sciences.", "Je fais scientifique." }, 1, "J'etudie les sciences" },
  { "Which sentence means 'She is a doctor'?", { "Elle etudie medecin.", "Elle est medecin.", "Elle fais medecin.", "Elle a medecin." }, 1, "Elle est medecin" },
  { "Which sentence is correct?", { "Je suis etudiante.", "Je suis etudier.", "J'ai etudiante.", "Je fais etudiante." }, 0, "Je suis etudiante" },
  { "Answer to: Qu'est-ce que tu etudies ?", { "Je suis scientifique.", "Je travaille a l'ecole.", "J'etudie les mathematiques.", "Je viens d'Azerbaidjan." }, 2, "J'etudie les mathematiques" },
  { "Je suis scientifique vs J'etudie les sciences:", { "exactly the same", "profession vs field of study", "nationality vs origin", "age vs birthday" }, 1, "Profession vs Study" },
  { "Which one is a profession?", { "une matiere", "une ecole", "un ingenieur", "les etudes" }, 2, "un ingenieur" },
  { "Which one is a place of education?", { "un medecin", "une universite", "un metier", "un scientifique" }, 1, "une universite" },
  { "Which one refers to a school subject?", { "une matiere", "un metier", "une ecole", "un etudiant" }, 0, "une matiere" },
  { "Je parle de mes etudes. means:", { "I talk about my job.", "I talk about my school.", "I talk about my studies.", "I talk about my profession." }, 2, "I talk about my studies" }
};

// -----------------------------------------------------
// Practice 04
// -----------------------------------------------------
Question practice04_questions[] = {
  { "What does un loisir mean?", { "A profession", "A hobby / leisure activity", "A subject", "A country" }, 1, "loisir = hobby" },
  { "What does un passe-temps mean?", { "A pastime / hobby", "A school", "A sport", "A book" }, 0, "passe-temps = pastime/hobby" },
  { "What does aimer mean?", { "To study", "To travel", "To like / love", "To work" }, 2, "aimer = to like / love" },
  { "What does adorer mean?", { "To dislike", "To really like / love", "To watch", "To draw" }, 1, "adorer = to love" },
  { "What does lire mean?", { "To write", "To read", "To sing", "To dance" }, 1, "lire = to read" },
  { "What does voyager mean?", { "To travel", "To listen", "To draw", "To play" }, 0, "voyager = to travel" },
  { "What does dessiner mean?", { "To dance", "To draw", "To sing", "To read" }, 1, "dessiner = to draw" },
  { "What does faire du sport mean?", { "To watch sports", "To play a video game", "To do sports / exercise", "To study sports" }, 2, "faire du sport = exercise" },
  { "What does ecouter de la musique mean?", { "To make music", "To listen to music", "To dance to music", "To sing music" }, 1, "ecouter = to listen" },
  { "What does regarder des films mean?", { "To make movies", "To read movies", "To watch movies", "To like movies" }, 2, "regarder = to watch" },
  { "What does jouer aux jeux video mean?", { "To create video games", "To watch video games", "To play video games", "To study video games" }, 2, "jouer = to play" },
  { "What does J'aime lire. mean?", { "I love books.", "I like reading.", "I am reading.", "I want to read." }, 1, "J'aime lire = I like reading" },
  { "What does J'adore voyager. mean?", { "I like travelling.", "I hate travelling.", "I love travelling.", "I travel often." }, 2, "J'adore = I love" },
  { "Complete: J'aime ___ des livres.", { "lire", "lis", "lu", "lit" }, 0, "Infinitive verb: lire" },
  { "Complete: J'aime ___ de la musique.", { "ecouter", "ecoute", "ecoute", "ecoutes" }, 0, "Infinitive verb: ecouter" },
  { "Complete: J'aime ___ des films.", { "regarder", "regarde", "regarde", "regardes" }, 0, "Infinitive verb: regarder" },
  { "Qu'est-ce que tu aimes faire ? asks:", { "What do you study?", "What do you like to do?", "What is your profession?", "Where are you from?" }, 1, "What do you like to do?" },
  { "Answer to: Qu'est-ce que tu aimes faire ?", { "Je suis etudiante.", "Je viens d'Azerbaidjan.", "J'aime lire et voyager.", "J'ai quinze ans." }, 2, "J'aime lire et voyager" },
  { "Which sentence means 'She likes dancing'?", { "Elle aime danser.", "Elle adore dessiner.", "Elle aime chanter.", "Elle fait du sport." }, 0, "Elle aime danser" },
  { "Which sentence means 'He likes singing'?", { "Il aime lire.", "Il aime chanter.", "Il adore voyager.", "Il regarde des films." }, 1, "Il aime chanter" },
  { "Which sentence means 'I like drawing'?", { "J'aime danser.", "J'aime dessiner.", "J'aime chanter.", "J'aime voyager." }, 1, "J'aime dessiner" },
  { "Which sentence means 'I do sports'?", { "Je joue aux jeux video.", "Je regarde des films.", "Je fais du sport.", "J'ecoute de la musique." }, 2, "Je fais du sport" },
  { "Which sentence means 'I like video games'?", { "J'aime jouer aux jeux video.", "J'aime regarder des films.", "J'aime ecouter de la musique.", "J'aime faire du sport." }, 0, "J'aime jouer aux jeux video" },
  { "Reading is my favorite hobby:", { "La lecture est mon loisir prefere.", "J'aime lire des livres.", "J'adore les loisirs.", "La musique est mon loisir prefere." }, 0, "La lecture est mon loisir prefere." },
  { "Best answer to: Quel est ton hobby ?", { "J'ai quinze ans.", "Je suis azerbaidjanaise.", "La lecture est mon loisir prefere.", "Je viens d'Azerbaidjan." }, 2, "La lecture est mon loisir prefere." }
};

// =====================================================
// GENERIC PRACTICE SESSION STRUCTURE
// =====================================================
struct PracticeSession {
  Question* questions;
  int questionCount;
};

PracticeSession practiceSessions[] = {
  { practice01_questions, sizeof(practice01_questions) / sizeof(practice01_questions[0]) },
  { practice02_questions, sizeof(practice02_questions) / sizeof(practice02_questions[0]) },
  { practice03_questions, sizeof(practice03_questions) / sizeof(practice03_questions[0]) },
  { practice04_questions, sizeof(practice04_questions) / sizeof(practice04_questions[0]) }
};

#define MAX_QUESTIONS_PER_SESSION 30
bool reviewNeeded[4][MAX_QUESTIONS_PER_SESSION] = { false };

// Dynamic Review Tracking Item
struct ReviewItem {
  int sessionIndex;
  int questionIndex;
};

#define MAX_TOTAL_REVIEW_ITEMS 120
ReviewItem activeReviewList[MAX_TOTAL_REVIEW_ITEMS];

// =====================================================
// VOCAB SESSIONS REGISTRY
// =====================================================
struct Session {
  const char* title;
  VocabEntry* vocabList;
  int vocabCount;
};

Session sessions[] = {
  { "01 Age & Birthday", session01_vocab, sizeof(session01_vocab) / sizeof(session01_vocab[0]) },
  { "02 Nationality & Origin", session02_vocab, sizeof(session02_vocab) / sizeof(session02_vocab[0]) },
  { "03 Profession & Studies", session03_vocab, sizeof(session03_vocab) / sizeof(session03_vocab[0]) },
  { "04 Hobbies & Interests", session04_vocab, sizeof(session04_vocab) / sizeof(session04_vocab[0]) }
};

const int NUM_SESSIONS = sizeof(sessions) / sizeof(sessions[0]);

// =====================================================
// UI STATE
// =====================================================
enum State {
  STATE_MENU,
  STATE_SESSION_LIST,
  STATE_PRACTICE_LIST,
  STATE_SUB_SCREEN,
  STATE_VOCAB,
  STATE_VOCAB_COMPLETED,
  STATE_PRACTICE,
  STATE_PRACTICE_FEEDBACK,
  STATE_PRACTICE_COMPLETED,
  STATE_REVIEW,
  STATE_REVIEW_FEEDBACK,
  STATE_ABOUT
};

State currentState = STATE_MENU;

const char* menuItems[] = {
  "VOCAB",
  "PRACTICE",
  "REVIEW",
  "ABOUT"
};

const int NUM_MENU_ITEMS = 4;
int selectedIndex = 0;

// Selection tracking
int selectedSessionIndex = 0;
int currentSessionIndex = 0;

// Vocab state
int vocabIndex = 0;
int vocabSection = 0;

// Practice state
int questionIndex = 0;
int selectedOption = 0;
int practiceScore = 0;
bool lastAnswerCorrect = false;

// Review state
int reviewTotalCount = 0;
int currentReviewIndex = 0;

// =====================================================
// FUNCTION DECLARATIONS
// =====================================================
bool wasButtonPressed(Button &btn);
bool hasCyrillic(const char* text);

void drawMenu();
void drawSessionList();
void drawPracticeList();
void drawSubScreen();
void drawVocab();
void drawVocabCompleted();
void drawPracticeQuestion();
void drawPracticeFeedback();
void drawPracticeCompleted();
void drawReview();
void drawReviewFeedback();
void drawAbout();

void drawText(const char* text, int x, int y, int fontSize = 14);
void drawCentered(const char* text, int y, int fontSize = 14);

// =====================================================
// SETUP
// =====================================================
void setup() {
  Serial.begin(115200);

  pinMode(BTN_UP, INPUT_PULLUP);
  pinMode(BTN_DOWN, INPUT_PULLUP);
  pinMode(BTN_PREV, INPUT_PULLUP);
  pinMode(BTN_NEXT, INPUT_PULLUP);
  pinMode(BTN_SUBMIT, INPUT_PULLUP);
  pinMode(BTN_BACK, INPUT_PULLUP);

  tft.begin();
  tft.setRotation(1);

  u8g2_for_adafruit_gfx.begin(tft);

  drawMenu();
}

// =====================================================
// LOOP
// =====================================================
void loop() {

  // ---------------------------------------------------
  // VOCAB SCREEN
  // ---------------------------------------------------
  if (currentState == STATE_VOCAB) {
    if (wasButtonPressed(btnBack)) {
      currentState = STATE_SESSION_LIST;
      drawSessionList();
      return;
    }
    if (wasButtonPressed(btnPrev)) {
      vocabIndex--;
      if (vocabIndex < 0) vocabIndex = sessions[currentSessionIndex].vocabCount - 1;
      vocabSection = 0;
      drawVocab();
    }
    if (wasButtonPressed(btnNext)) {
      vocabIndex++;
      if (vocabIndex >= sessions[currentSessionIndex].vocabCount) {
        currentState = STATE_VOCAB_COMPLETED;
        drawVocabCompleted();
        return;
      }
      vocabSection = 0;
      drawVocab();
    }
    if (wasButtonPressed(btnDown)) {
      vocabSection++;
      if (vocabSection > 2) vocabSection = 2;
      drawVocab();
    }
    if (wasButtonPressed(btnUp)) {
      vocabSection--;
      if (vocabSection < 0) vocabSection = 0;
      drawVocab();
    }
    return;
  }

  // ---------------------------------------------------
  // VOCAB COMPLETED
  // ---------------------------------------------------
  if (currentState == STATE_VOCAB_COMPLETED) {
    if (wasButtonPressed(btnBack) || wasButtonPressed(btnSubmit) || wasButtonPressed(btnNext)) {
      currentState = STATE_SESSION_LIST;
      drawSessionList();
      return;
    }
    return;
  }

  // ---------------------------------------------------
  // PRACTICE QUESTION SCREEN
  // ---------------------------------------------------
  if (currentState == STATE_PRACTICE) {
    if (wasButtonPressed(btnBack)) {
      currentState = STATE_PRACTICE_LIST;
      drawPracticeList();
      return;
    }
    if (wasButtonPressed(btnUp)) {
      selectedOption--;
      if (selectedOption < 0) selectedOption = 3;
      drawPracticeQuestion();
    }
    if (wasButtonPressed(btnDown)) {
      selectedOption++;
      if (selectedOption > 3) selectedOption = 0;
      drawPracticeQuestion();
    }
    if (wasButtonPressed(btnSubmit)) {
      PracticeSession &ps = practiceSessions[currentSessionIndex];
      Question &q = ps.questions[questionIndex];
      if (selectedOption == q.correctAnswer) {
        lastAnswerCorrect = true;
        practiceScore++;
      } else {
        lastAnswerCorrect = false;
        reviewNeeded[currentSessionIndex][questionIndex] = true; // Add mistake to global matrix
      }
      currentState = STATE_PRACTICE_FEEDBACK;
      drawPracticeFeedback();
    }
    return;
  }

  // ---------------------------------------------------
  // PRACTICE FEEDBACK
  // ---------------------------------------------------
  if (currentState == STATE_PRACTICE_FEEDBACK) {
    if (wasButtonPressed(btnSubmit) || wasButtonPressed(btnNext)) {
      PracticeSession &ps = practiceSessions[currentSessionIndex];
      questionIndex++;
      if (questionIndex >= ps.questionCount) {
        currentState = STATE_PRACTICE_COMPLETED;
        drawPracticeCompleted();
      } else {
        selectedOption = 0;
        currentState = STATE_PRACTICE;
        drawPracticeQuestion();
      }
    }
    return;
  }

  // ---------------------------------------------------
  // PRACTICE COMPLETED
  // ---------------------------------------------------
  if (currentState == STATE_PRACTICE_COMPLETED) {
    if (wasButtonPressed(btnBack) || wasButtonPressed(btnSubmit) || wasButtonPressed(btnNext)) {
      currentState = STATE_PRACTICE_LIST;
      drawPracticeList();
    }
    return;
  }

  // ---------------------------------------------------
  // REVIEW QUESTION SCREEN
  // ---------------------------------------------------
  if (currentState == STATE_REVIEW) {
    if (wasButtonPressed(btnBack)) {
      currentState = STATE_MENU;
      drawMenu();
      return;
    }
    if (reviewTotalCount == 0) return;

    if (wasButtonPressed(btnUp)) {
      selectedOption--;
      if (selectedOption < 0) selectedOption = 3;
      drawReview();
    }
    if (wasButtonPressed(btnDown)) {
      selectedOption++;
      if (selectedOption > 3) selectedOption = 0;
      drawReview();
    }
    if (wasButtonPressed(btnSubmit)) {
      ReviewItem &item = activeReviewList[currentReviewIndex];
      Question &q = practiceSessions[item.sessionIndex].questions[item.questionIndex];

      if (selectedOption == q.correctAnswer) {
        lastAnswerCorrect = true;
        reviewNeeded[item.sessionIndex][item.questionIndex] = false; // Resolved mistake!
      } else {
        lastAnswerCorrect = false;
      }
      currentState = STATE_REVIEW_FEEDBACK;
      drawReviewFeedback();
    }
    return;
  }

  // ---------------------------------------------------
  // REVIEW FEEDBACK
  // ---------------------------------------------------
  if (currentState == STATE_REVIEW_FEEDBACK) {
    if (wasButtonPressed(btnSubmit) || wasButtonPressed(btnNext)) {
      currentReviewIndex++;
      if (currentReviewIndex >= reviewTotalCount) {
        currentState = STATE_REVIEW;
        drawReview();
      } else {
        selectedOption = 0;
        currentState = STATE_REVIEW;
        drawReview();
      }
    }
    return;
  }

  // ---------------------------------------------------
  // VOCAB SESSION LIST
  // ---------------------------------------------------
  if (currentState == STATE_SESSION_LIST) {
    if (wasButtonPressed(btnBack)) {
      currentState = STATE_MENU;
      drawMenu();
      return;
    }
    if (wasButtonPressed(btnUp)) {
      selectedSessionIndex--;
      if (selectedSessionIndex < 0) selectedSessionIndex = NUM_SESSIONS - 1;
      drawSessionList();
    }
    if (wasButtonPressed(btnDown)) {
      selectedSessionIndex++;
      if (selectedSessionIndex >= NUM_SESSIONS) selectedSessionIndex = 0;
      drawSessionList();
    }
    if (wasButtonPressed(btnSubmit)) {
      currentSessionIndex = selectedSessionIndex;
      vocabIndex = 0;
      vocabSection = 0;
      currentState = STATE_VOCAB;
      drawVocab();
    }
    return;
  }

  // ---------------------------------------------------
  // PRACTICE SESSION LIST
  // ---------------------------------------------------
  if (currentState == STATE_PRACTICE_LIST) {
    if (wasButtonPressed(btnBack)) {
      currentState = STATE_MENU;
      drawMenu();
      return;
    }
    if (wasButtonPressed(btnUp)) {
      selectedSessionIndex--;
      if (selectedSessionIndex < 0) selectedSessionIndex = NUM_SESSIONS - 1;
      drawPracticeList();
    }
    if (wasButtonPressed(btnDown)) {
      selectedSessionIndex++;
      if (selectedSessionIndex >= NUM_SESSIONS) selectedSessionIndex = 0;
      drawPracticeList();
    }
    if (wasButtonPressed(btnSubmit)) {
      currentSessionIndex = selectedSessionIndex;
      questionIndex = 0;
      selectedOption = 0;
      practiceScore = 0;
      currentState = STATE_PRACTICE;
      drawPracticeQuestion();
    }
    return;
  }

  // ---------------------------------------------------
  // SUB-SCREEN / ABOUT
  // ---------------------------------------------------
  if (currentState == STATE_SUB_SCREEN || currentState == STATE_ABOUT) {
    if (wasButtonPressed(btnBack)) {
      currentState = STATE_MENU;
      drawMenu();
    }
    return;
  }

  // ---------------------------------------------------
  // MAIN MENU
  // ---------------------------------------------------
  if (currentState == STATE_MENU) {
    if (wasButtonPressed(btnUp)) {
      selectedIndex--;
      if (selectedIndex < 0) selectedIndex = NUM_MENU_ITEMS - 1;
      drawMenu();
    }
    if (wasButtonPressed(btnDown)) {
      selectedIndex++;
      if (selectedIndex >= NUM_MENU_ITEMS) selectedIndex = 0;
      drawMenu();
    }
    if (wasButtonPressed(btnSubmit)) {
      if (selectedIndex == 0) { // VOCAB
        currentState = STATE_SESSION_LIST;
        selectedSessionIndex = 0;
        drawSessionList();
      } else if (selectedIndex == 1) { // PRACTICE
        currentState = STATE_PRACTICE_LIST;
        selectedSessionIndex = 0;
        drawPracticeList();
      } else if (selectedIndex == 2) { // REVIEW
        currentReviewIndex = 0;
        selectedOption = 0;
        currentState = STATE_REVIEW;
        drawReview();
      } else if (selectedIndex == 3) { // ABOUT
        currentState = STATE_ABOUT;
        drawAbout();
      }
    }
  }
}

// =====================================================
// BUTTON DEBOUNCER
// =====================================================
bool wasButtonPressed(Button &btn) {
  bool currentReading = digitalRead(btn.pin);
  bool pressDetected = false;

  if (currentReading != btn.lastReading) {
    btn.lastDebounceTime = millis();
  }

  if ((millis() - btn.lastDebounceTime) > 30) {
    if (currentReading != btn.steadyState) {
      btn.steadyState = currentReading;
      if (btn.steadyState == LOW) {
        pressDetected = true;
      }
    }
  }
  btn.lastReading = currentReading;
  return pressDetected;
}

// =====================================================
// TEXT HELPERS
// =====================================================
bool hasCyrillic(const char* text) {
  if (!text) return false;
  while (*text) {
    uint8_t c = (uint8_t)*text;
    if (c == 0xD0 || c == 0xD1 || c == 0xCC) return true;
    text++;
  }
  return false;
}

void drawText(const char* text, int x, int y, int fontSize) {
  if (hasCyrillic(text)) {
    u8g2_for_adafruit_gfx.setFont(u8g2_font_unifont_t_cyrillic);
  } else {
    u8g2_for_adafruit_gfx.setFont(u8g2_font_unifont_t_extended);
  }

  u8g2_for_adafruit_gfx.setFontMode(1);
  u8g2_for_adafruit_gfx.setForegroundColor(ILI9341_BLACK);
  u8g2_for_adafruit_gfx.setFontDirection(0);
  u8g2_for_adafruit_gfx.drawUTF8(x, y, text);
}

void drawCentered(const char* text, int y, int fontSize) {
  if (hasCyrillic(text)) {
    u8g2_for_adafruit_gfx.setFont(u8g2_font_unifont_t_cyrillic);
  } else {
    u8g2_for_adafruit_gfx.setFont(u8g2_font_unifont_t_extended);
  }

  int width = u8g2_for_adafruit_gfx.getUTF8Width(text);
  int x = (320 - width) / 2;
  u8g2_for_adafruit_gfx.setForegroundColor(ILI9341_BLACK);
  u8g2_for_adafruit_gfx.drawUTF8(x, y, text);
}


// --- ADD THIS HELPER FUNCTION BEFORE DRAW MENU ---
void drawFrenchFlag(int x, int y, int width, int height) {
  int stripeWidth = width / 3;

  // Blue stripe
  tft.fillRect(x, y, stripeWidth, height, ILI9341_BLUE);
  // White stripe
  tft.fillRect(x + stripeWidth, y, stripeWidth, height, ILI9341_WHITE);
  // Red stripe
  tft.fillRect(x + (stripeWidth * 2), y, stripeWidth, height, ILI9341_RED);

  // Subtle light-gray border (so white stripe stays visible)
  uint16_t lightGray = tft.color565(200, 200, 200);
  tft.drawRect(x - 1, y - 1, width + 2, height + 2, lightGray);
}

// --- REPLACE YOUR EXISTING drawMenu() WITH THIS ---
void drawMenu() {
  tft.fillScreen(ILI9341_WHITE);
  
  // Title + Flag header line
  drawText("POCKET FRENCH", 70, 35);
  drawFrenchFlag(215, 23, 21, 14); 

  tft.drawLine(25, 50, 295, 50, ILI9341_BLACK);

  int startY = 85;
  int spacing = 32;

  for (int i = 0; i < NUM_MENU_ITEMS; i++) {
    char line[30];
    if (i == selectedIndex) {
      snprintf(line, sizeof(line), "> %s", menuItems[i]);
    } else {
      snprintf(line, sizeof(line), "  %s", menuItems[i]);
    }
    drawText(line, 55, startY + i * spacing);
  }
}

// =====================================================
// DRAWING UI SCREENS
// =====================================================


void drawSessionList() {
  tft.fillScreen(ILI9341_WHITE);
  drawText("VOCAB", 15, 25);
  tft.drawLine(15, 35, 305, 35, ILI9341_BLACK);

  int startY = 65;
  int spacing = 28;

  for (int i = 0; i < NUM_SESSIONS; i++) {
    char line[40];
    if (i == selectedSessionIndex) {
      snprintf(line, sizeof(line), "> %s", sessions[i].title);
    } else {
      snprintf(line, sizeof(line), "  %s", sessions[i].title);
    }
    drawText(line, 20, startY + i * spacing);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("< Home", 15, 230);
}

void drawPracticeList() {
  tft.fillScreen(ILI9341_WHITE);
  drawText("PRACTICE", 15, 25);
  tft.drawLine(15, 35, 305, 35, ILI9341_BLACK);

  int startY = 65;
  int spacing = 28;

  for (int i = 0; i < NUM_SESSIONS; i++) {
    char line[40];
    if (i == selectedSessionIndex) {
      snprintf(line, sizeof(line), "> %s", sessions[i].title);
    } else {
      snprintf(line, sizeof(line), "  %s", sessions[i].title);
    }
    drawText(line, 20, startY + i * spacing);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("< Home", 15, 230);
}

void drawSubScreen() {
  tft.fillScreen(ILI9341_WHITE);
  drawCentered("PRACTICE", 40);
  drawCentered("SOON...", 120);
  drawText("< Back", 15, 225);
}

void drawAbout() {
  tft.fillScreen(ILI9341_WHITE);
  
  drawCentered("ABOUT", 35);
  tft.drawLine(25, 48, 295, 48, ILI9341_BLACK);
  
  drawCentered("Pocket French", 85);
  drawCentered("A device for learning", 115);
  drawCentered("French anywhere", 135);
  
  drawCentered("Made by Aylin Muzaffarli", 165);
  drawCentered("August 2026", 185);
  
  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("< Back", 15, 230);
}

void drawVocab() {
  tft.fillScreen(ILI9341_WHITE);
  Session &activeSession = sessions[currentSessionIndex];
  VocabEntry &v = activeSession.vocabList[vocabIndex];

  drawText("VOCAB", 15, 25);
  char counter[20];
  snprintf(counter, sizeof(counter), "%d/%d", vocabIndex + 1, activeSession.vocabCount);
  int counterWidth = u8g2_for_adafruit_gfx.getUTF8Width(counter);
  drawText(counter, 305 - counterWidth, 25);
  tft.drawLine(15, 35, 305, 35, ILI9341_BLACK);

  if (vocabSection == 0) {
    drawText(v.french, 20, 70);
    drawText(v.pronunciation, 20, 105);
    drawText(v.translation, 20, 145);
    drawText("v more", 245, 220);
  } else if (vocabSection == 1) {
    drawText("EXAMPLE", 20, 65);
    drawText(v.example, 20, 100);
    drawText(v.examplePronunciation, 20, 135);
    drawText("v translation", 185, 220);
  } else if (vocabSection == 2) {
    drawText("TRANSLATION", 20, 65);
    drawText(v.exampleTranslation, 20, 105);
    drawText("^ back", 20, 220);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("< prev", 15, 235);
  drawText("next >", 245, 235);
}

void drawVocabCompleted() {
  tft.fillScreen(ILI9341_WHITE);
  int totalWords = sessions[currentSessionIndex].vocabCount;
  drawCentered("SESSION COMPLETE", 60);
  tft.drawLine(40, 80, 280, 80, ILI9341_BLACK);
  char summary[30];
  snprintf(summary, sizeof(summary), "%d / %d words", totalWords, totalWords);
  drawCentered(summary, 115);
  drawCentered("Nice work! Tres bien !", 150);
  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("< Sessions", 15, 230);
}

void drawPracticeQuestion() {
  tft.fillScreen(ILI9341_WHITE);
  PracticeSession &ps = practiceSessions[currentSessionIndex];
  Question &q = ps.questions[questionIndex];

  char title[30];
  snprintf(title, sizeof(title), "PRACTICE 0%d", currentSessionIndex + 1);
  drawText(title, 15, 25);

  char counter[20];
  snprintf(counter, sizeof(counter), "%d/%d", questionIndex + 1, ps.questionCount);
  int width = u8g2_for_adafruit_gfx.getUTF8Width(counter);
  drawText(counter, 305 - width, 25);
  tft.drawLine(15, 35, 305, 35, ILI9341_BLACK);

  drawText(q.prompt, 15, 60);

  int startY = 95;
  int spacing = 26;
  const char* labels[] = { "A", "B", "C", "D" };

  for (int i = 0; i < 4; i++) {
    char line[40];
    if (i == selectedOption) {
      snprintf(line, sizeof(line), "> %s  %s", labels[i], q.options[i]);
    } else {
      snprintf(line, sizeof(line), "  %s  %s", labels[i], q.options[i]);
    }
    drawText(line, 20, startY + i * spacing);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("Submit [OK]", 200, 230);
}

void drawPracticeFeedback() {
  tft.fillScreen(ILI9341_WHITE);
  PracticeSession &ps = practiceSessions[currentSessionIndex];
  Question &q = ps.questions[questionIndex];

  if (lastAnswerCorrect) {
    drawCentered("CORRECT !", 50);
    tft.drawLine(40, 70, 280, 70, ILI9341_BLACK);
    drawCentered(q.options[q.correctAnswer], 110);
    drawCentered(q.note, 150);
  } else {
    drawCentered("NOT QUITE", 50);
    tft.drawLine(40, 70, 280, 70, ILI9341_BLACK);
    drawCentered("Correct answer:", 100);
    char ansStr[40];
    const char* labels[] = { "A", "B", "C", "D" };
    snprintf(ansStr, sizeof(ansStr), "%s - %s", labels[q.correctAnswer], q.options[q.correctAnswer]);
    drawCentered(ansStr, 130);
    drawCentered(q.note, 160);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("Next >", 245, 230);
}

void drawPracticeCompleted() {
  tft.fillScreen(ILI9341_WHITE);
  PracticeSession &ps = practiceSessions[currentSessionIndex];

  drawCentered("PRACTICE COMPLETE", 50);
  tft.drawLine(30, 70, 290, 70, ILI9341_BLACK);

  char scoreStr[30];
  snprintf(scoreStr, sizeof(scoreStr), "Score: %d / %d", practiceScore, ps.questionCount);
  drawCentered(scoreStr, 110);

  if (practiceScore == ps.questionCount) {
    drawCentered("Parfait! Perfect!", 150);
  } else {
    drawCentered("Added mistakes to Review!", 150);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("< Practice List", 15, 230);
}

void drawReview() {
  tft.fillScreen(ILI9341_WHITE);

  // Rebuild multi-session active review queue
  reviewTotalCount = 0;
  for (int s = 0; s < NUM_SESSIONS; s++) {
    for (int q = 0; q < practiceSessions[s].questionCount; q++) {
      if (reviewNeeded[s][q]) {
        if (reviewTotalCount < MAX_TOTAL_REVIEW_ITEMS) {
          activeReviewList[reviewTotalCount].sessionIndex = s;
          activeReviewList[reviewTotalCount].questionIndex = q;
          reviewTotalCount++;
        }
      }
    }
  }

  drawText("REVIEW", 15, 25);
  tft.drawLine(15, 35, 305, 35, ILI9341_BLACK);

  if (reviewTotalCount == 0) {
    drawCentered("Nothing left to review!", 110);
    drawCentered("Tres bien !", 140);
    tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
    drawText("< Home", 15, 230);
    return;
  }

  if (currentReviewIndex >= reviewTotalCount) currentReviewIndex = 0;

  ReviewItem &item = activeReviewList[currentReviewIndex];
  Question &q = practiceSessions[item.sessionIndex].questions[item.questionIndex];

  char counter[20];
  snprintf(counter, sizeof(counter), "%d/%d", currentReviewIndex + 1, reviewTotalCount);
  int width = u8g2_for_adafruit_gfx.getUTF8Width(counter);
  drawText(counter, 305 - width, 25);

  drawText(q.prompt, 15, 60);

  int startY = 95;
  int spacing = 26;
  const char* labels[] = { "A", "B", "C", "D" };

  for (int i = 0; i < 4; i++) {
    char line[40];
    if (i == selectedOption) {
      snprintf(line, sizeof(line), "> %s  %s", labels[i], q.options[i]);
    } else {
      snprintf(line, sizeof(line), "  %s  %s", labels[i], q.options[i]);
    }
    drawText(line, 20, startY + i * spacing);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("Submit [OK]", 200, 230);
}

void drawReviewFeedback() {
  tft.fillScreen(ILI9341_WHITE);
  ReviewItem &item = activeReviewList[currentReviewIndex];
  Question &q = practiceSessions[item.sessionIndex].questions[item.questionIndex];

  if (lastAnswerCorrect) {
    drawCentered("CLEARED !", 50);
    tft.drawLine(40, 70, 280, 70, ILI9341_BLACK);
    drawCentered("Removed from Review queue.", 110);
    drawCentered(q.note, 150);
  } else {
    drawCentered("STILL NEEDS WORK", 50);
    tft.drawLine(40, 70, 280, 70, ILI9341_BLACK);
    drawCentered("Correct answer:", 100);
    char ansStr[40];
    const char* labels[] = { "A", "B", "C", "D" };
    snprintf(ansStr, sizeof(ansStr), "%s - %s", labels[q.correctAnswer], q.options[q.correctAnswer]);
    drawCentered(ansStr, 130);
    drawCentered(q.note, 160);
  }

  tft.drawLine(15, 205, 305, 205, ILI9341_BLACK);
  drawText("Next >", 245, 230);
}