/* ==========================================================================
   Comprehensive Responsive & Cross-Browser Design System
   Compatible with Google Chrome, Mozilla Firefox, Brave, Vivaldi, Safari & Edge
   Optimized for Mobile (Small/Medium/Large), Tablets, Laptops & Desktop
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. Universal Cross-Browser Normalization & Root Settings
   -------------------------------------------------------------------------- */
:root {
  --screen-xs: 380px;
  --screen-sm: 576px;
  --screen-md: 768px;
  --screen-lg: 992px;
  --screen-xl: 1200px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

/* Standard thin scrollbar for Mozilla Firefox & Gecko */
* {
  scrollbar-width: thin;
  scrollbar-color: rgba(140, 150, 170, 0.35) transparent;
}

html.dark-mode * {
  scrollbar-color: rgba(255, 255, 255, 0.22) transparent;
}

/* Custom smooth scrollbar for Webkit/Chromium (Chrome, Brave, Vivaldi, Edge) */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: rgba(140, 150, 170, 0.35);
  border-radius: 6px;
}

::-webkit-scrollbar-thumb:hover {
  background: rgba(140, 150, 170, 0.55);
}

html.dark-mode ::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.22);
}

html.dark-mode ::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.38);
}

/* Prevent horizontal body overflow & jumpy layout */
html {
  overflow-x: hidden;
  max-width: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  max-width: 100vw;
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  padding-bottom: var(--safe-bottom);
}

/* --------------------------------------------------------------------------
   2. Main Card & App Container Fluidity
   -------------------------------------------------------------------------- */
.card {
  max-width: calc(100vw - 24px);
  box-sizing: border-box;
}

/* --------------------------------------------------------------------------
   3. Tablet & Medium Screens (769px to 1024px)
   -------------------------------------------------------------------------- */
@media screen and (max-width: 1024px) {
  body {
    padding: 16px 10px;
    align-items: flex-start;
    height: auto;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .card {
    width: 94vw;
    max-width: 94vw;
    margin: 84px auto 40px auto;
    padding: 24px 20px;
    /* fixed to the screen like on desktop: the task list scrolls inside the card
       and the page itself stays one screen tall */
    min-height: 0;
    height: max(460px, calc(100vh - 230px));
    height: max(460px, calc(100dvh - 230px));
  }

  .sidebar {
    width: 290px;
  }

  /* Folder tabs: on desktop they hang above the card, but the labels are far too
     long in many languages to fit there on a small screen (up to 490px too wide
     at 320px). Here they sit inside the card, in rows that wrap, so every tab is
     visible in every language and nothing has to be swiped to.
     `display` is left alone: the app shows either the task or the timetable group. */
  .card > .tabs-container,
  .card > .tabs-container-right,
  .card > .schedule-tabs-right {
    position: relative;
    top: auto !important;
    left: auto !important;
    right: auto !important;
    flex-shrink: 0;
    flex-wrap: wrap;
    align-items: stretch;
    width: 100%;
    max-width: 100%;
    gap: 4px;
    margin: 0 0 6px;
  }

  .card > .tabs-container-right,
  .card > .schedule-tabs-right {
    order: -2;
  }

  .card > .tabs-container {
    order: -1;
  }

  .card > .tabs-container .extra-tab,
  .card > .tabs-container-right .tab-dropdown-wrapper,
  .card > .schedule-tabs-right .tab-dropdown-wrapper {
    flex: 1 1 auto;
    min-width: 0;
  }

  .card > .tabs-container-right .tab-dropdown-wrapper > .extra-tab,
  .card > .schedule-tabs-right .tab-dropdown-wrapper > .extra-tab {
    width: 100%;
  }

  .card > .tabs-container .extra-tab,
  .card > .tabs-container-right .extra-tab,
  .card > .schedule-tabs-right .extra-tab {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 30px;
    margin: 0;
    padding: 4px 8px;
    border-radius: 8px;
    border-bottom-width: 1px;
    border-bottom-style: solid;
    font-size: 12px;
    line-height: 1.2;
    text-align: center;
    white-space: normal;
    overflow-wrap: anywhere;
    transform: none;
  }

  /* Long languages can need several rows of tabs. The list keeps a usable height:
     the card may then grow past its normal size and the page scrolls a little.
     `contain: size` stops the list's own tasks from stretching the card; they
     still scroll inside it. */
  .card:has(.list-container) {
    min-height: fit-content;
  }

  .card > .list-container,
  .card > #scheduleViewContainer {
    contain: size;
    min-height: 130px;
  }

  .settings-modal-dialog {
    width: min(92vw, 760px);
    max-height: 88vh;
    max-height: 88dvh;
  }

  /* The task-details arrows float outside the card on desktop; there is no room
     for that here, so they move inside the card's right edge and the list
     leaves space for them */
  .task-side-arrows-strip {
    left: auto;
    right: 2px;
    width: 38px;
  }

  .side-task-arrow-btn {
    left: 1px !important;
  }

  .card:has(.side-task-arrow-btn) .list-container {
    padding-right: 32px;
  }

  /* Edit mode on tablets: the card narrows beside the 320px edit panel instead
     of sliding 240px left (which pushed it off the screen) */
  .card.shifted-left {
    transform: none;
    width: calc(100vw - 352px);
    max-width: none;
    margin-left: 16px;
  }

  /* The timetable "Activity" editor lives inside the card, whose will-change
     traps the fixed backdrop in the card's box. While it is open, release it so
     it covers the whole screen above the top controls. */
  .card:has(.slot-editor-backdrop:not([hidden])) {
    will-change: auto;
    transform: none !important;
    z-index: 1000000;
  }

  .slot-editor-dialog {
    max-height: calc(100dvh - 32px);
  }

  /* leave room under the card so the fixed footer never covers the action bar */
  body {
    padding-bottom: 90px;
  }
}

