/* ===== Reset ===== */
*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* ===== CSS Variables ===== */
:root {
    --bg: #ffffff;
    --bg2: #f5f5f4;
    --bg3: #eeece8;
    --text: #1c1c1a;
    --text2: #6b6b67;
    --text3: #a8a8a4;
    --border: rgba(0, 0, 0, 0.10);
    --border2: rgba(0, 0, 0, 0.18);
    --blue: #185FA5;
    --blue-bg: #E6F1FB;
    --blue-border: #B5D4F4;
    --green: #0F6E56;
    --green-bg: #E1F5EE;
    --green-border: #9FE1CB;
    --grp: #5BAEDC;
    --grp-bg: rgba(91, 174, 220, 0.07);
    --radius-md: 8px;
    --radius-lg: 12px;
    --sidebar-w: 240px;
    --header-h: 56px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --block-font-size: 13.5px;
    --group-font-size: 22px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1c1c1a;
        --bg2: #272725;
        --bg3: #303030;
        --text: #f0ede8;
        --text2: #a0a09c;
        --text3: #686864;
        --border: rgba(255, 255, 255, 0.10);
        --border2: rgba(255, 255, 255, 0.18);
        --blue: #5b9fd4;
        --blue-bg: rgba(24, 95, 165, 0.15);
        --blue-border: rgba(24, 95, 165, 0.4);
        --green: #3ecfa0;
        --green-bg: rgba(15, 110, 86, 0.15);
        --green-border: rgba(15, 110, 86, 0.4);
        --grp: #4a9ec8;
        --grp-bg: rgba(74, 158, 200, 0.09);
    }
}

/* ===== Base ===== */
html,
body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: var(--bg2);
    color: var(--text);
    font-size: 15px;
    line-height: 1.5;
}

button {
    cursor: pointer;
    font-family: inherit;
}

input,
textarea {
    font-family: inherit;
}

input:focus,
textarea:focus {
    outline: none;
}

/* ===== Layout ===== */
.app {
    display: flex;
    height: 100dvh;
    overflow: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-w);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg2);
    border-right: 0.5px solid var(--border);
    overflow: hidden;
}

.sidebar-header {
    padding: 14px 12px 10px;
    border-bottom: 0.5px solid var(--border);
    flex-shrink: 0;
}

.sidebar-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}

.sidebar-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
}

.sidebar-status {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--text3);
    transition: background .3s;
}

.sidebar-status.online {
    background: var(--green);
}

.sidebar-status.error {
    background: #e24b4a;
}

.search-wrap {
    position: relative;
}

.search-wrap svg {
    position: absolute;
    left: 9px;
    top: 50%;
    transform: translateY(-50%);
    width: 14px;
    height: 14px;
    color: var(--text3);
    pointer-events: none;
}

#searchInput {
    width: 100%;
    padding: 7px 10px 7px 30px;
    font-size: 13px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border);
    background: var(--bg);
    color: var(--text);
}

#searchInput::placeholder {
    color: var(--text3);
}

.note-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px;
    -webkit-overflow-scrolling: touch;
}

.note-item {
    padding: 9px 10px;
    border-radius: var(--radius-md);
    cursor: pointer;
    margin-bottom: 2px;
    border: 0.5px solid transparent;
    transition: background .1s;
}

.note-item:hover {
    background: var(--bg3);
}

.note-item.active {
    background: var(--bg);
    border-color: var(--border2);
}

.note-item-title {
    font-size: 13px;
    font-weight: 500;
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: 4px;
    color: var(--text);
}

.note-item-title svg {
    flex-shrink: 0;
    color: var(--text3);
}

.note-item-meta {
    font-size: 11px;
    color: var(--text3);
}

