:root {
  color-scheme: light dark;
  --bg: var(--tg-theme-bg-color, #eef2f6);
  --text: var(--tg-theme-text-color, #17212b);
  --hint: var(--tg-theme-hint-color, #7b8794);
  --surface: var(--tg-theme-secondary-bg-color, #ffffff);
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --border: rgba(107, 123, 139, 0.22);
  --border-strong: rgba(107, 123, 139, 0.38);
  --shadow: 0 10px 30px rgba(31, 45, 61, 0.08);
  --danger: #dc4c4c;
  --paper-line: rgba(91, 113, 135, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body {
  min-height: 100vh;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.hidden {
  display: none !important;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 72px;
  padding: calc(12px + env(safe-area-inset-top)) 18px 12px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  backdrop-filter: blur(18px);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 11px;
}

.brand-icon {
  display: grid;
  place-items: center;
  flex: 0 0 42px;
  height: 42px;
  border-radius: 13px;
  background: var(--button);
  color: var(--button-text);
  box-shadow: 0 8px 20px color-mix(in srgb, var(--button) 28%, transparent);
  font-size: 14px;
  font-weight: 850;
}

.brand-copy {
  min-width: 0;
}

h1 {
  margin: 0;
  font-size: 19px;
  line-height: 1.15;
}

#userLine {
  overflow: hidden;
  max-width: 440px;
  margin: 4px 0 0;
  color: var(--hint);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 9px;
}

.search-box {
  display: flex;
  align-items: center;
  width: min(420px, 34vw);
  min-width: 230px;
  height: 42px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  color: var(--hint);
}

.search-box:focus-within {
  border-color: color-mix(in srgb, var(--button) 58%, var(--border));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--button) 12%, transparent);
}

.search-icon {
  font-size: 18px;
}

.search-box input {
  width: 100%;
  min-width: 0;
  margin-left: 7px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
}

.search-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 24px;
  padding: 0 7px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--button) 14%, transparent);
  color: var(--button);
  font-size: 11px;
  font-weight: 800;
}

.primary {
  border: 0;
  border-radius: 11px;
  background: var(--button);
  color: var(--button-text);
  font-weight: 760;
}

.add-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-height: 42px;
  padding: 0 14px;
  white-space: nowrap;
}

.page {
  width: 100%;
  padding: 14px 14px calc(28px + env(safe-area-inset-bottom));
}

.notice {
  max-width: 1200px;
  margin: 0 auto 12px;
  padding: 12px 14px;
  border: 1px solid rgba(220, 76, 76, 0.3);
  border-radius: 12px;
  background: rgba(220, 76, 76, 0.1);
  color: #a82d2d;
  white-space: pre-wrap;
}

.notebooks {
  --default-notebook-width: calc((100% - 28px) / 3);
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  align-content: flex-start;
  gap: 14px;
  width: 100%;
}

.notebook {
  position: relative;
  flex: 0 1 var(--notebook-width, var(--default-notebook-width));
  width: var(--notebook-width, var(--default-notebook-width));
  min-width: min(100%, 320px);
  max-width: 100%;
  overflow: visible;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
  transition: border-color 0.18s ease, box-shadow 0.18s ease, transform 0.18s ease;
}

.notebook:hover {
  border-color: var(--border-strong);
  box-shadow: 0 13px 34px rgba(31, 45, 61, 0.11);
  transform: translateY(-1px);
}

.notebook.search-current {
  border-color: var(--button);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--button) 14%, transparent), var(--shadow);
}

.notebook.dirty .dock-dot {
  box-shadow: 0 0 0 5px color-mix(in srgb, var(--button) 18%, transparent);
  animation: dotPulse 1.8s ease-in-out infinite;
}

.notebook-header {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 50px;
  padding: 9px 13px 8px;
  border-bottom: 1px solid var(--border);
}

.note-title {
  flex: 1;
  min-width: 0;
  padding: 7px 8px;
  border: 1px solid transparent;
  border-radius: 9px;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  font-weight: 800;
}

.note-title:hover,
.note-title:focus {
  border-color: var(--border);
  background: var(--bg);
}

.save-state {
  overflow: hidden;
  max-width: 125px;
  color: var(--hint);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.notebook-stage {
  position: relative;
  height: 340px;
  min-height: 260px;
  overflow: hidden;
  border-radius: 0 0 16px 16px;
  outline: 0;
  background:
    linear-gradient(var(--paper-line) 1px, transparent 1px) 0 42px / 100% 28px,
    var(--surface);
}

.notebook-stage:focus-within {
  box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--button) 15%, transparent);
}

