:root {
  --n4-green-light: #7edb7a;
  --n4-green-strong: #4fcf66;
  --n4-green: #3aaa57;
  --n4-green-deep: #267d3c;
  --n4-ink: #1f3526;
  --n4-muted: #68766c;
  --n4-line: #dcebdd;
  --n4-soft: #f0fbf2;
  --n4-card: #ffffff;
  --n4-shadow: 0 12px 34px rgba(55, 92, 38, 0.10);
}

* { box-sizing: border-box; }

body.n4-grammar-page {
  min-height: 100vh;
  margin: 0;
  overflow: hidden;
  color: var(--n4-ink);
  background: #f8fbf4;
  font-family: "Poppins", "Noto Sans JP", sans-serif;
}

.n4-grammar-page .level-new-header {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 1000;
}

.n4-grammar-shell {
  display: grid;
  grid-template-columns: 294px minmax(0, 1fr);
  height: calc(100vh - 78px);
  margin-top: 78px;
}

.grammar-sidebar {
  position: relative;
  z-index: 30;
  display: flex;
  min-width: 0;
  flex-direction: column;
  overflow: hidden;
  background: rgba(255, 255, 252, 0.96);
  border-right: 1px solid #e5ecd9;
  box-shadow: 12px 0 30px rgba(79, 108, 53, 0.05);
}