.note-item-preview {
    font-size: 12px;
    color: var(--text2);
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-footer {
    padding: 10px 12px;
    padding-bottom: calc(10px + var(--safe-bottom));
    border-top: 0.5px solid var(--border);
    flex-shrink: 0;
}

.new-note-btn {
    width: 100%;
    padding: 8px;
    font-size: 13px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border2);
    background: var(--bg);
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.new-note-btn:hover {
    background: var(--bg3);
}

.new-note-btn svg {
    width: 14px;
    height: 14px;
}

.room-info {
    font-size: 11px;
    color: var(--text3);
    padding: 6px 2px 2px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
}

.room-info-label {
    display: flex;
    align-items: center;
    gap: 4px;
    overflow: hidden;
}

.room-info-label svg {
    flex-shrink: 0;
    width: 11px;
    height: 11px;
}

.room-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.logout-btn {
    flex-shrink: 0;
    font-size: 11px;
    color: var(--text3);
    background: none;
    border: 0.5px solid var(--border);
    border-radius: 4px;
    padding: 2px 7px;
    cursor: pointer;
}

.logout-btn:hover {
    color: #e24b4a;
    border-color: #e24b4a;
}

/* ===== Main ===== */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--bg);
    min-width: 0;
}

.main-header {
    height: var(--header-h);
    padding: 0 16px;
    border-bottom: 0.5px solid var(--border);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.back-btn {
    display: none;
    align-items: center;
    gap: 3px;
    font-size: 13px;
    color: var(--blue);
    background: none;
    border: none;
    padding: 6px 4px;
    flex-shrink: 0;
}

.back-btn svg {
    width: 16px;
    height: 16px;
}

.header-center {
    flex: 1;
    min-width: 0;
}

#noteTitleInput {
    font-size: 17px;
    font-weight: 600;
    border: none;
    background: transparent;
    width: 100%;
    color: var(--text);
}

#noteTitleInput::placeholder {
    color: var(--text3);
    font-weight: 400;
}

.note-updated {
    font-size: 11px;
    color: var(--text3);
    margin-top: 1px;
}

.header-actions {
    display: flex;
    gap: 5px;
    flex-shrink: 0;
}

.icon-btn {
    width: 32px;
    height: 32px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border);
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text2);
    transition: background .1s;
}

.icon-btn svg {
    width: 16px;
    height: 16px;
}

.icon-btn:hover {
    background: var(--bg2);
}

.icon-btn.active {
    color: var(--blue);
    border-color: var(--blue-border);
    background: var(--blue-bg);
}

/* ===== Sort mode banner ===== */
.sort-banner {
    display: none;
    background: var(--blue-bg);
    border-bottom: 0.5px solid var(--blue-border);
    padding: 8px 16px;
    font-size: 12px;
    color: var(--blue);
    text-align: center;
    flex-shrink: 0;
}

.sort-banner.show {
    display: block;
}

