* { box-sizing: border-box; }

:root { color-scheme: light; --canvas: #fffef9; --bar: #e8edf1; --border: #b1bcc6; --selected: #dbe5ed; --hover: #d1dce7; --panel: #f0f3f5; --control: #fffef9; --text: #24354b; --muted: #526275; --rule: #b8cfe7; --margin: #df9b9f; --paper-pattern: repeating-linear-gradient(to bottom, transparent 0 calc(var(--note-line-height, 28px) - 1px), var(--rule) calc(var(--note-line-height, 28px) - 1px) var(--note-line-height, 28px)); --paper-margin: linear-gradient(to right, transparent 39px, var(--margin) 39px 40px, transparent 40px); --paper-inset: 56px; }
:root[data-theme="legal"] { --canvas: #fff1ab; --bar: #e6d48b; --border: #b3a064; --selected: #ead78e; --hover: #e2cb7b; --panel: #f3e2a3; --control: #fff6cf; --text: #423921; --muted: #645733; --rule: #cabc7b; --margin: #d89182; }
:root[data-theme="dot"] { --canvas: #f8f5ec; --bar: #e6e1d3; --border: #b8af9b; --selected: #dad3c2; --hover: #d0c7b3; --panel: #eee9dd; --control: #faf8f0; --text: #3d3a30; --muted: #66614f; --paper-pattern: radial-gradient(circle at 1px calc(100% - 1px), #b5ad98 1px, transparent 1px); --paper-margin: none; --paper-inset: 24px; }
:root[data-theme="plain"] { --canvas: #fff; --bar: #eee; --border: #bbb; --selected: #ddd; --hover: #d5d5d5; --panel: #f5f5f5; --control: #fff; --text: #292929; --muted: #595959; --paper-pattern: none; --paper-margin: none; --paper-inset: 24px; }
body { margin: 0; padding: 0; color: var(--text); background: var(--canvas); font: 13px 'Segoe UI', Arial, sans-serif; }
[hidden] { display: none !important; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--text); outline-offset: -3px; }
.notepad { height: 100dvh; display: flex; flex-direction: column; background: var(--canvas); }
.tabbar { display: flex; align-items: stretch; flex-shrink: 0; min-width: 0; background: var(--bar); border-bottom: 1px solid var(--border); }
#tabs { display: flex; overflow-x: auto; min-width: 0; }
.tab { display: flex; width: 250px; flex-shrink: 0; position: relative; border-right: 1px solid var(--border); }
.tab button, #add-tab { border: 0; background: transparent; padding: 9px 12px; }
.tab [role=tab] { flex: 1; min-width: 0; text-align: left; cursor: grab; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab:has([aria-selected=true]) { background: var(--selected); }
.tab button:hover, #add-tab:hover { background: var(--hover); }
.tab .edit-tab { display: grid; place-items: center; padding: 9px 14px; }
.tab.dragging { opacity: .45; }
.tab.drop-before { box-shadow: inset 3px 0 var(--text); }
.tab.drop-after { box-shadow: inset -3px 0 var(--text); }
.tab-menu { position: fixed; z-index: 10; width: 155px; padding: 5px; background: var(--panel); border: 1px solid var(--border); box-shadow: 2px 3px 8px #0003; }
.tab-menu button { display: flex; align-items: center; gap: 12px; width: 100%; border: 0; background: transparent; padding: 10px; text-align: left; }
.tab-menu button:hover { background: var(--hover); }
#add-tab { font-size: 20px; flex-shrink: 0; }
:is(#editor, .ql-editor) { display: block; flex: 1; min-height: 0; width: 100%; resize: none; border: 0; border-radius: 0; outline: none; padding: 16px; background: var(--canvas); color: var(--text); caret-color: var(--text); font: 15px/1.5 Consolas, 'Courier New', monospace; tab-size: 4; }
footer { min-height: 27px; background: var(--bar); border-top: 1px solid var(--border); display: flex; align-items: center; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
footer span { padding: 5px 12px; border-left: 1px solid var(--border); }
footer span:first-child { flex: 1; border: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
@media (max-width: 480px) { footer span { padding: 5px 7px; } :is(#editor, .ql-editor) { padding: 12px; } }

.page-header { display: flex; justify-content: flex-end; align-items: center; min-height: 56px; padding: 10px 16px; flex-shrink: 0; background: var(--panel); border-bottom: 1px solid var(--border); }
.page-header button, :is(#login-dialog, #settings-dialog) button { padding: 8px 16px; border: 1px solid var(--border); border-radius: 4px; background: var(--control); }
.page-header button:hover, :is(#login-dialog, #settings-dialog) button:hover { background: var(--selected); }
:is(#login-dialog, #settings-dialog) { width: min(380px, calc(100% - 32px)); max-height: calc(100dvh - 32px); overflow-y: auto; padding: 24px; border: 1px solid var(--border); border-radius: 8px; color: var(--text); background: var(--panel); box-shadow: 0 16px 60px #0006; }
:is(#login-dialog, #settings-dialog)::backdrop { background: #0008; }
.dialog-heading { display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px; }
.dialog-heading h2 { margin: 0; font-size: 20px; }
#close-login { font-size: 22px; line-height: 1; padding: 4px 8px !important; border: 0 !important; background: transparent !important; }
#auth-form { display: grid; gap: 14px; }
#auth-form label { display: grid; gap: 6px; }
#auth-form input { width: 100%; padding: 10px; border: 1px solid var(--border); border-radius: 4px; background: var(--control); }
:is(#login-dialog, #settings-dialog) button:disabled { opacity: .6; cursor: wait; }
#account-session { display: grid; gap: 16px; }
#account-email { overflow-wrap: anywhere; }
#auth-status { margin: 16px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }

/* Keep the app within the visible screen, including when the keyboard opens. */
html, body { width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
body { position: fixed; inset: 0; }
.notepad { position: absolute; top: var(--viewport-top, 0px); width: 100%; height: var(--viewport-height, 100dvh); min-height: 0; overflow: hidden; padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
.page-header { padding-top: max(10px, env(safe-area-inset-top)); }
#tabs { position: relative; flex: 1 1 auto; overflow-y: hidden; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; touch-action: pan-x pinch-zoom; }
:is(#editor, .ql-editor) { flex: 1 1 0; overflow: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch; font-size: 16px; }
footer { flex-shrink: 0; padding-bottom: env(safe-area-inset-bottom); }
footer span:first-child { min-width: 0; overflow-wrap: anywhere; }
#auth-form input { font-size: 16px; }
:is(#login-dialog, #settings-dialog) { max-height: calc(var(--viewport-height, 100dvh) - 32px); overscroll-behavior: contain; }
@media (pointer: coarse) {
  .tab { width: 180px; }
  .tab button, #add-tab, .page-header button, :is(#login-dialog, #settings-dialog) button { min-height: 44px; }
  .tab [role=tab] { cursor: pointer; }
  .tab-menu button { min-height: 44px; }
}
@media (max-width: 480px) {
  footer { flex-wrap: nowrap; }
  footer span { white-space: nowrap; }
  footer span:first-child { white-space: normal; }
  footer #count, footer span:last-child { display: none; }
}

.page-header { gap: 10px; }
#open-settings { margin-right: 0; }
.close-dialog { font-size: 22px; line-height: 1; padding: 4px 8px; border: 0 !important; background: transparent !important; }
.theme-options { margin: 0; padding: 0; border: 0; display: grid; gap: 8px; }
.theme-options legend { margin-bottom: 12px; font-size: 14px; }
.theme-option { display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.theme-option:has(input:checked) { background: var(--selected); }
.theme-option input { margin: 0; accent-color: var(--text); }
.theme-swatch { width: 24px; height: 24px; flex-shrink: 0; border-radius: 50%; border: 1px solid var(--border); }
#theme-status { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 16px 0 0; }

.header-logo { display: block; width: clamp(100px, 30vw, 180px); height: auto; max-height: 60px; object-fit: contain; margin-right: auto; flex-shrink: 1; min-width: 0; }
.page-header > button { flex-shrink: 0; }
@media (max-width: 380px) {
  .page-header { gap: 8px; padding-left: 10px; padding-right: 10px; }
  .page-header button { padding-left: 10px; padding-right: 10px; }
}

/* Paper spacing follows the selected base text size. */
:is(#editor, .ql-editor) { padding: 20px 24px 28px var(--paper-inset); line-height: 28px; background-color: var(--canvas); background-image: var(--paper-margin), var(--paper-pattern); background-position: 0 20px; background-attachment: local; }
:root[data-theme="dot"] :is(#editor, .ql-editor) { background-size: auto, calc(var(--note-line-height) / 2) var(--note-line-height); }
.theme-option strong { display: block; font-size: 13px; font-weight: 600; }
.theme-option small { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; }
.theme-swatch { width: 42px; height: 48px; border-radius: 3px; box-shadow: 2px 2px 0 #0001; }
.swatch-ruled { background: linear-gradient(to right, transparent 8px, #df9b9f 8px 9px, transparent 9px), repeating-linear-gradient(to bottom, #fffef9 0 8px, #b8cfe7 8px 9px); }
.swatch-legal { background: linear-gradient(to right, transparent 8px, #d89182 8px 9px, transparent 9px), repeating-linear-gradient(to bottom, #fff1ab 0 8px, #cabc7b 8px 9px); }
.swatch-dot { background: radial-gradient(circle, #b5ad98 1px, transparent 1px) 0 0 / 8px 8px, #f8f5ec; }
.swatch-plain { background: #fff; }

:root[data-theme="darkpad"] { color-scheme: dark; --canvas: #080f1c; --bar: #101e33; --border: #30496c; --selected: #1b3455; --hover: #254469; --panel: #0d192b; --control: #162740; --text: #e1edff; --muted: #a6bddb; --rule: #1d3554; --margin: #3d6da3; }
.swatch-darkpad { background: linear-gradient(to right, transparent 8px, #3d6da3 8px 9px, transparent 9px), repeating-linear-gradient(to bottom, #080f1c 0 8px, #1d3554 8px 9px); }

:root { --note-font: 'SFMono-Regular', Consolas, 'Liberation Mono', 'Courier New', monospace; }
:root[data-font="sans"] { --note-font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif; }
:root[data-font="serif"] { --note-font: Georgia, 'Times New Roman', serif; }
:root[data-font="rounded"] { --note-font: ui-rounded, 'SF Pro Rounded', 'Arial Rounded MT Bold', 'Trebuchet MS', sans-serif; }
:is(#editor, .ql-editor), #font-preview { font-family: var(--note-font); }
.font-setting { margin-top: 22px; padding-top: 20px; border-top: 1px solid var(--border); }
.font-setting > label { display: block; margin-bottom: 8px; font-size: 14px; }
#font-preview { margin: 12px 0; padding: 12px; font-size: 16px; line-height: 28px; color: var(--text); background: var(--canvas); border: 1px solid var(--border); border-radius: 4px; }
#font-status { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }

:root { --note-size: 16px; --note-line-height: 28px; }
:root[data-text-size="medium"] { --note-size: 18px; --note-line-height: 32px; }
:root[data-text-size="large"] { --note-size: 22px; --note-line-height: 40px; }
:is(#editor, .ql-editor), #font-preview { font-size: var(--note-size); line-height: var(--note-line-height); }
#note-size { width: 100%; min-height: 44px; padding: 10px; font: inherit; font-size: 16px; color: var(--text); background: var(--control); border: 1px solid var(--border); border-radius: 4px; }
#note-size:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }
label[for="note-size"] { margin-top: 16px; }
#size-status { margin: 8px 0 0; color: var(--muted); font-size: 12px; line-height: 1.5; }

.rich-host { flex: 1 1 0; min-height: 0; width: 100%; overflow: hidden; font-family: var(--note-font); }
.ql-container { border: 0; font-size: var(--note-size); }
.ql-editor { height: 100%; white-space: pre-wrap; overflow-wrap: anywhere; color: var(--text); }
.ql-editor p, .ql-editor h1, .ql-editor h2, .ql-editor h3 { padding: 0; margin: 0; font-family: inherit; font-weight: 400; }
.ql-editor h1 { font-size: 2em; line-height: calc(var(--note-line-height) * 2); font-weight: 700; }
.ql-editor h2 { font-size: 1.5em; line-height: calc(var(--note-line-height) * 2); font-weight: 700; }
.ql-editor h3 { font-size: 1.2em; line-height: var(--note-line-height); font-weight: 700; }
.heading-toolbar { display: flex; align-items: center; gap: 6px; padding: 4px 12px; flex-shrink: 0; background: var(--bar); border-bottom: 1px solid var(--border); }
.heading-toolbar > span { margin-right: 6px; color: var(--muted); font-size: 12px; }
.heading-toolbar button { min-width: 44px; min-height: 40px; padding: 6px 10px; background: var(--control); color: var(--text); border: 1px solid var(--border); border-radius: 4px; }
.heading-toolbar button[aria-pressed="true"] { background: var(--selected); box-shadow: inset 0 -2px var(--text); }
.heading-toolbar button:disabled { opacity: .5; }
@media (pointer: coarse) { .heading-toolbar button { min-height: 44px; } }

/* Each paragraph owns its paper row so larger and wrapped headings fit too. */
.ql-editor > p, .ql-editor > h1, .ql-editor > h2, .ql-editor > h3 {
  --paper-step: var(--note-line-height);
  background-color: var(--canvas);
  background-image: repeating-linear-gradient(to bottom, transparent 0 calc(var(--paper-step) - 1px), var(--rule) calc(var(--paper-step) - 1px) var(--paper-step));
  background-position: 0 0;
  line-height: var(--paper-step);
}
.ql-editor > h1, .ql-editor > h2 { --paper-step: calc(var(--note-line-height) * 2); }
:root[data-theme="dot"] .ql-editor > :is(p, h1, h2, h3) {
  background-image: radial-gradient(circle at 1px calc(100% - 1px), #b5ad98 1px, transparent 1px);
  background-size: calc(var(--paper-step) / 2) var(--paper-step);
  background-position: calc(-1 * var(--paper-inset)) 0;
}
:root[data-theme="plain"] .ql-editor > :is(p, h1, h2, h3) { background-image: none; }

#open-add { margin-right: 6px; font-weight: 600; }
.add-menu { position: fixed; z-index: 20; width: 180px; padding: 5px; background: var(--panel); border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 6px 20px #0003; }
.add-menu button { display: block; width: 100%; min-height: 44px; border: 0; background: transparent; text-align: left; }
.add-menu button:hover, .add-menu button:focus-visible { background: var(--selected); }
@media (max-width: 480px) {
  .heading-toolbar { gap: 4px; padding: 4px 8px; }
  .heading-toolbar > span { display: none; }
  #open-add { margin-right: auto; }
}

.ql-editor hr { border: 0; margin: 0; height: var(--note-line-height); background-color: var(--canvas); background-image: linear-gradient(to bottom, transparent calc(50% - 1px), var(--border) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)); }
.ql-editor li { line-height: var(--note-line-height); }
.ql-editor li[data-list="checked"] { color: var(--muted); text-decoration: line-through; }
.ql-editor li[data-list] > .ql-ui::before { color: var(--text); cursor: pointer; }
.ql-editor li > .ql-ui:focus-visible { outline: 2px solid var(--text); outline-offset: 2px; }

.header-add { display: flex; align-items: center; flex-shrink: 0; }
.header-add #open-add { margin: 0; }

.header-add #open-add { font: inherit; font-weight: 400; }
.font-options { padding: 0; margin: 0; border: 0; display: grid; gap: 8px; }
.font-options legend { margin-bottom: 8px; font-size: 14px; }
.font-choice { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 4px; background: var(--control); cursor: pointer; }
.font-choice:has(input:checked) { background: var(--selected); }
.font-choice input { margin: 0; accent-color: var(--text); }
.font-choice span { font-size: 16px; }
.font-mono { font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', 'Courier New', monospace; }
.font-sans { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif; }
.font-serif { font-family: Georgia, 'Times New Roman', serif; }
.font-rounded { font-family: ui-rounded, 'SF Pro Rounded', 'Arial Rounded MT Bold', 'Trebuchet MS', sans-serif; }