/* --------------------------------------------------------------------------
   4. Mobile Large & Phablets (481px to 768px)
   -------------------------------------------------------------------------- */
@media screen and (max-width: 768px) {
  body {
    padding: 10px 6px;
    display: block;
    height: auto;
  }

  /* Edit panel on phones: a bottom sheet instead of a side panel, so the card
     stays in place and the tasks above it can still be tapped and selected */
  .right-panel {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 40vh;
    height: 40dvh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -6px 24px rgba(0, 0, 0, 0.25);
    transform: translateY(105%);
    overflow: hidden;
  }

  .right-panel.active {
    transform: translateY(0);
  }

  .card.shifted-left {
    transform: none;
    width: 96vw;
    max-width: 96vw;
    margin-left: auto;
  }

  /* while editing, the card ends where the sheet begins */
  body:has(.right-panel.active) .card:has(.list-container) {
    height: max(240px, calc(60vh - 84px));
    height: max(240px, calc(60dvh - 84px));
  }

  /* if a long language makes the card taller than the free space, the page can
     still scroll it clear of the sheet */
  body:has(.right-panel.active) {
    padding-bottom: 40vh;
    padding-bottom: 40dvh;
  }

  /* more room for the tasks while editing: the big title and the counter step aside
     (the class name stays, so it is still clear which list is being edited) */
  body:has(.right-panel.active) .card > .task-counter,
  body:has(.right-panel.active) .card > h2 {
    display: none !important; /* the counter's display is set inline by the app */
  }

  /* the class drawer handle would sit on the Close button while editing */
  body:has(.right-panel.active) .sidebar.closed .toggle-btn {
    visibility: hidden;
  }

  /* On phones the footer follows the card instead of floating over it */
  footer {
    position: static;
    padding: 4px 0 calc(12px + var(--safe-bottom));
  }

  /* Top Right Controls Responsive Bar */
  .top-controls-wrapper {
    position: fixed;
    top: 10px;
    right: 10px;
    z-index: 1500;
    display: flex;
    align-items: center;
    gap: 6px;
    /* no backdrop-filter: it would become the frame for the fixed language menu
       inside this bar, which then opens off the screen */
    background: rgba(255, 255, 255, 0.96);
    padding: 4px 8px;
    border-radius: 24px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08);
  }

  html.dark-mode .top-controls-wrapper {
    background: rgba(20, 20, 20, 0.96);
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  }

  .lang-switcher-btn,
  .top-settings-btn,
  .top-notifications-btn,
  .ja-vertical-toggle-btn {
    min-width: 36px;
    min-height: 36px;
    padding: 6px;
  }

  /* the language menu's phone layout is in style.css: every page needs it, and
     only the app loads this file */

  /* Main Card Mobile Adaptations */
  .card {
    width: 96vw;
    max-width: 96vw;
    /* clears the fixed top controls and the accessibility button */
    margin: 72px auto 24px auto;
    padding: 18px 14px;
    border-radius: 12px;
    min-height: 0;
    height: max(420px, calc(100vh - 188px));
    height: max(420px, calc(100dvh - 188px));
  }

  /* Sidebar off-canvas mobile behavior */
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: min(85vw, 320px);
    height: 100vh;
    height: 100dvh;
    z-index: 2500;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.25);
  }

  /* On phones the modals fill the screen, so the floating accessibility button
     goes under them (still above the page and the top controls) */
  #a11yWidgetContainer {
    z-index: 9000 !important;
  }

  /* the floating accessibility button would sit on the open drawer's title */
  body:has(.sidebar:not(.closed)) #a11yWidgetContainer {
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
  }

  .sidebar .toggle-btn {
    right: -34px;
    width: 34px;
    height: 58px;
    border-top-right-radius: 10px;
    border-bottom-right-radius: 10px;
  }

  /* Task Input & Add section */
  .input-container {
    display: flex;
    flex-wrap: nowrap;
    gap: 8px;
  }

  /* Task List Item Responsive Layout */
  .item {
    padding: 10px 8px;
    gap: 6px;
  }

  .item .task-text,
  .item input[type="text"] {
    font-size: 14px;
    min-height: 38px;
  }

  /* Settings (and the notes window that shares its look): sized to the visible
     screen, so the header with its close button and the footer always fit */
  .settings-modal-backdrop {
    padding: 8px;
    height: 100vh;
    height: 100dvh;
  }

  .settings-modal-dialog {
    width: 100%;
    max-width: 100%;
    height: auto;
    max-height: 100%;
    margin: 0;
    border-radius: 14px;
  }

  .settings-modal-body {
    flex: 1 1 auto;
    min-height: 0;
  }

  /* All tabs visible at once: a wrapping grid instead of a strip that had to be
     swiped sideways (with a hidden scrollbar, most tabs looked missing) */
  .settings-navbar-wrapper {
    overflow: visible;
    padding: 8px 10px;
  }

  .settings-nav-tabs {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
    gap: 6px;
    white-space: normal;
  }

  .settings-nav-tab {
    justify-content: center;
    text-align: center;
    min-height: 34px;
    padding: 6px 6px;
    font-size: 12px;
    line-height: 1.2;
    white-space: normal;
    border-bottom: 1px solid #e2e8f0;
    border-radius: 8px;
  }

  html.dark-mode .settings-nav-tab,
  body.dark-mode .settings-nav-tab {
    border-bottom-color: #333333;
  }

  /* icon, label and the "beta" pill share a small tab: let them wrap inside it */
  .settings-nav-tab {
    flex-wrap: wrap;
    gap: 2px 4px;
    min-width: 0;
    overflow: hidden;
  }

  .settings-nav-tab .settings-beta-pill {
    margin-left: 0;
    padding: 0 4px;
    font-size: 9px;
  }

  .settings-modal-body {
    padding: 16px 14px;
  }

  .settings-section {
    padding: 14px 12px;
  }

  .settings-toggle-row {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
  }

  .toggle-row-info {
    max-width: calc(100% - 60px);
  }

  /* Schedule / Timetable Views on Mobile */
  .schedule-view-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sched-week-grid,
  .schedule-grid-table {
    min-width: 580px;
  }

  .calendar-cards-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 8px;
  }

  /* Timetable on phones: every pixel of height goes to the week */
  .card.is-schedule-mode {
    padding-top: 10px;
    padding-bottom: 8px;
  }

  .card.is-schedule-mode > h2 {
    margin: 0 0 4px;
    font-size: 20px;
  }

  .card.is-schedule-mode > h2 .todo,
  .card.is-schedule-mode > h2 a {
    padding: 2px 10px;
  }

  .card.is-schedule-mode > #scheduleViewContainer {
    padding: 2px 0;
    gap: 0;
  }

  .card.is-schedule-mode .actions .btn {
    padding-top: 6px;
    padding-bottom: 6px;
    line-height: 1.2;
  }

  /* Full timetable on phones: the whole week fits the screen width (like a
     printed university timetable) instead of a 640px table to swipe around */
  .uni-table {
    min-width: 0;
    font-size: 10px;
  }

  /* hour-row height comes from fitHourGrid() in main.js (fits the screen,
     never below 40px) */

  /* inside an ODD/EVEN half the week note repeats the half's own label */
  .uni-half .uni-card em {
    display: none;
  }

  .uni-table th:first-child {
    width: 36px;
  }

  .uni-table th {
    padding: 4px 1px;
    font-size: 9.5px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .uni-table caption,
  .uni-caption-title {
    font-size: 16px;
  }

  .uni-caption-week {
    font-size: 13px;
    gap: 4px;
  }

  .uni-stack {
    inset: 2px;
    gap: 2px;
  }

  .uni-half {
    padding-top: 10px;
  }

  .uni-half > b {
    top: 0;
    right: 2px;
    font-size: 7.5px;
  }

  .uni-card {
    padding: 3px 4px;
    border-radius: 5px;
    gap: 1px;
    line-height: 1.2;
  }

  .uni-card strong {
    font-size: 10px;
    overflow-wrap: anywhere;
  }

  .uni-card span,
  .uni-card em {
    font-size: 9px;
    overflow-wrap: anywhere;
  }

  /* Optimised/Quick plan cards: one column on phones, the countdown sits under
     the activity instead of on top of its title */
  .sched-plan-card {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "info" "link" "side";
    gap: 6px;
    padding: 10px 62px 10px 12px;
  }

  .sched-plan-info {
    gap: 10px;
    flex-wrap: wrap;
  }

  .sched-plan-linkcol.is-empty {
    display: none;
  }

  .sched-plan-side {
    justify-self: start;
    align-items: flex-start;
    max-width: none;
  }

  .sched-plan-card .sched-plan-count {
    position: static;
  }

  /* week arrows in the timetable header: finger-sized */
  .uni-week-nav {
    min-width: 32px;
    min-height: 32px;
  }
}