/* ===== Blocks ===== */
.blocks-wrap {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.blocks-area {
    padding: 14px 16px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* ===== Group ===== */
.block-group {
    position: relative;
    border: 1.5px solid var(--grp);
    border-radius: 14px;
    background: var(--grp-bg);
    padding: 0 10px 10px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.block-group.drag-over-group {
    border: 2px dashed var(--blue);
    background: color-mix(in srgb, var(--blue) 6%, var(--bg));
    transition: background 150ms, border-color 150ms;
}

/* 挿入位置インジケーター */
.drop-indicator {
    display: none;
    position: absolute;
    height: 3px;
    border-radius: 3px;
    background: var(--blue);
    pointer-events: none;
    z-index: 100;
    box-shadow: 0 0 6px color-mix(in srgb, var(--blue) 60%, transparent);
}

.drop-indicator::before,
.drop-indicator::after {
    content: '';
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--blue);
}

.drop-indicator::before {
    left: -4px;
}

.drop-indicator::after {
    right: -4px;
}

.block-group-label {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    width: 100%;
    padding: 0 4px;
    max-width: 100%;
    line-height: 1;
}

.block-group-label span {
    font-size: var(--group-font-size);
    font-weight: 700;
    color: var(--grp);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
    text-align: center;
    flex: 1;
    line-height: 1;
}

.block-group-label span:hover {
    opacity: .7;
}

.block-group-label input {
    font-size: var(--group-font-size);
    font-weight: 700;
    color: var(--grp);
    border: none;
    background: transparent;
    padding: 0;
    width: 140px;
    display: none;
    text-align: center;
}

.grp-unlink {
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    border: none;
    background: none;
    color: var(--text3);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    opacity: .6;
}

.grp-unlink:hover {
    color: #e24b4a;
    opacity: 1;
}

.grp-unlink svg {
    width: 13px;
    height: 13px;
}

/* グループ内ブロック追加エリア */
.grp-add-area {
    display: flex;
    align-items: flex-start;
    gap: 6px;
    padding: 0 2px;
}

.grp-add-ta {
    flex: 1;
    resize: none;
    border: 0.5px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text1);
    font-size: 12.5px;
    line-height: 1.4;
    padding: 5px 8px;
    height: 28px;
    min-height: 28px;
    outline: none;
    transition: border-color .15s;
    font-family: inherit;
}

.grp-add-ta:focus {
    border-color: var(--grp);
}

.grp-add-ta::placeholder {
    color: var(--text3);
    opacity: .7;
}

.grp-add-btn {
    flex-shrink: 0;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    border: none;
    background: var(--grp);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: .85;
    transition: opacity .15s;
    padding: 0;
}

.grp-add-btn:hover {
    opacity: 1;
}

.grp-add-btn svg {
    width: 14px;
    height: 14px;
}

/* グループドラッグハンドル */
.group-drag-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text3);
    touch-action: none;
    cursor: grab;
    padding: 6px 4px;
    visibility: hidden;
    pointer-events: none;
}

.group-drag-handle svg {
    width: 18px;
    height: 18px;
}

.sort-mode .group-drag-handle {
    visibility: visible;
    pointer-events: auto;
}

/* ===== Block card ===== */
.block-card {
    border: 0.5px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 12px 14px;
    background: var(--bg);
    position: relative;
    transition: border-color .12s;
}

.block-card.pinned {
    background: var(--bg2);
    border-color: var(--border2);
}

.block-card.flash-copy {
    border-color: var(--green);
}

.block-card.drag-over {
    border: 1.5px dashed var(--blue);
}

.block-card.dragging {
    opacity: .22;
    transition: opacity 150ms;
}

/* 並び替えモード時カードスタイル */
.sort-mode .block-card {
    padding-left: 44px;
}

.sort-mode .block-card .block-header {
    pointer-events: none;
}

.sort-mode .block-card .block-text {
    cursor: default;
}

/* ソートモード中はテキスト選択を完全禁止 */
.sort-mode * {
    user-select: none !important;
    -webkit-user-select: none !important;
}

/* ドラッグハンドル */
.sort-handle {
    display: flex;
    align-items: center;
    position: absolute;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text3);
    touch-action: none;
    cursor: grab;
    padding: 4px;
    visibility: hidden;
    pointer-events: none;
}

.sort-handle:active {
    cursor: grabbing;
}

.sort-handle svg {
    width: 18px;
    height: 18px;
}

.sort-mode .sort-handle {
    visibility: visible;
    pointer-events: auto;
}

.block-header {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 8px;
}

.block-time {
    font-size: 11px;
    color: var(--text3);
    display: flex;
    align-items: center;
    gap: 3px;
    flex: 1;
}

.block-time svg {
    width: 11px;
    height: 11px;
}

.block-actions {
    display: flex;
    gap: 4px;
    position: relative;
}

.block-act {
    width: 28px;
    height: 28px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border);
    background: var(--bg2);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text2);
}

.block-act svg {
    width: 14px;
    height: 14px;
}

.block-act:hover {
    background: var(--bg3);
    color: var(--text);
}

.block-act.pin-on {
    color: var(--blue);
    border-color: var(--blue-border);
    background: var(--blue-bg);
}