.note-text {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 18px 76px;
  outline: 0;
  color: var(--text);
  font-size: 14px;
  line-height: 28px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.note-text:empty::before {
  content: attr(data-placeholder);
  color: color-mix(in srgb, var(--hint) 80%, transparent);
  pointer-events: none;
}

.image-layer,
.drawing-layer {
  position: absolute;
  inset: 0;
}

.image-layer {
  z-index: 3;
  pointer-events: none;
}

.note-image {
  position: absolute;
  display: block;
  min-width: 60px;
  min-height: 40px;
  overflow: visible;
  border: 1px solid transparent;
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(24, 36, 48, 0.14);
  pointer-events: auto;
  touch-action: none;
  user-select: none;
}

.note-image.selected {
  border-color: var(--button);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--button) 18%, transparent), 0 8px 24px rgba(24, 36, 48, 0.14);
}

.note-image img {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 9px;
  object-fit: contain;
  pointer-events: none;
}

.image-delete,
.image-resize {
  position: absolute;
  display: none;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 999px;
  background: rgba(24, 34, 45, 0.76);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  backdrop-filter: blur(8px);
}

.note-image.selected .image-delete,
.note-image.selected .image-resize {
  display: flex;
}

.image-delete {
  top: -10px;
  right: -10px;
}

.image-resize {
  right: -10px;
  bottom: -10px;
  cursor: nwse-resize;
}

.drawing-layer {
  z-index: 5;
  width: 100%;
  height: 100%;
  pointer-events: none;
  touch-action: none;
}

.notebook.draw-mode .drawing-layer {
  pointer-events: auto;
  cursor: crosshair;
}

.notebook.draw-mode .note-text,
.notebook.draw-mode .note-image {
  user-select: none;
}

.drawing-hint {
  position: absolute;
  z-index: 10;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  padding: 7px 10px;
  border-radius: 999px;
  background: rgba(31, 120, 209, 0.88);
  color: white;
  font-size: 11px;
  white-space: nowrap;
  pointer-events: none;
  backdrop-filter: blur(8px);
}

.corner-dock {
  position: absolute;
  z-index: 20;
  right: 12px;
  bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 18px;
  min-height: 18px;
}

.corner-dock::before {
  content: "";
  position: absolute;
  right: -12px;
  bottom: -12px;
  width: 118px;
  height: 78px;
}

.dock-dot {
  position: relative;
  z-index: 3;
  display: block;
  width: 16px;
  height: 16px;
  border: 2px solid color-mix(in srgb, var(--surface) 72%, transparent);
  border-radius: 5px;
  background: var(--button);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--button) 13%, transparent);
  cursor: nwse-resize;
  touch-action: none;
}

.dock-dot::after {
  content: "";
  position: absolute;
  right: 2px;
  bottom: 2px;
  width: 7px;
  height: 7px;
  border-right: 2px solid rgba(255, 255, 255, 0.95);
  border-bottom: 2px solid rgba(255, 255, 255, 0.95);
  border-radius: 1px;
}

.notebook.resizing {
  user-select: none;
  transition: none;
}

.notebook.resizing .dock-tools {
  opacity: 0;
  pointer-events: none;
}

.dock-tools {
  position: absolute;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 24px 6px 7px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  box-shadow: 0 9px 28px rgba(25, 37, 48, 0.16);
  opacity: 0;
  pointer-events: none;
  transform: translate(5px, 5px) scale(0.92);
  transform-origin: right bottom;
  transition: opacity 0.16s ease, transform 0.16s ease;
  backdrop-filter: blur(14px);
}

.corner-dock:hover .dock-tools,
.corner-dock:focus-within .dock-tools,
.notebook.draw-mode .dock-tools {
  opacity: 1;
  pointer-events: auto;
  transform: translate(0, 0) scale(1);
}

.dock-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-width: 32px;
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  color: color-mix(in srgb, var(--text) 64%, transparent);
  font-weight: 800;
  opacity: 0.68;
  backdrop-filter: blur(10px);
}

.dock-button:hover,
.dock-button.active {
  border-color: color-mix(in srgb, var(--button) 48%, var(--border));
  background: color-mix(in srgb, var(--button) 13%, var(--surface));
  color: var(--button);
  opacity: 1;
}

.dock-button.danger:hover {
  border-color: color-mix(in srgb, var(--danger) 45%, var(--border));
  background: color-mix(in srgb, var(--danger) 10%, var(--surface));
  color: var(--danger);
}