.grammar-level-card {
  display: flex;
  align-items: center;
  gap: 13px;
  margin: 18px 18px 12px;
  padding: 14px;
  border: 1px solid #d4edd7;
  border-radius: 17px;
  background: linear-gradient(135deg, #fbfff3, #edf9ef);
}

.grammar-level-mark {
  display: grid;
  width: 52px;
  height: 52px;
  flex: 0 0 52px;
  place-items: center;
  border-radius: 15px;
  color: #246b36;
  background: linear-gradient(145deg, #b9f4bf, #70dc7a);
  font-size: 21px;
  font-weight: 800;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.75), 0 8px 18px rgba(128, 173, 24, .16);
}

.grammar-level-card div { min-width: 0; }
.grammar-level-card strong { display: block; color: #2d6a3c; font-size: 14px; }
.grammar-level-card div span { display: block; margin-top: 3px; color: #7c8b70; font-size: 11px; }

.grammar-sidebar-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 20px 9px;
  color: #738066;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .13em;
  text-transform: uppercase;
}

.grammar-sidebar-heading::after {
  content: "";
  height: 1px;
  flex: 1;
  margin-left: 12px;
  background: #e4eadb;
}

.grammar-sidebar-heading button {
  display: none;
  width: 34px;
  height: 34px;
  margin-left: 8px;
  border: 0;
  border-radius: 10px;
  background: #eff6df;
  color: #50721e;
  font-size: 23px;
  cursor: pointer;
}

.grammar-lesson-nav {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px 12px;
  scrollbar-width: thin;
  scrollbar-color: #c5d8a8 transparent;
}

.grammar-lesson-button {
  display: grid;
  width: 100%;
  grid-template-columns: 19px minmax(0,1fr) auto;
  align-items: center;
  gap: 9px;
  min-height: 45px;
  margin: 3px 0;
  padding: 8px 10px;
  border: 1px solid transparent;
  border-radius: 12px;
  color: #596454;
  background: transparent;
  text-align: left;
  font-family: inherit;
  cursor: pointer;
  transition: .2s ease;
}

.grammar-lesson-button:hover { background: #f3f8e9; color: #3c651c; }
.grammar-lesson-button.is-active {
  color: #264d13;
  border-color: #d9e9ad;
  background: linear-gradient(135deg, #bff0c4, #7cdc87);
  box-shadow: 0 8px 18px rgba(121, 165, 30, .15);
}

.grammar-lesson-icon {
  display: grid;
  width: 19px;
  height: 19px;
  place-items: center;
  color: #6d9950;
}
.grammar-lesson-icon svg { width: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.grammar-lesson-copy { min-width: 0; }
.grammar-lesson-copy strong { display: block; overflow: hidden; font-size: 12px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
.grammar-lesson-copy span { display: block; overflow: hidden; margin-top: 2px; color: inherit; opacity: .7; font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
.grammar-lesson-count { min-width: 25px; padding: 3px 7px; border-radius: 999px; background: rgba(72, 113, 30, .10); text-align: center; font-size: 10px; font-weight: 700; }

.grammar-sidebar-review {
  padding: 11px 17px 12px;
  border-top: 1px solid #edf1e7;
}
.grammar-sidebar-review > span { display: block; margin-bottom: 5px; color: #7b8770; font-size: 9px; font-weight: 800; letter-spacing: .13em; text-transform: uppercase; }
.grammar-sidebar-review button {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 9px;
  padding: 8px 6px;
  border: 0;
  color: #5d6858;
  background: transparent;
  font: 500 11px inherit;
  cursor: pointer;
}
.grammar-sidebar-review button:hover { color: #4e7e16; }
.grammar-sidebar-review svg { width: 17px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.grammar-progress-card {
  margin: 0 17px 17px;
  padding: 13px 14px;
  border: 1px solid #dce9bb;
  border-radius: 16px;
  background: linear-gradient(145deg, #fbfff4, #f0f8da);
}
.grammar-progress-card > p { margin: 0 0 10px; color: #486d20; font-size: 10px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.grammar-progress-summary { display: flex; align-items: center; gap: 12px; }
.grammar-progress-ring {
  --progress: 0;
  display: grid;
  width: 58px;
  height: 58px;
  flex: 0 0 58px;
  place-items: center;
  border-radius: 50%;
  background: conic-gradient(#8abe00 calc(var(--progress) * 1%), #e6efd8 0);
  position: relative;
}
.grammar-progress-ring::before { content: ""; position: absolute; inset: 6px; border-radius: 50%; background: #fbfff3; }
.grammar-progress-ring strong { position: relative; color: #365a16; font-size: 14px; }
.grammar-progress-summary > div:last-child strong { display: block; color: #35472d; font-size: 13px; }
.grammar-progress-summary > div:last-child span { color: #89917f; font-size: 10px; }
.grammar-progress-track { height: 5px; margin: 11px 0 9px; overflow: hidden; border-radius: 99px; background: #dfe9d0; }
.grammar-progress-track span { display: block; width: 0; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #9bd000, #d4ed4b); transition: width .3s ease; }
.grammar-progress-card button { width: 100%; border: 0; color: #789057; background: transparent; font: 600 9px inherit; cursor: pointer; }

.grammar-sidebar-overlay { display: none; }

.grammar-main {
  min-width: 0;
  overflow-y: auto;
  padding: 0 34px 28px;
  scroll-behavior: smooth;
  background:
    radial-gradient(circle at 82% 0%, rgba(205, 241, 74, .12), transparent 26%),
    linear-gradient(180deg, #ffffff 0%, #fbfdf8 100%);
}

.grammar-top-controls {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-height: 62px;
  padding: 10px 0;
  background: rgba(255,255,255,.88);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid rgba(220, 231, 211, .8);
}
.grammar-mobile-menu { display: none; }
.grammar-view-buttons { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.grammar-view-buttons button,
.grammar-mobile-menu {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 7px 15px;
  border: 1px solid #dde7d2;
  border-radius: 999px;
  color: #506048;
  background: #fff;
  box-shadow: 0 5px 14px rgba(64, 91, 48, .05);
  font: 600 11px inherit;
  cursor: pointer;
  transition: .2s ease;
}
.grammar-view-buttons button:hover { border-color: #bcd67c; color: #4d740d; }
.grammar-view-buttons button.is-active { color: #fff; border-color: #38a755; background: #38a755; box-shadow: 0 7px 18px rgba(47, 127, 69, .2); }
.grammar-view-buttons svg,
.grammar-mobile-menu svg { width: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.grammar-hero {
  position: relative;
  display: flex;
  min-height: 202px;
  align-items: center;
  justify-content: space-between;
  gap: 25px;
  margin: 10px 0 22px;
  padding: 27px 37px;
  overflow: hidden;
  border-radius: 22px;
  background:
    radial-gradient(circle at 88% 20%, rgba(255,255,255,.32) 0 7%, transparent 7.5%),
    radial-gradient(circle at 77% 78%, rgba(255,255,255,.18) 0 5%, transparent 5.5%),
    linear-gradient(110deg, #6fdb77 0%, #9be7a2 54%, #7cdd85 100%);
  box-shadow: 0 14px 35px rgba(137, 179, 25, .18);
}
.grammar-hero::after { content: "✿"; position: absolute; right: 19%; bottom: 10px; color: rgba(255,255,255,.24); font-size: 86px; transform: rotate(-14deg); }
.grammar-hero-content { position: relative; z-index: 2; max-width: 760px; }
.grammar-lesson-badge { display: inline-flex; margin-bottom: 10px; padding: 6px 11px; border-radius: 9px; color: #26733a; background: rgba(255,255,255,.82); font-size: 13px; font-weight: 800; box-shadow: 0 5px 13px rgba(97, 126, 22, .12); }
.grammar-hero h1 { margin: 0; color: #153e22; font-size: clamp(25px, 3vw, 36px); line-height: 1.25; letter-spacing: -.03em; }
.grammar-hero p { max-width: 730px; margin: 9px 0 14px; color: #2b6d3b; font-size: 13px; line-height: 1.65; }
.grammar-hero-tags { display: flex; flex-wrap: wrap; gap: 8px; }
.grammar-hero-tags span { padding: 6px 13px; border: 1px solid rgba(86, 120, 10, .14); border-radius: 999px; color: #2c6c3b; background: rgba(255,255,255,.66); font: 600 10px "Noto Sans JP", sans-serif; }
.grammar-hero-count {
  position: relative;
  z-index: 2;
  display: grid;
  width: 122px;
  height: 122px;
  flex: 0 0 122px;
  place-content: center;
  text-align: center;
  border: 7px solid rgba(255,255,255,.82);
  border-radius: 50%;
  color: #2d7a40;
  background: rgba(255,255,255,.72);
  box-shadow: 0 0 0 5px rgba(92, 134, 15, .15), 0 11px 25px rgba(86, 124, 13, .15);
}
.grammar-hero-count strong { font-size: 31px; line-height: 1; }
.grammar-hero-count span { margin-top: 5px; font-size: 9px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.grammar-hero-count i { color: #43ba58; font-style: normal; font-size: 12px; }

.grammar-search-row { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.grammar-search-box { display: flex; min-width: 0; flex: 1; align-items: center; gap: 10px; min-height: 43px; padding: 0 14px; border: 1px solid #dfe9d6; border-radius: 12px; background: #fff; box-shadow: 0 5px 18px rgba(60, 93, 39, .05); }
.grammar-search-box svg { width: 18px; flex: 0 0 18px; fill: none; stroke: #45b95a; stroke-width: 2; stroke-linecap: round; }
.grammar-search-box input {
  width: 100%;
  min-width: 0;
  border: 0 !important;
  outline: 0;
  color: #2d3c2a;
  background: transparent !important;
  box-shadow: none !important;
  appearance: none;
  -webkit-appearance: none;
  font: 500 12px inherit;
}
.grammar-search-box input::placeholder { color: #a1aa9b; }
#clearGrammarSearch { min-height: 43px; padding: 0 14px; border: 1px solid #dfe8d5; border-radius: 12px; color: #718069; background: #fff; font: 600 10px inherit; cursor: pointer; }

.grammar-group { margin-bottom: 18px; }
.grammar-group-heading { display: flex; align-items: center; gap: 11px; margin: 0 0 10px; color: #4e7915; font-size: 11px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.grammar-group-heading::after { content: ""; height: 1px; flex: 1; background: #dce8cc; }
.grammar-card-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  gap: 12px;
}

.grammar-card {
  width: 100%;
  min-width: 0;
  align-self: start;
  overflow: hidden;
  border: 1px solid #dfe7da;
  border-radius: 15px;
  background: var(--n4-card);
  box-shadow: 0 5px 18px rgba(55, 83, 42, .055);
  transition: border-color .2s ease, box-shadow .2s ease, transform .2s ease;
}
.grammar-card:hover { border-color: #a9e5af; transform: translateY(-1px); box-shadow: 0 9px 22px rgba(69, 99, 48, .08); }
.grammar-card.is-open { border-color: #62ce70; box-shadow: 0 10px 25px rgba(115, 157, 25, .12); }
.grammar-card.is-learned { border-color: #72d97f; }
.grammar-card-toggle {
  display: grid;
  width: 100%;
  grid-template-columns: 36px minmax(0, 1fr) auto 24px;
  align-items: center;
  gap: 10px;
  min-height: 68px;
  padding: 10px 15px;
  border: 0;
  color: inherit;
  background: transparent;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
}
.grammar-card-number { display: grid; width: 31px; height: 31px; place-items: center; border-radius: 50%; color: #fff; background: linear-gradient(145deg, #5fcf6d, #39a84e); font-size: 11px; font-weight: 800; box-shadow: inset 0 1px 0 rgba(255,255,255,.4); }
.grammar-card-title { min-width: 0; }
.grammar-card-title strong { display: block; overflow: hidden; color: #354333; font: 700 16px "Noto Sans JP", sans-serif; text-overflow: ellipsis; white-space: nowrap; }
.grammar-card-title small { display: none; margin-top: 2px; color: #8a9485; font-size: 10px; }
.grammar-card-meaning { max-width: min(460px, 42vw); overflow: hidden; color: #727c6e; font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
.grammar-card-arrow { width: 17px; height: 17px; fill: none; stroke: #30412e; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s ease; }
.grammar-card.is-open .grammar-card-arrow { transform: rotate(180deg); }

.grammar-card-panel { display: grid; grid-template-rows: 0fr; transition: grid-template-rows .24s ease; }
.grammar-card.is-open .grammar-card-panel { grid-template-rows: 1fr; }
.grammar-card-panel-inner { min-height: 0; overflow: hidden; }
.grammar-card-content { padding: 0 16px 16px; border-top: 1px solid #edf1e9; }
.grammar-detail-block { margin-top: 13px; }
.grammar-detail-label { display: block; margin-bottom: 5px; color: #4fbf60; font-size: 9px; font-weight: 800; letter-spacing: .1em; text-transform: uppercase; }
.grammar-formula { display: inline-flex; padding: 7px 11px; border: 1px solid #bee7c2; border-radius: 8px; color: #2f8d44; background: #effaf1; font: 700 12px "Noto Sans JP", sans-serif; }
.grammar-explanation { margin: 0; color: #5d685a; font-size: 11px; line-height: 1.7; }
.grammar-examples { display: grid; gap: 7px; }
.grammar-example { padding: 9px 11px; border-radius: 10px; background: linear-gradient(120deg, #f7faef, #f1f8e8); }
.grammar-example .jp { display: block; color: #293d28; font: 600 12px "Noto Sans JP", sans-serif; }
.grammar-example .vi { display: block; margin-top: 2px; color: #7d8979; font-size: 10px; }
.grammar-card-footer { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-top: 12px; }
.grammar-learn-button { display: inline-flex; align-items: center; gap: 7px; min-height: 34px; padding: 7px 11px; border: 1px solid #b8e2bd; border-radius: 10px; color: #338f47; background: #fff; font: 700 10px inherit; cursor: pointer; }
.grammar-learn-button svg { width: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.grammar-card.is-learned .grammar-learn-button { color: #fff; border-color: #55c66a; background: #55c66a; }
.grammar-card-code { color: #a0aa99; font-size: 9px; }

.n4-grammar-page[data-view="formula"] .grammar-card { display: block; }
.n4-grammar-page[data-view="formula"] .grammar-card-panel { display: none; }
.n4-grammar-page[data-view="formula"] .grammar-card-toggle { min-height: 61px; }
.n4-grammar-page[data-view="formula"] .grammar-card-title small { display: block; }
.n4-grammar-page[data-hide-examples="true"] .grammar-examples-block { display: none; }

.grammar-empty-state { padding: 56px 20px; text-align: center; color: #798473; }
.grammar-empty-state span { display: block; font-size: 38px; }
.grammar-empty-state strong { display: block; margin-top: 8px; color: #4a5c44; }
.grammar-empty-state p { font-size: 11px; }

.grammar-tip-bar { display: grid; grid-template-columns: auto auto 1fr auto; align-items: center; gap: 11px; margin: 18px 0 5px; padding: 13px 16px; border: 1px solid #c9e9cd; border-radius: 13px; color: #77816f; background: linear-gradient(100deg, #fbfff5, #f4fae5); font-size: 10px; }
.grammar-tip-bar strong { color: #527c17; }
.grammar-tip-bar p { margin: 0; }
.grammar-tip-icon { font-size: 17px; }
.grammar-tip-flower { color: #5dcc6a; font-size: 21px; }


.grammar-modal[hidden],
.grammar-toast[hidden] {
  display: none;
}

.grammar-modal {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: grid;
  place-items: center;
  padding: 20px;
}

.grammar-modal-backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  background: rgba(28, 39, 27, .46);
  backdrop-filter: blur(5px);
  -webkit-backdrop-filter: blur(5px);
  cursor: default;
}

.grammar-modal-dialog {
  position: relative;
  width: min(430px, 100%);
  padding: 28px 28px 24px;
  border: 1px solid #dce9c4;
  border-radius: 24px;
  color: #253421;
  background:
    radial-gradient(circle at 90% 0%, rgba(190, 230, 45, .19), transparent 32%),
    #ffffff;
  box-shadow: 0 28px 80px rgba(36, 65, 29, .22);
  text-align: center;
  animation: grammarModalIn .22s ease-out;
}

.grammar-modal-icon {
  display: grid;
  width: 64px;
  height: 64px;
  margin: 0 auto 15px;
  place-items: center;
  border-radius: 20px;
  color: #416d0b;
  background: linear-gradient(145deg, #bdf0c2, #6fd879);
  box-shadow: 0 12px 25px rgba(121, 166, 25, .22);
  font-size: 30px;
  font-weight: 800;
}

.grammar-modal-dialog.is-login-required .grammar-modal-icon {
  color: #2d673d;
  background: linear-gradient(145deg, #dcf6e3, #aee3bc);
}

.grammar-modal-dialog h2 {
  margin: 0;
  color: inherit;
  font-size: 22px;
  line-height: 1.35;
}

.grammar-modal-dialog p {
  margin: 11px 0 0;
  color: #6d7868;
  font-size: 12px;
  line-height: 1.75;
}

.grammar-modal-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 22px;
}

.grammar-modal-actions button {
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 13px;
  font: 700 11px inherit;
  cursor: pointer;
  transition: transform .18s ease, box-shadow .18s ease, border-color .18s ease;
}

.grammar-modal-actions button:hover {
  transform: translateY(-1px);
}

.grammar-modal-secondary {
  border: 1px solid #dce6d5;
  color: #687264;
  background: #ffffff;
}

.grammar-modal-primary {
  border: 1px solid #49b85b;
  color: #ffffff;
  background: linear-gradient(135deg, #4fc161, #3ea750);
  box-shadow: 0 9px 20px rgba(104, 157, 18, .2);
}

.grammar-toast {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 3100;
  max-width: min(360px, calc(100vw - 32px));
  padding: 13px 17px;
  border: 1px solid #bce4c1;
  border-radius: 14px;
  color: #2b733c;
  background: #edfaef;
  box-shadow: 0 14px 38px rgba(49, 83, 31, .18);
  font-size: 11px;
  font-weight: 600;
}

@keyframes grammarModalIn {
  from { opacity: 0; transform: translateY(10px) scale(.97); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}


/* =========================================================
   DARK MODE
   Đồng bộ bảng màu đen xám dùng chung của toàn project.
   Màu xanh lá cây sáng chỉ đóng vai trò màu nhấn của page N4.
   ========================================================= */
body.dark.n4-grammar-page,
html.dark body.n4-grammar-page {
  --n4-ink: var(--dark-text, #f4f6f8);
  --n4-muted: var(--dark-muted, #c5cad1);
  --n4-line: var(--dark-border, #5c6470);
  --n4-card: var(--dark-surface, #3f444c);

  color: var(--dark-text, #f4f6f8);
  background: var(--dark-page, #34383f) !important;
}

body.dark.n4-grammar-page .n4-grammar-shell,
html.dark body.n4-grammar-page .n4-grammar-shell {
  background: var(--dark-page, #34383f);
}

/* Header dùng đúng bảng màu Dark Mode chung của project. */
body.dark.n4-grammar-page .level-new-header,
html.dark body.n4-grammar-page .level-new-header {
  color: var(--dark-text, #f4f6f8) !important;
  background: var(--dark-surface, #3f444c) !important;
  border-bottom-color: var(--dark-border, #5c6470) !important;
  box-shadow: var(
    --dark-shadow,
    0 12px 28px rgba(10, 12, 15, 0.22)
  ) !important;
}

body.dark.n4-grammar-page .level-header-back,
body.dark.n4-grammar-page .level-header-icon,
html.dark body.n4-grammar-page .level-header-back,
html.dark body.n4-grammar-page .level-header-icon {
  color: var(--dark-text, #f4f6f8) !important;
  background: var(--dark-surface-soft, #484e57) !important;
  border-color: var(--dark-border, #5c6470) !important;
}

body.dark.n4-grammar-page .level-header-back:hover,
body.dark.n4-grammar-page .level-header-icon:hover,
html.dark body.n4-grammar-page .level-header-back:hover,
html.dark body.n4-grammar-page .level-header-icon:hover {
  color: #ffffff !important;
  background: var(--dark-surface-hover, #525965) !important;
  border-color: #7f8997 !important;
}

/* Sidebar */
body.dark.n4-grammar-page .grammar-sidebar,
html.dark body.n4-grammar-page .grammar-sidebar {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface, #3f444c);
  border-right-color: var(--dark-border, #5c6470);
  box-shadow: none;
}

body.dark.n4-grammar-page .grammar-level-card,
body.dark.n4-grammar-page .grammar-progress-card,
html.dark body.n4-grammar-page .grammar-level-card,
html.dark body.n4-grammar-page .grammar-progress-card {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface-soft, #484e57);
  border-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-level-card strong,
body.dark.n4-grammar-page .grammar-progress-card > p,
body.dark.n4-grammar-page
  .grammar-progress-summary
  > div:last-child
  strong,
html.dark body.n4-grammar-page .grammar-level-card strong,
html.dark body.n4-grammar-page .grammar-progress-card > p,
html.dark body.n4-grammar-page
  .grammar-progress-summary
  > div:last-child
  strong {
  color: var(--dark-text, #f4f6f8);
}

body.dark.n4-grammar-page .grammar-level-card div span,
body.dark.n4-grammar-page
  .grammar-progress-summary
  > div:last-child
  span,
html.dark body.n4-grammar-page .grammar-level-card div span,
html.dark body.n4-grammar-page
  .grammar-progress-summary
  > div:last-child
  span {
  color: var(--dark-muted, #c5cad1);
}

body.dark.n4-grammar-page .grammar-sidebar-heading,
body.dark.n4-grammar-page .grammar-sidebar-review > span,
html.dark body.n4-grammar-page .grammar-sidebar-heading,
html.dark body.n4-grammar-page .grammar-sidebar-review > span {
  color: var(--dark-muted, #c5cad1);
}

body.dark.n4-grammar-page .grammar-sidebar-heading::after,
body.dark.n4-grammar-page .grammar-sidebar-review,
html.dark body.n4-grammar-page .grammar-sidebar-heading::after,
html.dark body.n4-grammar-page .grammar-sidebar-review {
  border-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-lesson-button,
html.dark body.n4-grammar-page .grammar-lesson-button {
  color: var(--dark-muted, #c5cad1);
}

body.dark.n4-grammar-page .grammar-lesson-button:hover,
html.dark body.n4-grammar-page .grammar-lesson-button:hover {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface-hover, #525965);
}

body.dark.n4-grammar-page .grammar-lesson-button.is-active,
html.dark body.n4-grammar-page .grammar-lesson-button.is-active {
  color: #223000;
  border-color: rgba(190, 231, 54, 0.72);
  background: linear-gradient(135deg, #9ce6a4, #66cd71);
  box-shadow: 0 8px 18px rgba(160, 207, 43, 0.14);
}

body.dark.n4-grammar-page .grammar-progress-ring::before,
html.dark body.n4-grammar-page .grammar-progress-ring::before {
  background: var(--dark-surface-soft, #484e57);
}

body.dark.n4-grammar-page .grammar-progress-track,
html.dark body.n4-grammar-page .grammar-progress-track {
  background: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-progress-card button,
html.dark body.n4-grammar-page .grammar-progress-card button {
  color: #aee5b5;
}

/* Nội dung chính: đúng màu đen xám của project, không dùng xanh đen. */
body.dark.n4-grammar-page .grammar-main,
html.dark body.n4-grammar-page .grammar-main {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-page, #34383f) !important;
}

body.dark.n4-grammar-page .grammar-top-controls,
html.dark body.n4-grammar-page .grammar-top-controls {
  background: rgba(52, 56, 63, 0.95);
  border-bottom-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-view-buttons button,
body.dark.n4-grammar-page #clearGrammarSearch,
html.dark body.n4-grammar-page .grammar-view-buttons button,
html.dark body.n4-grammar-page #clearGrammarSearch {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface-soft, #484e57);
  border-color: var(--dark-border, #5c6470);
  box-shadow: none;
}

body.dark.n4-grammar-page .grammar-view-buttons button:hover,
body.dark.n4-grammar-page #clearGrammarSearch:hover,
html.dark body.n4-grammar-page .grammar-view-buttons button:hover,
html.dark body.n4-grammar-page #clearGrammarSearch:hover {
  color: #ffffff;
  background: var(--dark-surface-hover, #525965);
  border-color: #7f8997;
}

body.dark.n4-grammar-page .grammar-view-buttons button.is-active,
html.dark body.n4-grammar-page .grammar-view-buttons button.is-active {
  color: #ffffff;
  background: #4d8657;
  border-color: #64a86f;
}

/* Ô tìm kiếm dùng cùng surface/input của các page khác. */
body.dark.n4-grammar-page .grammar-search-box,
html.dark body.n4-grammar-page .grammar-search-box {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface-soft, #484e57) !important;
  border-color: var(--dark-border, #5c6470) !important;
  box-shadow: none;
}

body.dark.n4-grammar-page .grammar-search-box:focus-within,
html.dark body.n4-grammar-page .grammar-search-box:focus-within {
  border-color: #57b967 !important;
  box-shadow: 0 0 0 3px rgba(183, 227, 25, 0.12);
}

body.dark.n4-grammar-page .grammar-search-box input,
body.dark.n4-grammar-page .grammar-search-box input:focus,
body.dark.n4-grammar-page .grammar-search-box input:active,
html.dark body.n4-grammar-page .grammar-search-box input,
html.dark body.n4-grammar-page .grammar-search-box input:focus,
html.dark body.n4-grammar-page .grammar-search-box input:active {
  color: var(--dark-text, #f4f6f8) !important;
  background: transparent !important;
  border: 0 !important;
  outline: 0 !important;
  box-shadow: none !important;
  -webkit-text-fill-color: var(--dark-text, #f4f6f8);
  color-scheme: dark;
}

body.dark.n4-grammar-page
  .grammar-search-box
  input::placeholder,
html.dark body.n4-grammar-page
  .grammar-search-box
  input::placeholder {
  color: var(--dark-muted, #c5cad1) !important;
  -webkit-text-fill-color: var(--dark-muted, #c5cad1);
  opacity: 1;
}

body.dark.n4-grammar-page
  .grammar-search-box
  input::-webkit-search-cancel-button,
html.dark body.n4-grammar-page
  .grammar-search-box
  input::-webkit-search-cancel-button {
  filter: invert(0.9);
}

/* Thẻ ngữ pháp */
body.dark.n4-grammar-page .grammar-group-heading,
html.dark body.n4-grammar-page .grammar-group-heading {
  color: #9bddaa;
}

body.dark.n4-grammar-page .grammar-group-heading::after,
html.dark body.n4-grammar-page .grammar-group-heading::after {
  background: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-card,
html.dark body.n4-grammar-page .grammar-card {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface, #3f444c);
  border-color: var(--dark-border, #5c6470);
  box-shadow: var(
    --dark-shadow,
    0 12px 28px rgba(10, 12, 15, 0.22)
  );
}

body.dark.n4-grammar-page .grammar-card:hover,
body.dark.n4-grammar-page .grammar-card.is-open,
html.dark body.n4-grammar-page .grammar-card:hover,
html.dark body.n4-grammar-page .grammar-card.is-open {
  background: var(--dark-surface, #3f444c);
  border-color: #64c774;
}

body.dark.n4-grammar-page .grammar-card-title strong,
body.dark.n4-grammar-page .grammar-example .jp,
html.dark body.n4-grammar-page .grammar-card-title strong,
html.dark body.n4-grammar-page .grammar-example .jp {
  color: var(--dark-text, #f4f6f8);
}

body.dark.n4-grammar-page .grammar-card-title small,
body.dark.n4-grammar-page .grammar-card-meaning,
body.dark.n4-grammar-page .grammar-explanation,
body.dark.n4-grammar-page .grammar-example .vi,
html.dark body.n4-grammar-page .grammar-card-title small,
html.dark body.n4-grammar-page .grammar-card-meaning,
html.dark body.n4-grammar-page .grammar-explanation,
html.dark body.n4-grammar-page .grammar-example .vi {
  color: var(--dark-muted, #c5cad1);
}

body.dark.n4-grammar-page .grammar-card-content,
html.dark body.n4-grammar-page .grammar-card-content {
  border-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-formula,
body.dark.n4-grammar-page .grammar-example,
body.dark.n4-grammar-page .grammar-learn-button,
html.dark body.n4-grammar-page .grammar-formula,
html.dark body.n4-grammar-page .grammar-example,
html.dark body.n4-grammar-page .grammar-learn-button {
  background: var(--dark-surface-soft, #484e57);
  border-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-formula,
body.dark.n4-grammar-page .grammar-learn-button,
html.dark body.n4-grammar-page .grammar-formula,
html.dark body.n4-grammar-page .grammar-learn-button {
  color: #b7e8be;
}

body.dark.n4-grammar-page .grammar-tip-bar,
html.dark body.n4-grammar-page .grammar-tip-bar {
  color: var(--dark-muted, #c5cad1);
  background: var(--dark-surface, #3f444c);
  border-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-empty-state strong,
html.dark body.n4-grammar-page .grammar-empty-state strong {
  color: var(--dark-text, #f4f6f8);
}

/* Popup */
body.dark.n4-grammar-page .grammar-modal-backdrop,
html.dark body.n4-grammar-page .grammar-modal-backdrop {
  background: rgba(12, 14, 18, 0.62);
}

body.dark.n4-grammar-page .grammar-modal-dialog,
html.dark body.n4-grammar-page .grammar-modal-dialog {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface, #3f444c);
  border-color: var(--dark-border, #5c6470);
  box-shadow: 0 28px 80px rgba(8, 10, 13, 0.46);
}

body.dark.n4-grammar-page .grammar-modal-dialog p,
html.dark body.n4-grammar-page .grammar-modal-dialog p {
  color: var(--dark-muted, #c5cad1);
}

body.dark.n4-grammar-page .grammar-modal-secondary,
html.dark body.n4-grammar-page .grammar-modal-secondary {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface-soft, #484e57);
  border-color: var(--dark-border, #5c6470);
}

body.dark.n4-grammar-page .grammar-toast,
html.dark body.n4-grammar-page .grammar-toast {
  color: var(--dark-text, #f4f6f8);
  background: var(--dark-surface, #3f444c);
  border-color: var(--dark-border, #5c6470);
}

@media (max-width: 1100px) {
  .n4-grammar-shell { grid-template-columns: 250px minmax(0,1fr); }
  .grammar-main { padding-inline: 22px; }
  .grammar-card-grid { grid-template-columns: 1fr; }
  .grammar-card-meaning { max-width: 320px; }
}

@media (max-width: 820px) {
  body.n4-grammar-page { overflow: auto; }
  .n4-grammar-shell { display: block; height: auto; min-height: calc(100vh - 70px); margin-top: 70px; }
  .grammar-sidebar {
    position: static;
    width: 100%;
    max-height: 390px;
    transform: none;
    border-right: 0;
    border-bottom: 1px solid #e5ecd9;
    box-shadow: none;
  }
  .grammar-sidebar-heading button,
  .grammar-sidebar-overlay,
  .grammar-mobile-menu { display: none !important; }
  .grammar-main { min-height: calc(100vh - 70px); overflow: visible; padding: 0 16px 24px; }
  .grammar-top-controls { justify-content: flex-end; }
  .grammar-view-buttons button { padding-inline: 11px; font-size: 9px; }
  .grammar-view-buttons button span,
  .grammar-view-buttons button svg { display: none; }
  .grammar-hero { min-height: 180px; padding: 23px; }
  .grammar-hero-count { width: 96px; height: 96px; flex-basis: 96px; }
  .grammar-hero-count strong { font-size: 25px; }
}

@media (max-width: 580px) {
  .grammar-top-controls { align-items: flex-start; gap: 8px; }
  .grammar-view-buttons { gap: 5px; }
  .grammar-view-buttons button { min-height: 31px; padding: 6px 9px; }
  .grammar-hero { align-items: flex-start; padding: 21px 18px; border-radius: 17px; }
  .grammar-hero h1 { font-size: 24px; }
  .grammar-hero-count { position: absolute; top: 15px; right: 15px; width: 70px; height: 70px; border-width: 4px; }
  .grammar-hero-count strong { font-size: 20px; }
  .grammar-hero-count span { font-size: 7px; }
  .grammar-hero-content { padding-right: 58px; }
  .grammar-hero-tags { padding-right: 0; }
  .grammar-search-row { align-items: stretch; }
  #clearGrammarSearch { padding-inline: 10px; }
  .grammar-card-toggle { grid-template-columns: 33px minmax(0,1fr) 20px; }
  .grammar-card-meaning { display: none; }
  .grammar-card-title strong { font-size: 14px; }
  .grammar-tip-bar { grid-template-columns: auto 1fr auto; }
  .grammar-tip-bar p { grid-column: 1 / -1; }
}


@media (max-width: 460px) {
  .grammar-modal-dialog { padding: 24px 19px 20px; }
  .grammar-modal-actions { grid-template-columns: 1fr; }
  .grammar-toast { right: 16px; bottom: 16px; }
}