.block-act.grp-on {
    color: var(--grp);
    border-color: var(--grp);
    background: var(--grp-bg);
}

/* 並び替えモード中はアクションボタン非表示 */
.sort-mode .block-actions {
    display: none;
}

.sort-mode .block-time {
    pointer-events: none;
}

.block-text {
    font-size: var(--block-font-size);
    line-height: 1.7;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'SF Mono', 'Menlo', 'Monaco', 'Consolas', monospace;
    cursor: pointer;
    user-select: text;
}

.copy-badge {
    position: absolute;
    bottom: 10px;
    right: 12px;
    font-size: 11px;
    color: var(--green);
    background: var(--green-bg);
    border: 0.5px solid var(--green-border);
    padding: 2px 10px;
    border-radius: 999px;
    pointer-events: none;
    opacity: 0;
    transition: opacity .15s;
}

.copy-badge.show {
    opacity: 1;
}

.block-edit-wrap {
    display: none;
}

.block-edit-wrap.active {
    display: block;
}

.block-text-wrap.editing {
    display: none;
}

.block-edit-ta {
    width: 100%;
    font-size: var(--block-font-size);
    line-height: 1.7;
    color: var(--text);
    background: var(--bg);
    border: none;
    border-radius: var(--radius-md);
    resize: none;
    font-family: 'SF Mono', 'Menlo', 'Monaco', 'Consolas', monospace;
    padding: 0;
    min-height: 60px;
}

.block-edit-actions {
    display: flex;
    justify-content: flex-end;
    gap: 6px;
    margin-top: 8px;
}

.edit-cancel-btn,
.edit-save-btn {
    padding: 5px 14px;
    font-size: 12px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border2);
    background: var(--bg2);
    color: var(--text);
}

.edit-save-btn {
    background: var(--blue);
    color: #fff;
    border-color: var(--blue);
}

.edit-save-btn:hover {
    opacity: .9;
}

/* ===== Group picker popup ===== */
.grp-picker {
    display: none;
    position: absolute;
    right: 0;
    top: 34px;
    background: var(--bg);
    border: 0.5px solid var(--border2);
    border-radius: var(--radius-lg);
    padding: 6px;
    z-index: 60;
    min-width: 150px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, .13);
}

.grp-picker.open {
    display: block;
}

.grp-picker-item {
    padding: 7px 10px;
    font-size: 13px;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--text);
    white-space: nowrap;
}

.grp-picker-item:hover {
    background: var(--bg2);
}

.grp-picker-item.cur {
    color: var(--grp);
    font-weight: 500;
}

.grp-picker-sep {
    height: 0.5px;
    background: var(--border);
    margin: 4px 0;
}

.grp-picker-new {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 10px;
    font-size: 13px;
    border-radius: var(--radius-md);
    cursor: pointer;
    color: var(--blue);
}

.grp-picker-new:hover {
    background: var(--bg2);
}

.grp-picker-new svg {
    width: 13px;
    height: 13px;
}

/* ===== Empty state ===== */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 20px;
    color: var(--text3);
    font-size: 14px;
    text-align: center;
    gap: 10px;
}

.empty-state svg {
    width: 36px;
    height: 36px;
    opacity: .5;
}

/* ===== Add bar ===== */
.add-bar {
    padding: 10px 16px;
    padding-bottom: calc(10px + var(--safe-bottom));
    border-top: 0.5px solid var(--border);
    display: flex;
    gap: 8px;
    flex-shrink: 0;
    background: var(--bg);
}

#newBlockText {
    flex: 1;
    padding: 8px 12px;
    font-size: 13px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border);
    background: var(--bg2);
    color: var(--text);
    resize: none;
    height: 38px;
    max-height: 120px;
    line-height: 1.5;
    font-family: 'SF Mono', 'Menlo', 'Monaco', 'Consolas', monospace;
    transition: border-color .1s, background .1s;
}

#newBlockText:focus {
    border-color: var(--border2);
    background: var(--bg);
}

#newBlockText::placeholder {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    color: var(--text3);
}