.save-button {
  min-width: 102px;
  border-color: transparent;
  background: var(--button);
  color: var(--button-text);
  opacity: 1;
}

.save-button:hover {
  background: color-mix(in srgb, var(--button) 88%, #000);
  color: var(--button-text);
}

.empty {
  display: flex;
  min-height: 65vh;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--hint);
  text-align: center;
}

.empty-icon {
  font-size: 48px;
}

.empty strong {
  color: var(--text);
  font-size: 18px;
}

.empty button {
  margin-top: 8px;
  padding: 11px 15px;
}

button:disabled,
input:disabled,
[contenteditable="false"] {
  opacity: 0.55;
  cursor: wait;
}

@keyframes dotPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.18); }
}

@media (max-width: 1150px) {
  .notebooks {
    --default-notebook-width: calc((100% - 14px) / 2);
  }
}

@media (max-width: 820px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    gap: 10px;
    padding-left: 12px;
    padding-right: 12px;
  }

  .toolbar {
    width: 100%;
  }

  .search-box {
    flex: 1;
    width: auto;
    min-width: 0;
  }

  .page {
    padding-left: 9px;
    padding-right: 9px;
  }

  .notebooks {
    --default-notebook-width: 100%;
  }

  .notebook {
    min-width: 0;
  }

  .save-state {
    max-width: 95px;
  }
}

@media (max-width: 470px) {
  .brand-icon {
    display: none;
  }

  h1 {
    font-size: 17px;
  }

  .add-button {
    width: 42px;
    padding: 0;
  }

  .add-button span:last-child,
  .refresh-label {
    display: none;
  }

  .refresh-button {
    width: 42px;
    padding: 0;
  }

  .save-label {
    display: none;
  }

  .save-button {
    min-width: 32px;
  }

  .dock-tools {
    gap: 4px;
    padding-left: 5px;
  }
}


.refresh-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 13px;
  border: 1px solid color-mix(in srgb, var(--button) 32%, var(--border));
  border-radius: 11px;
  background: color-mix(in srgb, var(--button) 9%, var(--surface));
  color: var(--button);
  font-weight: 760;
  white-space: nowrap;
}

.refresh-button:hover {
  border-color: color-mix(in srgb, var(--button) 62%, var(--border));
  background: color-mix(in srgb, var(--button) 15%, var(--surface));
}

.refresh-button.loading .refresh-icon {
  animation: refreshSpin 0.85s linear infinite;
}

@keyframes refreshSpin {
  to { transform: rotate(360deg); }
}

.logout-button {
  border: 1px solid rgba(147, 170, 196, 0.28);
  background: transparent;
  color: var(--muted, #9db0c5);
  border-radius: 12px;
  min-height: 40px;
  padding: 0 14px;
  cursor: pointer;
}

.logout-button:hover {
  color: var(--text, #f6f8fb);
  border-color: rgba(87, 162, 236, 0.65);
}

.login-screen {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: grid;
  place-items: center;
  padding: 20px;
  background:
    radial-gradient(circle at 50% 20%, rgba(47, 126, 204, 0.22), transparent 42%),
    #111a24;
}

.login-card {
  width: min(420px, 100%);
  display: grid;
  gap: 16px;
  padding: 30px;
  border: 1px solid rgba(132, 163, 194, 0.25);
  border-radius: 22px;
  background: #1b2836;
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.42);
}

.login-logo {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: #3e8ed8;
  color: white;
  font-weight: 800;
  font-size: 20px;
}

.login-card h2,
.login-card p {
  margin: 0;
}

.login-card p {
  color: #9db0c5;
}

.login-card label {
  display: grid;
  gap: 7px;
  color: #c9d5e2;
  font-size: 14px;
}

.login-card input {
  width: 100%;
  min-height: 46px;
  border: 1px solid rgba(132, 163, 194, 0.3);
  border-radius: 12px;
  padding: 0 13px;
  background: #121d28;
  color: #f6f8fb;
  outline: none;
}

.login-card input:focus {
  border-color: #4a9be8;
  box-shadow: 0 0 0 3px rgba(74, 155, 232, 0.14);
}

.login-error {
  padding: 11px 13px;
  border-radius: 11px;
  color: #ffb7b7;
  background: rgba(194, 55, 55, 0.17);
  border: 1px solid rgba(245, 93, 93, 0.35);
}

.login-card .primary {
  min-height: 46px;
}
