/*
 * Note mobile adaptive V1.
 * PC and tablet layouts remain unchanged. Mobile (<=640px) gets a dedicated
 * top bar, step drawer, bottom primary action, and symbol bottom sheet.
 */

.note-mobile-topbar,
.note-mobile-backdrop,
.note-mobile-step-sheet,
.note-mobile-more-menu,
.note-mobile-primary-bar {
  display: none;
}

@media (max-width: 640px) {
  :root {
    --note-mobile-header-height: 64px;
    --note-mobile-primary-height: 72px;
    --note-mobile-viewport-height: 100dvh;
  }

  html {
    min-width: 320px;
    background: var(--surface-canvas, #fafafa);
    overscroll-behavior-y: none;
  }

  body {
    min-width: 320px;
    padding-top: calc(var(--note-mobile-header-height) + env(safe-area-inset-top));
    padding-bottom: env(safe-area-inset-bottom);
    overflow-x: hidden;
    background: var(--surface-canvas, #fafafa);
    -webkit-text-size-adjust: 100%;
  }

  body.note-mobile-overlay-open {
    overflow: hidden;
    touch-action: none;
  }

  .app-shell {
    width: 100%;
    min-height: calc(var(--note-mobile-viewport-height) - var(--note-mobile-header-height));
    margin: 0;
    display: block;
    overflow: visible;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    background: var(--surface-canvas, #fafafa);
  }

  .sidebar {
    display: none !important;
  }

  .workspace {
    width: 100%;
    min-width: 0;
    padding: 16px 14px calc(var(--note-mobile-primary-height) + 24px + env(safe-area-inset-bottom));
    background: var(--surface-canvas, #fafafa);
  }

  .topbar,
  .stepper {
    display: none !important;
  }

  .action-status {
    margin: 0 0 12px;
    padding: 9px 12px;
    border: 1px solid var(--border-subtle, #ececec);
    border-radius: 10px;
    background: #fff;
  }

  .view {
    min-height: 0;
  }

  .view-grid,
  .editor-layout,
  .field-grid,
  .field-grid.three,
  .choice-grid,
  .metric-grid,
  .account-picker {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  .view-grid > aside,
  .editor-layout > aside {
    order: 2;
  }

  .stack,
  .view-grid,
  .editor-layout {
    gap: 12px;
  }

  .card {
    width: 100%;
    min-width: 0;
    border-radius: 12px;
  }

  .card-header {
    padding: 16px 16px 0;
    display: block;
  }

  .card-header > .tag,
  .card-header > .button-row {
    margin-top: 10px;
  }

  .card-header h2 {
    font-size: 18px;
    line-height: 28px;
  }

  .card-header h3 {
    font-size: 15px;
    line-height: 24px;
  }

  .card-body {
    padding: 16px;
  }

  .empty-state {
    padding: 32px 18px;
  }

  .metric,
  .choice-card,
  .topic-card,
  .saved-task-row,
  .account-choice {
    border-radius: 10px;
  }

  .topic-card {
    grid-template-columns: 32px minmax(0, 1fr);
    gap: 10px;
    padding: 14px;
  }

  .topic-card > .button,
  .topic-card > :last-child {
    grid-column: 1 / -1;
  }

  .saved-task-row {
    padding: 14px;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 12px;
  }

  .saved-task-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .saved-task-actions .button-danger {
    grid-column: 1 / -1;
  }

  .button,
  .plain-text-tool,
  .symbol-picker-tab-v2,
  .symbol-picker-pager-v5 button {
    min-height: 44px;
    touch-action: manipulation;
  }

  .button-row {
    width: 100%;
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 8px;
  }

  .button-row .button,
  .button-row a.button {
    width: 100%;
  }

  .button-row.end {
    justify-content: stretch;
  }

  .field input:not([type="checkbox"]):not([type="radio"]),
  .field textarea,
  .field select,
  .title-option input[type="text"],
  .symbol-picker-search-v5,
  .symbol-picker-category-v5 {
    font-size: 16px;
  }

  .field input:not([type="checkbox"]):not([type="radio"]),
  .field select {
    min-height: 48px;
  }

  .title-option {
    min-width: 0;
    padding: 10px;
    gap: 8px;
  }

  .title-option input[type="text"] {
    min-width: 0;
  }

  .plain-text-toolbar {
    padding: 8px;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .plain-text-toolbar-label {
    grid-column: 1 / -1;
  }

  .plain-text-tool {
    width: 100%;
    padding: 0 6px;
  }

  .editor textarea#draft-body {
    min-height: clamp(360px, calc(var(--note-mobile-viewport-height) - 310px), 560px);
    resize: none;
  }

  /* Dedicated mobile top bar, aligned with the assistant/diagnosis products. */
  .note-mobile-topbar {
    position: fixed;
    inset: 0 0 auto;
    z-index: 300;
    min-height: calc(var(--note-mobile-header-height) + env(safe-area-inset-top));
    padding-top: env(safe-area-inset-top);
    display: block;
    border-bottom: 1px solid var(--border-subtle, #ececec);
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 1px 0 rgba(38, 38, 38, 0.02);
    backdrop-filter: saturate(180%) blur(12px);
  }

  .note-mobile-topbar-inner {
    width: min(100%, 480px);
    min-height: var(--note-mobile-header-height);
    margin: 0 auto;
    padding: 8px 12px;
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto 44px;
    align-items: center;
    gap: 8px;
  }

  .note-mobile-title-wrap {
    min-width: 0;
  }

  .note-mobile-kicker,
  .note-mobile-account {
    display: block;
    overflow: hidden;
    color: #999;
    font-size: 10px;
    line-height: 15px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .note-mobile-route-title {
    display: block;
    overflow: hidden;
    color: var(--text-primary, #262626);
    font-size: 16px;
    font-weight: 600;
    line-height: 23px;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .note-mobile-progress,
  .note-mobile-more-toggle {
    height: 40px;
    border: 1px solid var(--border-subtle, #ececec);
    color: var(--text-primary, #262626);
    background: #fff;
    font-size: 12px;
    font-weight: 600;
  }

  .note-mobile-progress {
    padding: 0 11px;
    border-radius: 999px;
    color: var(--brand-default, #d62d46);
    background: var(--brand-subtle, #fff0f3);
    white-space: nowrap;
  }

  .note-mobile-more-toggle {
    width: 40px;
    padding: 0;
    border-radius: 50%;
    font-size: 20px;
    line-height: 1;
  }

  .note-mobile-backdrop {
    position: fixed;
    inset: 0;
    z-index: 310;
    display: block;
    visibility: hidden;
    opacity: 0;
    background: rgba(15, 18, 22, 0.38);
    transition: opacity 0.18s ease, visibility 0.18s ease;
  }

  body.note-mobile-overlay-open .note-mobile-backdrop {
    visibility: visible;
    opacity: 1;
  }

  /* 01—09 becomes a touch-friendly bottom drawer on phones. */
  .note-mobile-step-sheet {
    position: fixed;
    inset: auto 0 0;
    z-index: 330;
    max-height: min(80dvh, 720px);
    padding: 0 14px calc(16px + env(safe-area-inset-bottom));
    display: block;
    overflow: hidden;
    border-radius: 20px 20px 0 0;
    background: #fff;
    box-shadow: 0 -18px 48px rgba(15, 18, 22, 0.18);
    transform: translateY(105%);
    transition: transform 0.22s ease;
  }

  body.note-mobile-steps-open .note-mobile-step-sheet {
    transform: translateY(0);
  }

  .note-mobile-sheet-handle {
    width: 38px;
    height: 4px;
    margin: 9px auto 4px;
    border-radius: 99px;
    background: #d7dbe0;
  }

  .note-mobile-sheet-head {
    padding: 10px 2px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 1px solid var(--border-subtle, #ececec);
  }

  .note-mobile-sheet-head strong {
    font-size: 16px;
    font-weight: 600;
  }

  .note-mobile-sheet-close {
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 50%;
    color: #777;
    background: var(--surface-subtle, #f5f5f5);
    font-size: 21px;
  }

  .note-mobile-step-list {
    max-height: calc(min(80dvh, 720px) - 84px - env(safe-area-inset-bottom));
    padding: 10px 0 0;
    display: grid;
    gap: 6px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .note-mobile-step-option {
    width: 100%;
    min-height: 52px;
    padding: 7px 10px;
    display: grid;
    grid-template-columns: 34px minmax(0, 1fr) auto;
    align-items: center;
    gap: 10px;
    border: 1px solid var(--border-subtle, #ececec);
    border-radius: 10px;
    color: var(--text-primary, #262626);
    background: #fff;
    text-align: left;
  }

  .note-mobile-step-option[aria-current="step"] {
    border-color: rgba(214, 45, 70, 0.36);
    background: var(--brand-subtle, #fff0f3);
  }

  .note-mobile-step-option:disabled {
    color: #aaa;
    background: #f7f7f7;
    opacity: 0.75;
  }

  .note-mobile-step-index {
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    border-radius: 9px;
    color: #777;
    background: var(--surface-subtle, #f5f5f5);
    font-size: 11px;
    font-weight: 600;
  }

  .note-mobile-step-option[aria-current="step"] .note-mobile-step-index {
    color: #fff;
    background: var(--brand-default, #d62d46);
  }

  .note-mobile-step-label {
    min-width: 0;
    overflow: hidden;
    font-size: 14px;
    font-weight: 600;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .note-mobile-step-state {
    color: #999;
    font-size: 10px;
    white-space: nowrap;
  }

  .note-mobile-step-state.done {
    color: #2f8f67;
  }

  .note-mobile-step-state.current {
    color: var(--brand-default, #d62d46);
  }

  .note-mobile-more-menu {
    position: fixed;
    top: calc(var(--note-mobile-header-height) + env(safe-area-inset-top) - 4px);
    right: 10px;
    z-index: 340;
    width: min(220px, calc(100vw - 20px));
    padding: 8px;
    display: grid;
    gap: 6px;
    visibility: hidden;
    opacity: 0;
    border: 1px solid var(--border-subtle, #ececec);
    border-radius: 12px;
    background: #fff;
    box-shadow: 0 16px 40px rgba(15, 18, 22, 0.16);
    transform: translateY(-8px);
    transition: opacity 0.16s ease, transform 0.16s ease, visibility 0.16s ease;
  }

  body.note-mobile-more-open .note-mobile-more-menu {
    visibility: visible;
    opacity: 1;
    transform: none;
  }

  .note-mobile-more-action {
    min-height: 44px;
    padding: 0 12px;
    border: 0;
    border-radius: 9px;
    color: var(--text-primary, #262626);
    background: #fff;
    text-align: left;
    font-size: 13px;
  }

  .note-mobile-more-action:active {
    background: var(--brand-subtle, #fff0f3);
  }

  /* One primary action remains reachable at the bottom of long mobile pages. */
  .note-mobile-primary-bar {
    position: fixed;
    inset: auto 0 0;
    z-index: 250;
    padding: 9px 12px calc(9px + env(safe-area-inset-bottom));
    display: none;
    border-top: 1px solid var(--border-subtle, #ececec);
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 -8px 24px rgba(15, 18, 22, 0.08);
    backdrop-filter: saturate(180%) blur(12px);
  }

  .note-mobile-primary-bar[data-visible="true"] {
    display: block;
  }

  .note-mobile-primary-inner {
    width: min(100%, 480px);
    margin: 0 auto;
  }

  .note-mobile-primary-button {
    width: 100%;
    min-height: 48px;
    border: 0;
    border-radius: 10px;
    color: #fff;
    background: var(--brand-default, #d62d46);
    font-size: 15px;
    font-weight: 600;
  }

  .mobile-primary-source {
    display: none !important;
  }

  body.note-mobile-keyboard-open .note-mobile-primary-bar,
  body.note-mobile-keyboard-open .note-module-hub,
  body.note-mobile-symbol-open .note-mobile-primary-bar,
  body.note-mobile-symbol-open .note-module-hub,
  body.note-mobile-overlay-open.note-mobile-steps-open .note-module-hub,
  body.note-mobile-overlay-open.note-mobile-more-open .note-module-hub {
    display: none !important;
  }

  .note-module-hub {
    right: 14px;
    bottom: calc(var(--note-mobile-primary-height) + 14px + env(safe-area-inset-bottom));
  }

  .note-module-hub-toggle {
    width: 50px;
    height: 50px;
  }

  /* The picker becomes a genuine phone bottom sheet. */
  .symbol-picker-v2.symbol-picker-source-v5,
  .symbol-picker-source-v5 {
    inset: auto 0 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: min(82dvh, calc(var(--note-mobile-viewport-height) - 12px)) !important;
    padding-bottom: env(safe-area-inset-bottom);
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -18px 52px rgba(15, 18, 22, 0.22);
  }

  .symbol-picker-head-v2 {
    min-height: 58px;
    padding: 12px 14px;
  }

  .symbol-picker-tabs-v2 {
    padding: 8px 12px 0;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .symbol-picker-tab-v2 {
    width: 100%;
    padding: 0 6px;
  }

  .symbol-picker-filter-v5 {
    grid-template-columns: minmax(0, 1fr);
    gap: 7px;
  }

  .symbol-picker-meta-v5 {
    display: block;
  }

  .symbol-picker-source-v5-label {
    display: block;
    margin-top: 2px;
  }

  .symbol-picker-body-v2 {
    max-height: calc(min(82dvh, var(--note-mobile-viewport-height)) - 292px) !important;
    min-height: 150px;
    padding: 10px;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .symbol-grid-v5.emoji-v5 {
    grid-template-columns: repeat(auto-fill, minmax(48px, 1fr));
    gap: 6px;
  }

  .symbol-grid-v5.symbols-v5 {
    grid-template-columns: repeat(auto-fill, minmax(54px, 1fr));
    gap: 6px;
  }

  .symbol-grid-v5.kaomoji-v5 {
    grid-template-columns: minmax(0, 1fr);
  }

  .emoji-item-v5 {
    height: 50px;
    min-height: 50px;
  }

  .text-symbol-item-v5 {
    min-height: 46px;
  }

  .text-symbol-item-v5.is-medium-v5,
  .text-symbol-item-v5.is-long-v5,
  .kaomoji-item-v5.is-long-v5 {
    grid-column: auto;
  }

  .symbol-picker-pager-v5 {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    grid-template-columns: 88px minmax(0, 1fr) 88px;
  }

  .preview-dialog {
    width: 100%;
    max-width: 100%;
    max-height: calc(var(--note-mobile-viewport-height) - 12px);
    margin: auto 0 0;
    border: 0;
    border-radius: 20px 20px 0 0;
  }

  .preview-stage {
    grid-template-columns: minmax(0, 1fr);
  }
}

@media (max-width: 370px) {
  .note-mobile-topbar-inner {
    grid-template-columns: minmax(0, 1fr) auto 40px;
    gap: 5px;
    padding-right: 8px;
    padding-left: 8px;
  }

  .note-mobile-progress {
    padding: 0 8px;
    font-size: 11px;
  }

  .workspace {
    padding-right: 10px;
    padding-left: 10px;
  }

  .saved-task-actions {
    grid-template-columns: minmax(0, 1fr);
  }

  .saved-task-actions .button-danger {
    grid-column: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  .note-mobile-backdrop,
  .note-mobile-step-sheet,
  .note-mobile-more-menu {
    transition: none;
  }
}