.add-btn {
    padding: 0 14px;
    height: 38px;
    font-size: 13px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border2);
    background: var(--bg2);
    color: var(--text);
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 5px;
    flex-shrink: 0;
}

.add-btn svg {
    width: 14px;
    height: 14px;
}

.add-btn:hover {
    background: var(--bg3);
}

/* sort mode: hide add bar */
.sort-mode-active .add-bar {
    display: none !important;
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: calc(80px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: var(--text);
    color: var(--bg);
    font-size: 13px;
    padding: 8px 18px;
    border-radius: 999px;
    opacity: 0;
    transition: opacity .2s, transform .2s;
    pointer-events: none;
    white-space: nowrap;
    z-index: 100;
}

.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
}

.modal-overlay.hidden {
    display: none;
}

.modal {
    background: var(--bg);
    border-radius: var(--radius-lg);
    padding: 24px;
    width: 100%;
    max-width: 420px;
    border: 0.5px solid var(--border2);
    max-height: 90vh;
    overflow-y: auto;
}

.modal h2 {
    font-size: 17px;
    font-weight: 600;
    margin-bottom: 16px;
}

.modal-section {
    margin-bottom: 20px;
    padding-bottom: 20px;
    border-bottom: 0.5px solid var(--border);
}

.modal-section:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
}

.modal-section h3 {
    font-size: 13px;
    font-weight: 600;
    color: var(--text2);
    text-transform: uppercase;
    letter-spacing: .05em;
    margin-bottom: 12px;
}

.modal label {
    font-size: 12px;
    color: var(--text2);
    display: block;
    margin-bottom: 4px;
}

.modal input[type="text"],
.modal input[type="url"],
.modal input[type="password"] {
    width: 100%;
    padding: 9px 12px;
    font-size: 14px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border2);
    background: var(--bg2);
    color: var(--text);
    margin-bottom: 14px;
}

.modal input:focus {
    border-color: var(--blue);
}

/* range slider */
.slider-row {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 14px;
}

.slider-row input[type="range"] {
    flex: 1;
    accent-color: var(--blue);
    margin: 0;
}

.slider-val {
    font-size: 13px;
    color: var(--text2);
    min-width: 38px;
    text-align: right;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 4px;
}

.btn-primary {
    padding: 8px 20px;
    font-size: 14px;
    border-radius: var(--radius-md);
    border: none;
    background: var(--blue);
    color: #fff;
}

.btn-primary:hover {
    opacity: .9;
}

.btn-secondary {
    padding: 8px 16px;
    font-size: 14px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border2);
    background: var(--bg2);
    color: var(--text);
}

.btn-danger {
    padding: 8px 16px;
    font-size: 14px;
    border-radius: var(--radius-md);
    border: none;
    background: #e24b4a;
    color: #fff;
}

.btn-danger:hover {
    opacity: .9;
}

/* export button */
.export-btn {
    width: 100%;
    padding: 9px;
    font-size: 13px;
    border-radius: var(--radius-md);
    border: 0.5px solid var(--border2);
    background: var(--bg2);
    color: var(--text);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
}

.export-btn:hover {
    background: var(--bg3);
}

.export-btn svg {
    width: 15px;
    height: 15px;
}

/* ===== Mobile ===== */
@media (max-width: 600px) {
    :root {
        --sidebar-w: 100%;
    }

    .sidebar {
        position: fixed;
        inset: 0;
        z-index: 50;
        width: 100%;
        transform: translateX(-100%);
        transition: transform .25s ease;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .back-btn {
        display: flex;
    }

    .main-header {
        padding: 0 12px;
    }

    #noteTitleInput {
        font-size: 15px;
    }

    .blocks-area {
        padding: 12px 14px 14px;
    }

    .add-bar {
        padding: 8px 14px;
        padding-bottom: calc(8px + var(--safe-bottom));
    }
}

@media (min-width: 601px) {
    .sidebar {
        transform: none !important;
    }
}