/* Responsive safeguards and layout overrides for the existing MR.KOVH design. */
*,
*::before,
*::after { box-sizing: border-box; }

html { width: 100%; min-width: 320px; scroll-padding-top: 12px; }
body { width: 100%; min-width: 320px; margin: 0; }
body, button, input, select, textarea { font-family: inherit; }
img, video, canvas, svg, iframe { max-width: 100%; }
img, video { height: auto; }

.app { width: 100%; min-width: 0; min-height: 100vh; min-height: 100dvh; }
.side { width: 286px; min-width: 0; height: 100vh; height: 100dvh; overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; }
.main { width: calc(100% - 286px); min-width: 0; }
.main, .page, .card, .grid, .top, .top-title, .top-actions,
.page > *, [class*="-grid"], [class*="-layout"] { min-width: 0; }

.top { gap: 16px; }
.top-title { overflow-wrap: anywhere; }
.top h1, h1, h2, h3, h4, p, li, label, small, td, th,
.nav-label, .mr-demo-creds, .mr-plan-card, .legal-wrap {
  overflow-wrap: anywhere;
  word-break: normal;
}
.top-actions { display: flex; align-items: center; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }

button, .btn, .choice, .sql-tab, .filter-btn { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
input, select, textarea {
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]):not([type="file"]),
select, textarea { width: 100%; }
input[type="file"] { width: 100%; }
textarea { resize: vertical; }

.grid, .g2, .g3, .g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.card, .hero, .mr-premium-gate, .mr-admin-stat { max-width: 100%; }
.task, .row, .lesson, .section, .progressline { min-width: 0; flex-wrap: wrap; }
.task .push { margin-left: auto; }

.table-container {
  display: block;
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  -webkit-overflow-scrolling: touch;
}
.table-container > table { width: max-content; min-width: 100%; max-width: none; }
.table-container > .table { overflow: visible; }
.table-container:focus-visible { outline: 2px solid #5b5dde; outline-offset: 2px; }

.modal { padding: clamp(10px, 3vw, 24px); overflow-y: auto; overflow-x: hidden; }
.box {
  width: min(900px, 100%);
  max-width: 100%;
  max-height: min(90vh, 90dvh);
  overflow-x: hidden;
  overflow-y: auto;
  overflow-wrap: anywhere;
  margin: auto;
}
.mr-auth-overlay { overflow-y: auto; overflow-x: hidden; }
.mr-auth-card { max-width: 100%; max-height: calc(100vh - 24px); max-height: calc(100dvh - 24px); overflow-y: auto; }

.rb-layout, .talk-layout, .sql-doc-layout, .sql-lab-shell,
.sql-learning-grid, .playground-shell, .interview-layout { min-width: 0; }
.rb-layout > *, .rb-panel, .rb-field, .rb-row > *, .rb-section-title > * { min-width: 0; }
.rb-preview { max-width: 100%; overflow: auto; }
.resume-paper { max-width: 100%; }
.rb-template-list, .sql-tabs, .sqlcc-tabs, .tl-file-tabs { max-width: 100%; }
.tl-compiler, .playground-editor, .playground-result, .result-grid { min-width: 0; max-width: 100%; }
.sql-result, .sqlcc-output, .result-grid { overflow-x: auto; }
pre, .code { max-width: 100%; white-space: pre-wrap; overflow-wrap: anywhere; }

.mobile-header-controls, .mobile-menu-toggle, .mobile-brand-logo, .nav-scrim { display: none; }
.nav-scrim { border: 0; padding: 0; }

@media (min-width: 1440px) {
  .main { padding-inline: clamp(38px, 3vw, 72px); }
}

@media (max-width: 1100px) {
  .main { padding: 24px clamp(18px, 3vw, 30px) 48px; }
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rb-layout { grid-template-columns: minmax(0, 1fr) !important; }
  .rb-layout > *, .rb-panel { width: 100%; max-width: 100%; min-width: 0; }
  .rb-template-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(145px, 100%), 1fr)); overflow: visible; }
  .rb-template { width: 100%; min-width: 0; max-width: none; }
}

@media (max-width: 850px) {
  .side { width: 78px; }
  .main { width: calc(100% - 78px); margin-left: 78px; padding: 20px 18px 40px; }
  .top { align-items: flex-start; }
  .top-actions { gap: 7px; }
}