/* --------------------------------------------------------------------------
   5. Small Mobile Screens & Smart Watches (<= 480px)
   -------------------------------------------------------------------------- */
@media screen and (max-width: 480px) {
  body {
    padding: 4px;
  }

  .card,
  .card.shifted-left {
    width: calc(100vw - 8px);
    max-width: calc(100vw - 8px);
    margin: 72px auto 16px auto;
    padding: 14px 10px;
    border-radius: 10px;
    height: max(400px, calc(100vh - 182px));
    height: max(400px, calc(100dvh - 182px));
  }

  /* Typography scale on phone */
  #titlu {
    font-size: 20px !important;
  }

  .card > .tabs-container .extra-tab,
  .card > .tabs-container-right .extra-tab,
  .card > .schedule-tabs-right .extra-tab {
    min-height: 26px;
    padding: 3px 6px;
    font-size: 11.5px;
  }

  /* Task row controls */
  .item {
    flex-wrap: nowrap;
    padding: 8px 4px;
  }

  .item .actions {
    gap: 4px;
  }

  .item button {
    width: 32px;
    height: 32px;
    padding: 0;
  }

  /* Bottom action bar: the three buttons share the width instead of overflowing */
  .actions {
    gap: 6px;
  }

  .card:not(.is-schedule-mode) .actions > .left-actions,
  .card:not(.is-schedule-mode) .actions > .center-actions,
  .card:not(.is-schedule-mode) .actions > .right-actions {
    flex: 1 1 0;
    min-width: 0;
  }

  .card:not(.is-schedule-mode) .actions .btn {
    min-width: 0;
    width: 100%;
    padding: 8px 6px;
  }

  /* timetable action bar (grid): narrower side buttons so the middle one fits */
  .card.is-schedule-mode .actions .btn {
    min-width: 64px;
    padding: 6px 8px;
    font-size: 13px;
    white-space: nowrap;
  }

  .settings-modal-backdrop {
    padding: 6px;
  }

  .settings-modal-header {
    padding: 14px 16px;
  }

  .settings-modal-header h2 {
    font-size: 17px;
  }

  /* rows keep their border on phones, so they also keep side padding
     (with 0 the icons and text sat on the border) */
  .settings-toggle-row {
    padding: 10px 12px;
    gap: 10px;
  }

  .toggle-row-info strong {
    font-size: 13.5px;
  }

  .toggle-row-info span {
    font-size: 11.5px;
  }

  /* Schedule layout on small phones */
  .calendar-cards-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .sched-day-card {
    width: 100%;
  }

  /* Footer Links wrap neatly */
  .footer-links-wrapper {
    flex-direction: column;
    align-items: center;
    gap: 10px;
    text-align: center;
  }
}

/* --------------------------------------------------------------------------
   6. Extra Small Screens (<= 360px)
   -------------------------------------------------------------------------- */
@media screen and (max-width: 360px) {
  .top-controls-wrapper {
    gap: 4px;
    padding: 3px 6px;
  }

  .settings-nav-tabs {
    grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  }

  .settings-nav-tab {
    font-size: 11.5px;
    padding: 5px 4px;
  }
}

/* Touch screens have no hover: keep the edit/delete buttons of plan cards visible */
@media (hover: none) and (max-width: 1024px) {
  .sched-plan-tools {
    opacity: 1;
    pointer-events: auto;
  }
}

/* --------------------------------------------------------------------------
   7. Accessibility: Reduced Motion Support (Cross-Browser)
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* --------------------------------------------------------------------------
   8. High DPI / Retina Display & Touch Target Polish
   -------------------------------------------------------------------------- */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
  .toggle-row-info,
  .item .task-text {
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
  }
}