@media (max-width: 680px) {
  .side {
    position: fixed;
    z-index: 41;
    inset: 0 auto 0 0;
    width: min(84vw, 310px);
    height: 100vh;
    height: 100dvh;
    padding: 16px 12px max(18px, env(safe-area-inset-bottom));
    transform: translateX(-105%);
    transition: transform .22s ease;
    box-shadow: 10px 0 34px rgba(24, 34, 58, .16);
  }
  body.nav-open { overflow: hidden; }
  body.nav-open .side { transform: translateX(0); }
  .side .brand { padding: 8px 10px 16px; }
  .side .brand-logo { width: 190px; height: auto; max-height: 64px; }
  .side .nav { gap: 4px; }
  .side .nav button { justify-content: flex-start; gap: 10px; min-height: 46px; padding: 7px 9px; }
  .side .nav-label { display: block; white-space: normal; text-overflow: clip; overflow: visible; }
  .side .nav-icon { width: 34px; height: 34px; flex-basis: 34px; }
  body.nav-open .nav-scrim {
    display: block;
    position: fixed;
    z-index: 40;
    inset: 0;
    width: 100%;
    height: 100%;
    background: rgba(15, 23, 42, .42);
  }
  .app { display: block; }
  .main { width: 100%; margin: 0; padding: 14px clamp(12px, 4vw, 20px) 36px; }
  .top { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; margin-bottom: 18px; }
  .top-title { display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: start; gap: 10px; }
  .mobile-header-controls { display: flex; flex-direction: column; align-items: center; gap: 8px; }
  .mobile-brand-logo { display: block; width: 38px; height: auto; }
  .mobile-menu-toggle { display: inline-grid; place-items: center; width: 44px; height: 44px; flex: none; border: 1px solid #e0e4ed; border-radius: 12px; background: #fff; color: #303a4d; font-size: 21px; }
  .top-title h1 { font-size: clamp(18px, 5vw, 24px); line-height: 1.2; }
  .top-actions { grid-column: 1; justify-content: flex-start; padding-left: 54px; }
  .hero { padding: clamp(18px, 5vw, 26px); }
  .g4, .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rb-layout, .talk-layout, .coach-grid, .sql-doc-layout, .sql-lab-shell,
  .sql-learning-grid, .playground-shell, .interview-layout { grid-template-columns: minmax(0, 1fr) !important; }
  .rb-row, .rb-row.three { grid-template-columns: minmax(0, 1fr); }
  .rb-template-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rb-template-grid > * { min-width: 0; }
  .stack-diagram, .big-diagram { width: 100%; max-width: 100%; min-width: 0; }
  .big-diagram { grid-template-columns: minmax(0, 1fr) !important; }
  .big-diagram > * { min-width: 0; }
  .big-connector { font-size: 0; }
  .big-connector::after { content: '↓'; font-size: 22px; }
  #backendlab .tl-layout { grid-template-columns: minmax(0, 1fr) !important; }
  .talk-side { order: initial; }
  .rb-preview-wrap { position: static; }
  .sql-status, .speak-reading-duration { white-space: normal; }
  .modal { align-items: flex-start; padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom)); }
  .box { width: 100%; max-height: calc(100vh - 20px); max-height: calc(100dvh - 20px); padding: clamp(15px, 4vw, 22px); border-radius: 14px; }
  .mr-auth-overlay { place-items: start center; padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom)); }
  .mr-auth-card { width: min(520px, 100%); padding: clamp(17px, 5vw, 23px); }
  .mr-plan-grid, .mr-plan-card { min-width: 0; }
  .mr-plan-card strong { font-size: clamp(16px, 5vw, 20px); }
  .section { align-items: flex-start; gap: 10px; }
  .section > * { min-width: 0; }
}

@media (max-width: 430px) {
  .main { padding-inline: 12px; }
  .g4, .g3, .g2, .choices, .sql-chip-grid, .sql-topic-grid,
  .mock-dashboard, .sqlcc-topic-grid, .sqlcc-stats, .coach-metrics { grid-template-columns: minmax(0, 1fr); }
  .top-actions { gap: 6px; }
  .plan-badge { font-size: 10px; }
  .avatar { width: 36px; height: 36px; }
  .card { padding: 15px; }
  .table-container > .table { min-width: 520px; }
  .table-container > .mr-admin-table, .table-container > .pricing-table { min-width: 560px; }
  .rb-row, .rb-row.three { grid-template-columns: minmax(0, 1fr); }
  .quiz-body { padding: 17px; }
  .talk-review-grid, .quiz-result-breakdown { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sqlcc-hero h2 { font-size: clamp(25px, 8vw, 32px); }
  .speak-video-controls .btn, .speak-scroll-controls .btn { min-width: 0; }
}

@media (max-width: 360px) {
  .main { padding-inline: 10px; }
  .mobile-header-controls { gap: 5px; }
  .mobile-menu-toggle { width: 40px; height: 40px; }
  .top-title { gap: 8px; }
  .top-actions { padding-left: 48px; }
  .mr-auth-tabs { flex-wrap: wrap; }
  .coach-actions, .talk-actions-bottom, .practice-actions { align-items: stretch; }
  .coach-actions > *, .talk-actions-bottom > *, .practice-actions > * { flex: 1 1 auto; }
}

@media (orientation: landscape) and (max-height: 520px) {
  .side { padding-block: 10px; }
  .side .nav button { min-height: 40px; }
  .mr-auth-overlay { align-items: start; }
  .mr-auth-card { max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); }
  .box { max-height: calc(100vh - 16px); max-height: calc(100dvh - 16px); }
}

/* Let modal content use the overlay's scroll area instead of nested scrollbars. */
.modal { align-items: flex-start; overflow-y: auto; overscroll-behavior: contain; }
.modal.open { align-items: flex-start; }
.modal .box { max-height: none; overflow: visible; margin: auto; }
@media (max-width: 680px) { .modal .box { max-height: none; overflow: visible; margin: 0 auto; } }
